Skip to content

Commit

Permalink
Fixing .travis.yml merge conflict.
Browse files Browse the repository at this point in the history
  • Loading branch information
jah2488 committed Mar 3, 2014
2 parents 5c68d3d + a789e89 commit 8883997
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 17 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: ruby
rvm:
- 1.9.3
- 2.1.0
services: mongodb
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +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', '2.4.6'
#gem 'mongoid', '3.1.4'
65 changes: 49 additions & 16 deletions lib/mongoid/magic_counter_cache.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'mongoid'
require 'mongoid/version'
module Mongoid #:nodoc:

# The Counter Cache will yada yada
Expand Down Expand Up @@ -46,32 +47,64 @@ module ClassMethods
def counter_cache(*args, &block)
options = args.extract_options!
name = options[:class] || args.first.to_s

version = (Mongoid::VERSION.to_i >= 4) ? true : false

if options[:field]
counter_name = "#{options[:field].to_s}"
else
counter_name = "#{model_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]

after_create do |doc|
if doc.embedded?
parent = doc._parent
parent.inc(counter_name.to_sym, 1) if parent.respond_to? counter_name
else
relation = doc.send(name)
if relation && relation.class.fields.keys.include?(counter_name)
relation.inc(counter_name.to_sym, 1)
result = condition.nil? ? true : condition.call(doc)

if result
if doc.embedded?
parent = doc._parent
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)
if version
relation.inc(counter_name.to_sym => 1)
else
relation.inc(counter_name.to_sym, 1)
end
end
end
end
end

after_destroy do |doc|
if doc.embedded?
parent = doc._parent
parent.inc(counter_name.to_sym, -1) if parent.respond_to? counter_name
else
relation = doc.send(name)
if relation && relation.class.fields.keys.include?(counter_name)
relation.inc(counter_name.to_sym, -1)
result = condition.nil? ? true : condition.call(doc)

if result
if doc.embedded?
parent = doc._parent
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)
if version
relation.inc(counter_name.to_sym => -1)
else
relation.inc(counter_name.to_sym, -1)
end
end
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/models/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Article
include Mongoid::Document

embeds_many :reviews

field :title
field :review_count, type: Integer, default: 0

end
11 changes: 11 additions & 0 deletions spec/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Comment
include Mongoid::Document
include Mongoid::MagicCounterCache

belongs_to :post

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

counter_cache :post, :if => Proc.new { |act| (act.is_published) }
end
9 changes: 9 additions & 0 deletions spec/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Post
include Mongoid::Document

field :article
field :comment_count, :type => Integer, :default => 0

has_many :comments

end
10 changes: 10 additions & 0 deletions spec/models/review.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Review
include Mongoid::Document
include Mongoid::MagicCounterCache

embedded_in :article
counter_cache :article, :if => Proc.new { |act| (act.is_published) }

field :comment
field :is_published, type: Boolean, default: false
end
133 changes: 132 additions & 1 deletion spec/mongoid/magic_counter_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Mongoid

describe ".counter_cache" do

context "when the document is associated" do
context "when the document is associatedi without condition" do

before do
Library.delete_all
Expand Down Expand Up @@ -81,6 +81,7 @@ module Mongoid
book.foreign_publication_count.should == 1
end


context "when the referenced document has an embedded document" do

let(:page) do
Expand Down Expand Up @@ -119,6 +120,7 @@ module Mongoid
end

end

context "when the document is embedded" do

before do
Expand Down Expand Up @@ -205,6 +207,135 @@ module Mongoid
person.all_my_feels.should == person.feelings.size
end
end

context "when the document is associated with condition" do

before do
Post.delete_all
end

let(:post) do
Post.new
end

let(:comment) do
Comment.new(:is_published => true)
end

before do
post.save
post.comments.create(:remark => "I agree with you", :is_published => true)
end

it "sets the target of the relation" do
post.comments.first.remark.should == "I agree with you"
end

it "should have 1 comment for post" do
post.comments.size.should == 1
end

it "should have 1 in comment counter" do
post.comment_count.should == 1
end

it "sets the counter cache equal to the relation count on addition" do
5.times do |n|
post.comments << Comment.new(:is_published => true)
post.comment_count.should == post.comments.size
end
end

it "should increase counter when new books are added" do
post.comments.push( comment )
post.comments.size.should == 2
end

it "should increase counter when new books are added" do
post.comments.push( comment )
post.comments.size.should == post.comment_count
end

it "should decrease counter when published comment is deleted" do
post.comments.push( comment )
comment.destroy
post.comments.size.should == 1
end

it "should increase counter when new books are added" do
post.comments.push( comment )
comment.destroy
post.comments.size.should == post.comment_count
end

it "shouldnot increase counter when unpublished comment is added" do
post.comments << Comment.new
post.comments.size.should == post.comment_count + 1
end

it "shouldnot decrease counter when unpublished comment is deleted" do
post.comments << Comment.new(:remark => "2nd comment")
post.comments << Comment.new(:remark => "3rd comment", :is_published => true)
Comment.where(:remark == "2nd comment").first.destroy
post.comment_count.should == 2
end
end

context "when the document is embedded and has condition for counter" do

before do
Article.delete_all
end

let(:article) do
Article.new
end

let(:review) do
Review.new(:comment => "This is nice article")
end

before do
article.save
article.reviews.create(:comment => "This is very good article", :is_published => true)
end

it "should have 1 review in reviews" do
article.reviews.length.should == 1
end

it "should have correct comment" do
article.reviews.first.comment.should == "This is very good article"
end

it "should have 1 review in counter" do
article.review_count.should == 1
end

it "sets the counter cache equal to the relation count" do
article.reviews.length.should == article.review_count
end

it "sets the counter cache equal to the relation count on addition" do
5.times do |n|
article.reviews << Review.new(:is_published => true)
article.reviews.length.should == article.review_count
end
end

it "decreases the counter cache when records are deleted" do
article.reviews.all.destroy
article.reviews.length.should == article.review_count
end

it "counter should not get incremented if condition is not meet" do
5.times do |n|
article.reviews << Review.new
end
article.reviews.length.should == 6
article.review_count.should == 1
end
end
end

end
Expand Down

0 comments on commit 8883997

Please sign in to comment.