Skip to content

Commit

Permalink
Code modified to run for 2.x to 4.x mongoid versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sameer-Tilak committed Feb 28, 2014
1 parent eff4757 commit cdd49a9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ source "http://rubygems.org"

# Specify your gem's dependencies in mongoid_magic_counter_cache.gemspec
gemspec
gem 'mongoid', '>= 4.0.0.alpha2'
#gem 'mongoid', '>= 4.0.0.alpha2'
#gem 'mongoid', '2.4.6'
#gem 'mongoid', '3.1.4'
32 changes: 27 additions & 5 deletions lib/mongoid/magic_counter_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ module ClassMethods
def counter_cache(*args, &block)
options = args.extract_options!
name = options[:class] || args.first.to_s
#version = Mongoid::VERSION::STRING
version = system('bundle show | grep "* mongoid (4."')

if options[:field]
counter_name = "#{options[:field].to_s}"
else
counter_name = "#{model_name.name.demodulize.underscore}_count"
if version
counter_name = "#{model_name.name.demodulize.underscore}_count"
else
counter_name = "#{model_name.demodulize.underscore}_count"
end
end

condition = options[:if]
Expand All @@ -61,11 +67,19 @@ def counter_cache(*args, &block)
if result
if doc.embedded?
parent = doc._parent
parent.inc(counter_name.to_sym => 1) if parent.respond_to? counter_name
if version
parent.inc(counter_name.to_sym => 1) if parent.respond_to? counter_name
else
parent.inc(counter_name.to_sym, 1) if parent.respond_to? counter_name
end
else
relation = doc.send(name)
if relation && relation.class.fields.keys.include?(counter_name)
relation.inc(counter_name.to_sym => 1)
if version
relation.inc(counter_name.to_sym => 1)
else
relation.inc(counter_name.to_sym, 1)
end
end
end
end
Expand All @@ -77,11 +91,19 @@ def counter_cache(*args, &block)
if result
if doc.embedded?
parent = doc._parent
parent.inc(counter_name.to_sym => -1) if parent.respond_to? counter_name
if version
parent.inc(counter_name.to_sym => -1) if parent.respond_to? counter_name
else
parent.inc(counter_name.to_sym, -1) if parent.respond_to? counter_name
end
else
relation = doc.send(name)
if relation && relation.class.fields.keys.include?(counter_name)
relation.inc(counter_name.to_sym => -1)
if version
relation.inc(counter_name.to_sym => -1)
else
relation.inc(counter_name.to_sym, -1)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Comment
belongs_to :post

field :remark
field :is_published, type: Mongoid::Boolean, default: false
field :is_published, type: Boolean, default: false

counter_cache :post, :if => Proc.new { |act| (act.is_published) }
end
2 changes: 1 addition & 1 deletion spec/models/review.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ class Review
counter_cache :article, :if => Proc.new { |act| (act.is_published) }

field :comment
field :is_published, type: Mongoid::Boolean, default: false
field :is_published, type: Boolean, default: false
end

0 comments on commit cdd49a9

Please sign in to comment.