Module: ArResqueCounterCache::ActiveRecord::AsyncBuilderBehavior

Defined in:
lib/ar-resque-counter-cache/active_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



80
81
82
83
84
85
# File 'lib/ar-resque-counter-cache/active_record.rb', line 80

def self.included(base)
  base.class_eval do
    alias_method_chain :build, :async
    self.valid_options += [:async_counter_cache]
  end
end

Instance Method Details

#build_with_asyncObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ar-resque-counter-cache/active_record.rb', line 64

def build_with_async
  reflection = build_without_async
  if options[:async_counter_cache] && options[:counter_cache]
    raise 'Do not mix `:async_counter_cache` and `:counter_cache`.'
  end
  if options[:async_counter_cache]
    add_async_counter_cache_callbacks(reflection)
  end
  # At this point the standard counter cache callbacks will not be added;
  # however, we need the `:counter_cache` option to be present so that
  # `ActiveRecord::Base.reset_counters` works correctly (as of
  # activerecord 3.2.9).
  options[:counter_cache] = true if options[:async_counter_cache]
  reflection
end