Module: ArAsyncCounterCache::ActiveRecord::ClassMethods

Defined in:
lib/ar_async_counter_cache/active_record.rb

Instance Method Summary collapse

Instance Method Details

#async_counter_typesObject



47
48
49
# File 'lib/ar_async_counter_cache/active_record.rb', line 47

def async_counter_types
  @async_counter_types ||= {}
end

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ar_async_counter_cache/active_record.rb', line 29

def belongs_to(association_id, options={})
  if col = async_counter_cache_column(options.delete(:async_counter_cache))
    # Add callbacks only once: update_async_counters updates all the
    # model's belongs_to counter caches; we do this because there's no
    # need to add excessive messages on the async queue.
    add_callbacks if self.async_counter_types.empty?
    # Store async counter so that update_async_counters knows which
    # counters to update.
    self.async_counter_types[association_id] = col
    # Let's make the column read-only like the normal belongs_to
    # counter_cache.
    parrent_class = association_id.to_s.classify.constantize
    parrent_class.send(:attr_readonly, col.to_sym) if defined?(parrent_class) && parrent_class.respond_to?(:attr_readonly)
    raise "Please don't use both async_counter_cache and counter_cache." if options[:counter_cache]
  end
  super(association_id, options)
end