lib/bullet/detector/counter.rb in bullet-2.2.1 vs lib/bullet/detector/counter.rb in bullet-2.3.0

- old
+ new

@@ -1,43 +1,48 @@ module Bullet module Detector class Counter < Base - def self.clear - @@possible_objects = nil - @@impossible_objects = nil - end + class <<self + def clear + @@possible_objects = nil + @@impossible_objects = nil + end - def self.add_counter_cache(object, associations) - if conditions_met?( object, associations ) - create_notification object.class, associations + def add_counter_cache(object, associations) + if conditions_met?(object.ar_key, associations) + create_notification object.class.to_s, associations + end end - end - def self.add_possible_objects(objects) - possible_objects.add objects - end + def add_possible_objects(object_or_objects) + if object_or_objects.is_a? Array + object_or_objects.each { |object| possible_objects.add object.ar_key } + else + possible_objects.add object_or_objects.ar_key + end + end - def self.add_impossible_object(object) - impossible_objects.add object - end - - private - def self.create_notification( klazz, associations ) - notice = Bullet::Notification::CounterCache.new klazz, associations - Bullet.notification_collector.add notice - end + def add_impossible_object(object) + impossible_objects.add object.ar_key + end - def self.possible_objects - @@possible_objects ||= Bullet::Registry::Object.new - end + private + def create_notification(klazz, associations) + notice = Bullet::Notification::CounterCache.new klazz, associations + Bullet.notification_collector.add notice + end - def self.impossible_objects - @@impossible_objects ||= Bullet::Registry::Object.new - end + def possible_objects + @@possible_objects ||= Bullet::Registry::Object.new + end - def self.conditions_met?( object, associations ) - possible_objects.contains?( object ) and - !impossible_objects.contains?( object ) + def impossible_objects + @@impossible_objects ||= Bullet::Registry::Object.new + end + + def conditions_met?(object_ar_key, associations) + possible_objects.include?(object_ar_key) && !impossible_objects.include?(object_ar_key) + end end end end end