Module: CounterOne::Extensions::ClassMethods

Defined in:
lib/counter_one/extensions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#counter_one_cacheObject (readonly)

Returns the value of attribute counter_one_cache.



7
8
9
# File 'lib/counter_one/extensions.rb', line 7

def counter_one_cache
  @counter_one_cache
end

Instance Method Details

#counter_one(relation, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/counter_one/extensions.rb', line 9

def counter_one(relation, options = {})
  @counter_one_cache ||= []

  @counter_one_cache << Counter.new(self, relation, options)

  on = [options[:on]].flatten.compact

  after_create  :increment_counters if on.empty? || on.include?(:create)
  after_destroy :decrement_counters if on.empty? || on.include?(:destroy)
  after_update  :update_counters if on.empty? || on.include?(:update)
end

#counter_one_recalculate(relation = nil) ⇒ Object



21
22
23
24
# File 'lib/counter_one/extensions.rb', line 21

def counter_one_recalculate(relation = nil)
  counters = relation ? counter_one_cache.select { |counter| counter.relation == [relation].flatten } : counter_one_cache
  counters.each { |counter| recalculate_counters(counter) }
end