Module: FancyCount::CounterCacheable

Extended by:
ActiveSupport::Concern
Defined in:
lib/fancy_count/counter_cacheable.rb

Instance Method Summary collapse

Instance Method Details

#fancy_association_count(association, counter_name) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/fancy_count/counter_cacheable.rb', line 92

def fancy_association_count(association, counter_name)
  scope = association.public_send(counter_name)

  if association.respond_to?(:kept)
    scope = scope.kept
  end

  scope.count
end

#fancy_counter_cache_reconcile(name) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/fancy_count/counter_cacheable.rb', line 83

def fancy_counter_cache_reconcile(name)
  data = self.class.fancy_counter_caches.detect { |entry| entry[:name] == name }
  raise "Unknown counter #{name}" if data.blank?

  association = public_send(data[:association_name])
  new_count = fancy_association_count(association, name)
  association.public_send(data[:counter_name]).change(new_count)
end