module Mongoid::MagicCounterCache::ClassMethods
Public Instance Methods
counter_cache(*args, &block)
click to toggle source
# File lib/mongoid/magic_counter_cache.rb, line 67 def counter_cache(*args, &block) options = args.extract_options! name = options[:class] || args.first.to_s counter_name = get_counter_name(options) condition = options[:if] update_condition = options[:if_update] increment_proc = ->(doc, inc) do if doc.embedded? parent = doc._parent if parent.respond_to?(counter_name) increment_association(parent, counter_name.to_sym, inc) end else relation = doc.send(name) if relation && relation.class.fields.keys.include?(counter_name) increment_association(relation, counter_name.to_sym, inc) end end end callback_proc = ->(doc, inc) do result = condition_result(condition, doc) return unless result increment_proc.call(doc, inc) end update_callback_proc = ->(doc) do return if condition.nil? || update_condition.nil? # Don't execute if there is no update condition. return unless update_condition.call(doc) # Determine whether to execute update increment/decrements. inc = condition.call(doc) ? 1 : -1 increment_proc.call(doc, inc) end after_create( ->(doc) { callback_proc.call(doc, 1) }) after_destroy(->(doc) { callback_proc.call(doc, -1) }) after_update( ->(doc) { update_callback_proc.call(doc) }) end
Also aliased as: magic_counter_cache
Private Instance Methods
condition_result(condition, doc)
click to toggle source
# File lib/mongoid/magic_counter_cache.rb, line 115 def condition_result(condition, doc) return true if condition.nil? condition.call(doc) end
get_counter_name(options)
click to toggle source
# File lib/mongoid/magic_counter_cache.rb, line 111 def get_counter_name(options) options.fetch(:field, "#{actual_model_name.demodulize.underscore}_count").to_s end