Module: AwesomeCounterCache::ClassMethods

Defined in:
lib/awesome_counter_cache/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#awesome_counter_cache_for(relation_name, args = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/awesome_counter_cache/class_methods.rb', line 2

def awesome_counter_cache_for(relation_name, args = {})
  id = awesome_counter_caches.length
  args[:column_name] ||= "#{model_name.plural}_count"
  args[:delta_magnitude] ||= proc { 1 }
  args[:model_class] = self
  args[:relation_name] = relation_name
  args[:id] = id

  counter_cache = AwesomeCounterCache::CounterCache.new(**args)
  awesome_counter_caches[id] = counter_cache
  awesome_counter_cache_init_callbacks(counter_cache)
end

#awesome_counter_cache_init_callbacks(counter_cache) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/awesome_counter_cache/class_methods.rb', line 15

def awesome_counter_cache_init_callbacks(counter_cache)
  after_initialize do |model|
    awesome_counter_cache_after_initialize(counter_cache, model)
  end

  after_commit on: :create do |model|
    awesome_counter_cache_after_commit_on_create(counter_cache, model)
  end

  after_commit on: :destroy do |model|
    awesome_counter_cache_after_commit_on_destroy(counter_cache, model)
  end

  after_commit on: :update do |model|
    awesome_counter_cache_after_commit_on_update(counter_cache, model)
  end
end

#awesome_counter_cachesObject



33
34
35
# File 'lib/awesome_counter_cache/class_methods.rb', line 33

def awesome_counter_caches
  @awesome_counter_caches ||= {}
end