Module: ArAsyncCounterCache::ActiveRecord::ClassMethods

Defined in:
lib/ar_async_counter_cache/active_record.rb

Instance Method Summary collapse

Instance Method Details

#async_counter_typesObject



37
38
39
# File 'lib/ar_async_counter_cache/active_record.rb', line 37

def async_counter_types
  @async_counter_types ||= {}
end

#belongs_to(association_id, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ar_async_counter_cache/active_record.rb', line 18

def belongs_to(association_id, options={})
  column = async_counter_cache_column(options.delete(:async_counter_cache))
  raise "Please don't use both async_counter_cache and counter_cache." if column && options[:counter_cache]
  super(association_id, options)
  if column
    # Store the async_counter_cache column for the update_async_counters
    # helper method.
    self.async_counter_types[association_id] = column
    # Fetch the reflection.
    reflection = self.reflect_on_association(association_id)
    parent_class = reflection.klass
    # Let's make the column read-only like the normal belongs_to
    # counter_cache.
    parent_class.send(:attr_readonly, column.to_sym) if defined?(parent_class) && parent_class.respond_to?(:attr_readonly)
    parent_id_column = reflection.primary_key_name
    add_callbacks(parent_class.to_s, parent_id_column, column)
  end
end