Class: Bullet::Detector::CounterCache

Inherits:
Base show all
Defined in:
lib/bullet/detector/counter_cache.rb

Class Method Summary collapse

Class Method Details

.add_counter_cache(object, associations) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/bullet/detector/counter_cache.rb', line 7

def add_counter_cache(object, associations)
  return unless Bullet.start?
  return unless Bullet.counter_cache_enable?
  return unless object.bullet_primary_key_value

  Bullet.debug(
    'Detector::CounterCache#add_counter_cache',
    "object: #{object.bullet_key}, associations: #{associations}"
  )
  create_notification object.class.to_s, associations if conditions_met?(object, associations)
end

.add_impossible_object(object) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/bullet/detector/counter_cache.rb', line 33

def add_impossible_object(object)
  return unless Bullet.start?
  return unless Bullet.counter_cache_enable?
  return unless object.bullet_primary_key_value

  Bullet.debug('Detector::CounterCache#add_impossible_object', "object: #{object.bullet_key}")
  impossible_objects.add object.bullet_key
end

.add_possible_objects(object_or_objects) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bullet/detector/counter_cache.rb', line 19

def add_possible_objects(object_or_objects)
  return unless Bullet.start?
  return unless Bullet.counter_cache_enable?

  objects = Array.wrap(object_or_objects)
  return if objects.map(&:bullet_primary_key_value).compact.empty?

  Bullet.debug(
    'Detector::CounterCache#add_possible_objects',
    "objects: #{objects.map(&:bullet_key).join(', ')}"
  )
  objects.each { |object| possible_objects.add object.bullet_key }
end

.conditions_met?(object, _associations) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/bullet/detector/counter_cache.rb', line 42

def conditions_met?(object, _associations)
  possible_objects.include?(object.bullet_key) && !impossible_objects.include?(object.bullet_key)
end

.impossible_objectsObject



50
51
52
# File 'lib/bullet/detector/counter_cache.rb', line 50

def impossible_objects
  Thread.current[:bullet_counter_impossible_objects]
end

.possible_objectsObject



46
47
48
# File 'lib/bullet/detector/counter_cache.rb', line 46

def possible_objects
  Thread.current[:bullet_counter_possible_objects]
end