Skip to content

Commit

Permalink
Added specs for embedded document.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sameer-Tilak committed Feb 28, 2014
1 parent 606a41b commit 5dcb0a8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 17 deletions.
17 changes: 0 additions & 17 deletions lib/mongoid/magic_counter_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ def counter_cache(*args, &block)

after_create do |doc|
result = condition.nil? ? true : condition.call(doc)
#condition.each do |key, value|
# if doc.fields.keys.include?(key) and doc[key.to_sym] == value
# else
# result = false
# break
# end
#end
#end

if result
if doc.embedded?
Expand All @@ -81,15 +73,6 @@ def counter_cache(*args, &block)

after_destroy do |doc|
result = condition.nil? ? true : condition.call(doc)
#if condition
# condition.each do |key, value|
# if doc.fields.keys.include?(key) and doc[key.to_sym] == value
# else
# result = false
# break
# end
# end
#end

if result
if doc.embedded?
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
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: Mongoid::Boolean, default: false
end
56 changes: 56 additions & 0 deletions spec/mongoid/magic_counter_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,62 @@ module Mongoid
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 5dcb0a8

Please sign in to comment.