lib/bullet.rb in bullet-6.1.4 vs lib/bullet.rb in bullet-6.1.5

- old
+ new

@@ -18,13 +18,10 @@ autoload :Notification, 'bullet/notification' autoload :Detector, 'bullet/detector' autoload :Registry, 'bullet/registry' autoload :NotificationCollector, 'bullet/notification_collector' - BULLET_DEBUG = 'BULLET_DEBUG' - TRUE = 'true' - if defined?(Rails::Railtie) class BulletRailtie < Rails::Railtie initializer 'bullet.configure_rails_initialization' do |app| app.middleware.use Bullet::Rack end @@ -36,14 +33,15 @@ :unused_eager_loading_enable, :counter_cache_enable, :stacktrace_includes, :stacktrace_excludes, :skip_html_injection - attr_reader :whitelist + attr_reader :safelist attr_accessor :add_footer, :orm_patches_applied, :skip_http_headers - available_notifiers = UniformNotifier::AVAILABLE_NOTIFIERS.select { |notifier| notifier != :raise }.map { |notifier| "#{notifier}=" } + available_notifiers = + UniformNotifier::AVAILABLE_NOTIFIERS.select { |notifier| notifier != :raise }.map { |notifier| "#{notifier}=" } available_notifiers_options = { to: UniformNotifier } delegate(*available_notifiers, **available_notifiers_options) def raise=(should_raise) UniformNotifier.raise = (should_raise ? Notification::UnoptimizedQueryError : false) @@ -57,11 +55,11 @@ def enable=(enable) @enable = @n_plus_one_query_enable = @unused_eager_loading_enable = @counter_cache_enable = enable if enable? - reset_whitelist + reset_safelist unless orm_patches_applied self.orm_patches_applied = true Bullet::Mongoid.enable if mongoid? Bullet::ActiveRecord.enable if active_record? end @@ -70,12 +68,13 @@ def enable? !!@enable end + # Rails.root might be nil if `railties` is a dependency on a project that does not use Rails def app_root - @app_root ||= (defined?(::Rails.root) ? Rails.root.to_s : Dir.pwd).to_s + @app_root ||= (defined?(::Rails.root) && !::Rails.root.nil? ? Rails.root.to_s : Dir.pwd).to_s end def n_plus_one_query_enable? enable? && !!@n_plus_one_query_enable end @@ -94,33 +93,78 @@ def stacktrace_excludes @stacktrace_excludes ||= [] end + def add_safelist(options) + reset_safelist + @safelist[options[:type]][options[:class_name]] ||= [] + @safelist[options[:type]][options[:class_name]] << options[:association].to_sym + end + + def delete_safelist(options) + reset_safelist + @safelist[options[:type]][options[:class_name]] ||= [] + @safelist[options[:type]][options[:class_name]].delete(options[:association].to_sym) + @safelist[options[:type]].delete_if { |_key, val| val.empty? } + end + + def get_safelist_associations(type, class_name) + Array(@safelist[type][class_name]) + end + + def reset_safelist + @safelist ||= { n_plus_one_query: {}, unused_eager_loading: {}, counter_cache: {} } + end + + def clear_safelist + @safelist = nil + end + def add_whitelist(options) - reset_whitelist - @whitelist[options[:type]][options[:class_name]] ||= [] - @whitelist[options[:type]][options[:class_name]] << options[:association].to_sym + ActiveSupport::Deprecation.warn(<<~WARN.strip + add_whitelist is deprecated in favor of add_safelist. It will be removed from the next major release. + WARN + ) + + add_safelist(options) end def delete_whitelist(options) - reset_whitelist - @whitelist[options[:type]][options[:class_name]] ||= [] - @whitelist[options[:type]][options[:class_name]].delete(options[:association].to_sym) - @whitelist[options[:type]].delete_if { |_key, val| val.empty? } + ActiveSupport::Deprecation.warn(<<~WARN.strip + delete_whitelist is deprecated in favor of delete_safelist. It will be removed from the next major release. + WARN + ) + + delete_safelist(options) end def get_whitelist_associations(type, class_name) - Array(@whitelist[type][class_name]) + ActiveSupport::Deprecation.warn(<<~WARN.strip + get_whitelist_associations is deprecated in favor of get_safelist_associations. It will be removed from the next major release. + WARN + ) + + get_safelist_associations(type, class_name) end def reset_whitelist - @whitelist ||= { n_plus_one_query: {}, unused_eager_loading: {}, counter_cache: {} } + ActiveSupport::Deprecation.warn(<<~WARN.strip + reset_whitelist is deprecated in favor of reset_safelist. It will be removed from the next major release. + WARN + ) + + reset_safelist end def clear_whitelist - @whitelist = nil + ActiveSupport::Deprecation.warn(<<~WARN.strip + clear_whitelist is deprecated in favor of clear_safelist. It will be removed from the next major release. + WARN + ) + + clear_safelist end def bullet_logger=(active) if active require 'fileutils' @@ -130,10 +174,10 @@ UniformNotifier.customized_logger = bullet_log_file end end def debug(title, message) - puts "[Bullet][#{title}] #{message}" if ENV[BULLET_DEBUG] == TRUE + puts "[Bullet][#{title}] #{message}" if ENV['BULLET_DEBUG'] == 'true' end def start_request Thread.current[:bullet_start] = true Thread.current[:bullet_notification_collector] = Bullet::NotificationCollector.new