lib/bullet.rb in bullet-4.9.0 vs lib/bullet.rb in bullet-4.10.0

- old
+ new

@@ -25,11 +25,11 @@ end class << self attr_writer :enable, :n_plus_one_query_enable, :unused_eager_loading_enable, :counter_cache_enable, :stacktrace_includes attr_reader :notification_collector, :whitelist - attr_accessor :add_footer + attr_accessor :add_footer, :orm_pathches_applied delegate :alert=, :console=, :growl=, :rails_logger=, :xmpp=, :airbrake=, :bugsnag=, :to => UniformNotifier def raise=(should_raise) UniformNotifier.raise=(should_raise ? Notification::UnoptimizedQueryError : false) @@ -41,12 +41,15 @@ def enable=(enable) @enable = @n_plus_one_query_enable = @unused_eager_loading_enable = @counter_cache_enable = enable if enable? reset_whitelist - Bullet::Mongoid.enable if mongoid? - Bullet::ActiveRecord.enable if active_record? + unless orm_pathches_applied + self.orm_pathches_applied = true + Bullet::Mongoid.enable if mongoid? + Bullet::ActiveRecord.enable if active_record? + end end end def enable? !!@enable @@ -81,30 +84,56 @@ @whitelist = {:n_plus_one_query => {}, :unused_eager_loading => {}, :counter_cache => {}} end def bullet_logger=(active) if active - bullet_log_file = File.open("#{rails? ? Rails.root.to_s : Dir.pwd}/log/bullet.log", 'a+') + require 'fileutils' + root_path = "#{rails? ? Rails.root.to_s : Dir.pwd}" + FileUtils.mkdir_p(root_path + '/log') + bullet_log_file = File.open("#{root_path}/log/bullet.log", 'a+') bullet_log_file.sync = true UniformNotifier.customized_logger = bullet_log_file end end + def debug(title, message) + puts "[Bullet][#{title}] #{message}" if ENV['DEBUG'] == 'true' + end + def start_request - notification_collector.reset - DETECTORS.each { |bullet| bullet.start_request } + Thread.current[:bullet_start] = true + Thread.current[:bullet_notification_collector] = Bullet::NotificationCollector.new + + Thread.current[:bullet_object_associations] = Bullet::Registry::Base.new + Thread.current[:bullet_call_object_associations] = Bullet::Registry::Base.new + Thread.current[:bullet_possible_objects] = Bullet::Registry::Object.new + Thread.current[:bullet_impossible_objects] = Bullet::Registry::Object.new + Thread.current[:bullet_eager_loadings] = Bullet::Registry::Association.new + + Thread.current[:bullet_counter_possible_objects] ||= Bullet::Registry::Object.new + Thread.current[:bullet_counter_impossible_objects] ||= Bullet::Registry::Object.new end def end_request - DETECTORS.each { |bullet| bullet.end_request } + Thread.current[:bullet_start] = nil + Thread.current[:bullet_notification_collector] = nil + + Thread.current[:bullet_object_associations] = nil + Thread.current[:bullet_possible_objects] = nil + Thread.current[:bullet_impossible_objects] = nil + Thread.current[:bullet_call_object_associations] = nil + Thread.current[:bullet_eager_loadings] = nil + + Thread.current[:bullet_counter_possible_objects] = nil + Thread.current[:bullet_counter_impossible_objects] = nil end - def clear - DETECTORS.each { |bullet| bullet.clear } + def start? + Thread.current[:bullet_start] end def notification_collector - @notification_collector ||= Bullet::NotificationCollector.new + Thread.current[:bullet_notification_collector] end def notification? Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations notification_collector.notifications_present?