lib/bullet.rb in bullet-4.4.0 vs lib/bullet.rb in bullet-4.5.0

- old
+ new

@@ -28,22 +28,23 @@ end end end class <<self - attr_accessor :enable - attr_reader :notification_collector + attr_writer :enable, :n_plus_one_query_enable, :unused_eager_loading_enable, :counter_cache_enable + attr_reader :notification_collector, :whitelist delegate :alert=, :console=, :growl=, :rails_logger=, :xmpp=, :airbrake=, :to => UniformNotifier DETECTORS = [ Bullet::Detector::NPlusOneQuery, - Bullet::Detector::UnusedEagerAssociation, - Bullet::Detector::Counter ] + Bullet::Detector::UnusedEagerLoading, + Bullet::Detector::CounterCache ] def enable=(enable) - @enable = enable + @enable = @n_plus_one_query_enable = @unused_eager_loading_enable = @counter_cache_enable = enable if enable? + reset_whitelist if mongoid? Bullet::Mongoid.enable end if active_record? Bullet::ActiveRecord.enable @@ -51,13 +52,38 @@ end end end def enable? - @enable == true + !!@enable end + def n_plus_one_query_enable? + self.enable? && !!@n_plus_one_query_enable + end + + def unused_eager_loading_enable? + self.enable? && !!@unused_eager_loading_enable + end + + def counter_cache_enable? + self.enable? && !!@counter_cache_enable + end + + def add_whitelist(options) + @whitelist[options[:type]][options[:class_name].classify] ||= [] + @whitelist[options[:type]][options[:class_name].classify] << options[:association].to_s.tableize.to_sym + end + + def get_whitelist_associations(type, class_name) + Array(@whitelist[type][class_name]) + end + + def reset_whitelist + @whitelist = {:n_plus_one_query => {}, :unused_eager_loading => {}, :counter_cache => {}} + end + def bullet_logger=(active) if active bullet_log_file = File.open("#{rails2? ? RAILS_ROOT : Rails.root.to_s}/log/bullet.log", 'a+') bullet_log_file.sync = true UniformNotifier.customized_logger = bullet_log_file @@ -80,11 +106,11 @@ def notification_collector @notification_collector ||= Bullet::NotificationCollector.new end def notification? - Bullet::Detector::UnusedEagerAssociation.check_unused_preload_associations + Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations notification_collector.notifications_present? end def gather_inline_notifications responses = [] @@ -96,9 +122,18 @@ def perform_out_of_channel_notifications(env = {}) for_each_active_notifier_with_notification do |notification| notification.url = [env['HTTP_HOST'], env['REQUEST_URI']].compact.join notification.notify_out_of_channel + end + end + + def warnings + notification_collector.collection.inject({}) do |warnings, notification| + warning_type = notification.class.to_s.split(':').last.tableize + warnings[warning_type] ||= [] + warnings[warning_type] << notification + warnings end end private def for_each_active_notifier_with_notification