Skip to content

Commit

Permalink
Removing version flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
jah2488 committed Mar 4, 2014
1 parent b239731 commit 76c6b8f
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions lib/mongoid/magic_counter_cache.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'mongoid'
require 'mongoid/version'
module Mongoid #:nodoc:

# The Counter Cache will yada yada
#
# class Person
Expand Down Expand Up @@ -42,13 +41,33 @@ module Mongoid #:nodoc:
module MagicCounterCache
extend ActiveSupport::Concern

module LegacyCache
def actual_model_name
model_name
end

def increment_association(association, counter_name, inc)
association.inc(counter_name, inc)
end
end

module ModernCache
def actual_model_name
model_name.name
end

def increment_association(association, counter_name, inc)
association.inc(counter_name => inc)
end
end

module ClassMethods
include (Mongoid::VERSION.to_i >= 4) ? ModernCache : LegacyCache

def counter_cache(*args, &block)
options = args.extract_options!
name = options[:class] || args.first.to_s
version = (Mongoid::VERSION.to_i >= 4) ? true : false
counter_name = get_counter_name(options, version)
counter_name = get_counter_name(options)
condition = options[:if]

callback_proc = ->(doc, inc) do
Expand All @@ -57,12 +76,12 @@ def counter_cache(*args, &block)
if doc.embedded?
parent = doc._parent
if parent.respond_to?(counter_name)
increment_association(parent, counter_name.to_sym, inc, version)
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, version)
increment_association(relation, counter_name.to_sym, inc)
end
end
end
Expand All @@ -76,25 +95,14 @@ def counter_cache(*args, &block)

private

def get_counter_name(options, version)
return "#{options[:field].to_s}" if options[:field]
"#{actual_model_name(version).demodulize.underscore}_count"
end

def actual_model_name(version)
return model_name.name if version
model_name
def get_counter_name(options)
options.fetch(:field, "#{actual_model_name.demodulize.underscore}_count").to_s
end

def condition_result(condition, doc)
return true if condition.nil?
condition.call(doc)
end

def increment_association(association, counter_name, inc, version)
association.inc(counter_name => inc) if version
association.inc(counter_name, inc)
end
end
end
end

0 comments on commit 76c6b8f

Please sign in to comment.