Permalink
Browse files

Update parent for ManyToMany relationship

  • Loading branch information...
1 parent 494198f commit 5e9a09582333ca8a95957555cc780422bc9d59bf @tiendung tiendung committed Apr 17, 2011
View
@@ -1,6 +1,7 @@
== 0.7.3
* Add :return_votee => true option to vote function to warranty always return voteable object
* Add Voteable.voted?, Voteable.up_voted?, Voteable.down_voted?
+ * Update parent for ManyToMany relationship
* Refactor
== 0.7.2
View
@@ -32,7 +32,7 @@ post.rb
# set points for each vote
voteable self, :up => +1, :down => -1
- references_many :comments
+ has_many :comments
end
comment.rb
@@ -43,7 +43,7 @@ comment.rb
include Mongoid::Document
include Mongoid::Voteable
- referenced_in :post
+ belongs_to :post
voteable self, :up => +1, :down => -3
@@ -149,15 +149,26 @@ def unvote_query_and_update(options)
def update_parent_votes(doc, options)
VOTEABLE[name].each do |class_name, voteable|
- # For other class in VOTEABLE options, if is parent of current class
- next unless relation_metadata = relations[class_name.underscore]
- # If can find current votee foreign_key value for that class
- next unless foreign_key_value = doc[relation_metadata.foreign_key.to_s]
+ # For other class in VOTEABLE options, if has relationship with current class
+ relation_metadata = relations.find{ |x, r| r.class_name == class_name }.try(:last)
+ next unless relation_metadata.present?
- class_name.constantize.collection.update(
- { '_id' => foreign_key_value },
- { '$inc' => parent_inc_options(voteable, options) }
- )
+ # If cannot find current votee foreign_key value for that class
+ foreign_key_value = doc[relation_metadata.foreign_key.to_s]
+ next unless foreign_key_value.present?
+
+ if relation_metadata.relation == Mongoid::Relations::Referenced::In
+ class_name.constantize.collection.update(
+ { '_id' => foreign_key_value },
+ { '$inc' => parent_inc_options(voteable, options) }
+ )
+ elsif relation_metadata.relation == Mongoid::Relations::Referenced::ManyToMany
+ class_name.constantize.collection.update(
+ { '_id' => { '$in' => foreign_key_value } },
+ { '$inc' => parent_inc_options(voteable, options) },
+ { :multi => true }
+ )
+ end
end
end
View
@@ -0,0 +1,8 @@
+class Category
+ include Mongoid::Document
+ include Mongoid::Voteable
+
+ field :name
+
+ has_and_belongs_to_many :posts
+end
View
@@ -1,11 +1,13 @@
require 'post'
class Comment
- include Mongoid::Document
include Mongoid::Voteable
-
- referenced_in :post
+ include Mongoid::Document
+
+ field :content
+
+ belongs_to :post
voteable self, :up => +1, :down => -3
- voteable Post, :up => +2, :down => -1 #, :not_update_counters => true
+ voteable Post, :up => +2, :down => -1
end
View
@@ -2,7 +2,12 @@ class Post
include Mongoid::Document
include Mongoid::Voteable
- voteable self, :up => +1, :down => -1
+ field :title
+ field :content
- references_many :comments
+ has_and_belongs_to_many :categories
+ has_many :comments
+
+ voteable self, :up => +1, :down => -1
+ voteable Category, :up => +3, :down => -5, :update_counters => false
end
View
@@ -29,3 +29,4 @@
User.collection.drop
Post.collection.drop
Comment.collection.drop
+Category.collection.drop
@@ -2,9 +2,15 @@
describe Mongoid::Voteable do
before :all do
+ @category1 = Category.create!(:name => 'xyz')
+ @category2 = Category.create!(:name => 'abc')
+
@post1 = Post.create!
@post2 = Post.create!
+ @post1.categories << @category1
+ @category2.posts << @post1
+
@comment = @post2.comments.create!
@user1 = User.create!
@@ -13,6 +19,16 @@
context "just created" do
it 'votes_count, up_votes_count, down_votes_count, votes_point should be zero' do
+ @category1.up_votes_count.should == 0
+ @category1.down_votes_count.should == 0
+ @category1.votes_count.should == 0
+ @category1.votes_point.should == 0
+
+ @category2.up_votes_count.should == 0
+ @category2.down_votes_count.should == 0
+ @category2.votes_count.should == 0
+ @category2.votes_point.should == 0
+
@post1.up_votes_count.should == 0
@post1.down_votes_count.should == 0
@post1.votes_count.should == 0
@@ -30,6 +46,12 @@
end
it 'up_voter_ids, down_voter_ids should be empty' do
+ @category1.up_voter_ids.should be_empty
+ @category1.down_voter_ids.should be_empty
+
+ @category2.up_voter_ids.should be_empty
+ @category2.down_voter_ids.should be_empty
+
@post1.up_voter_ids.should be_empty
@post1.down_voter_ids.should be_empty
@@ -41,6 +63,9 @@
end
it 'voted by voter should be empty' do
+ Category.voted_by(@user1).should be_empty
+ Category.voted_by(@user2).should be_empty
+
Post.voted_by(@user1).should be_empty
Post.voted_by(@user2).should be_empty
@@ -100,6 +125,18 @@
Post.voted_by(@user1).to_a.should == [ @post1 ]
Post.voted_by(@user2).to_a.should be_empty
+
+ @category1.reload
+ @category1.up_votes_count.should == 0
+ @category1.down_votes_count.should == 0
+ @category1.votes_count.should == 0
+ @category1.votes_point.should == 3
+
+ @category2.reload
+ @category2.up_votes_count.should == 0
+ @category2.down_votes_count.should == 0
+ @category2.votes_count.should == 0
+ @category2.votes_point.should == 3
end
it 'user1 vote post1 has no effect' do
@@ -140,6 +177,20 @@
Post.voted_by(@user1).to_a.should == [ @post1 ]
Post.voted_by(@user2).to_a.should == [ @post1 ]
end
+
+ it 'categories votes' do
+ @category1.reload
+ @category1.up_votes_count.should == 0
+ @category1.down_votes_count.should == 0
+ @category1.votes_count.should == 0
+ @category1.votes_point.should == -2
+
+ @category2.reload
+ @category2.up_votes_count.should == 0
+ @category2.down_votes_count.should == 0
+ @category2.votes_count.should == 0
+ @category2.votes_point.should == -2
+ end
end
context 'user1 change vote on post1 from up to down' do

0 comments on commit 5e9a095

Please sign in to comment.