Module: CustomCounterCache::Model::ActsAsMethods

Defined in:
lib/custom_counter_cache/model.rb

Instance Method Summary collapse

Instance Method Details

#define_counter_cache(cache_column, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/custom_counter_cache/model.rb', line 9

def define_counter_cache(cache_column, &block)
  # counter accessors
  unless column_names.include?(cache_column) # Object.const_defined?(:Counter)
    has_many :counters, :as => :countable, :dependent => :destroy
    define_method "#{cache_column}" do
      # Check if the counter is already loaded (e.g. eager-loaded)
      if counters.loaded? && counter = counters.detect{|c| c.key == cache_column.to_s }
        counter.value
      else
        counters.find(:first, :conditions => { :key => cache_column.to_s }).value rescue 0
      end
    end
    define_method "#{cache_column}=" do |count|
      if ( counter = counters.find(:first, :conditions => { :key => cache_column.to_s }) )
        counter.update_attribute :value, count.to_i
      else
        counters.create :key => cache_column.to_s, :value => count.to_i
      end
    end
  end
  # counter update method
  define_method "update_#{cache_column}" do
    send "#{cache_column}=", block.call(self)
  end
end

#update_counter_cache(association, cache_column, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/custom_counter_cache/model.rb', line 35

def update_counter_cache(association, cache_column, options = {})
  association  = association.to_sym
  cache_column = cache_column.to_sym
  method_name  = "callback_#{cache_column}".to_sym
  reflection   = reflect_on_association(association)
  foreign_key  = reflection.options[:foreign_key] || reflection.association_foreign_key
  # define callback
  define_method method_name do
    # update old association
    if send("#{foreign_key}_changed?") || ( !respond_to?("#{association}_type") || send("#{association}_type_changed?") )
      old_id = send("#{foreign_key}_was")
      klass = if reflection.options[:polymorphic]
        ( send("#{association}_type_was") || send("#{association}_type") ).constantize
      else
        reflection.klass
      end
      if ( old_id && record = klass.find(old_id) )
        record.send("update_#{cache_column}")
      end
    end
    # update new association
    if ( record = send(association) )
      record.send("update_#{cache_column}")
    end
  end
  # set callbacks
  after_create  method_name, options
  after_update  method_name, options
  after_destroy method_name, options
end