Permalink
Browse files

Adds scopes and a votable/voter trigger for scoped association calls

  • Loading branch information...
1 parent 98094a6 commit 741c9a62c3b4890aaaed06ee941adf57a8fb3807 @hassox committed Apr 11, 2012
Showing with 46 additions and 13 deletions.
  1. +17 −0 README.markdown
  2. +9 −5 lib/acts_as_votable/votable.rb
  3. +5 −0 lib/acts_as_votable/vote.rb
  4. +6 −8 lib/acts_as_votable/voter.rb
  5. +9 −0 spec/voter_spec.rb
View
17 README.markdown
@@ -85,6 +85,23 @@ Revisiting the previous example of code.
@post.dislikes.size # => 2
@post.downvotes.size # => 2
+Active Record scopes are provided to make life easier.
+
+ @post.votes.up.by_type(User)
+ @post.votes.down
+ @user1.votes.up
+ @user1.votes.down
+ @user1.votes.up.by_type(Post)
+
+Once scoping is complete, you can also trigger a get for the
+voter/votable
+
+ @post.votes.up.by_type(User).voters
+ @post.votes.down.by_type(User).voters
+
+ @user.votes.up.for_type(Post).votables
+ @user.votes.up.votables
+
You can also 'unvote' a model to remove a previous vote.
@post.liked_by @user1
View
14 lib/acts_as_votable/votable.rb
@@ -35,6 +35,11 @@ def self.included base
base.class_eval do
belongs_to :votable, :polymorphic => true
+ has_many :votes, :class_name => "ActsAsVotable::Vote", :as => :votable do
+ def voters
+ includes(:voter).map(&:voter)
+ end
+ end
aliases.each do |method, links|
links.each do |new_method|
@@ -72,20 +77,20 @@ def vote args = {}
end
# find the vote
- votes = find_votes({
+ _votes_ = find_votes({
:voter_id => options[:voter].id,
:voter_type => options[:voter].class.name
})
- if votes.count == 0
+ if _votes_.count == 0
# this voter has never voted
vote = ActsAsVotable::Vote.new(
:votable => self,
:voter => options[:voter]
)
else
# this voter is potentially changing his vote
- vote = votes.first
+ vote = _votes_.first
end
last_update = vote.updated_at
@@ -146,9 +151,8 @@ def update_cached_votes
# results
def find_votes extra_conditions = {}
- ActsAsVotable::Vote.find(:all, :conditions => default_conditions.merge(extra_conditions))
+ votes.where(extra_conditions)
end
- alias :votes :find_votes
def up_votes
find_votes(:vote_flag => true)
View
5 lib/acts_as_votable/vote.rb
@@ -13,6 +13,11 @@ class Vote < ::ActiveRecord::Base
belongs_to :votable, :polymorphic => true
belongs_to :voter, :polymorphic => true
+ scope :up, where(:vote_flag => true)
+ scope :down, where(:vote_flag => false)
+ scope :for_type, lambda{ |klass| where(:votable_type => klass) }
+ scope :by_type, lambda{ |klass| where(:voter_type => klass) }
+
validates_presence_of :votable_id
validates_presence_of :voter_id
View
14 lib/acts_as_votable/voter.rb
@@ -13,7 +13,11 @@ def self.included(base)
base.class_eval do
belongs_to :voter, :polymorphic => true
- has_many :votes, :class_name => "ActsAsVotable::Vote"
+ has_many :votes, :class_name => "ActsAsVotable::Vote", :as => :voter do
+ def votables
+ includes(:votable).map(&:votable)
+ end
+ end
aliases.each do |method, links|
links.each do |new_method|
@@ -25,13 +29,6 @@ def self.included(base)
end
- def default_conditions
- {
- :voter_id => self.id,
- :voter_type => self.class.name
- }
- end
-
# voting
def vote args
args[:votable].vote args.merge({:voter => self})
@@ -86,5 +83,6 @@ def find_up_votes_for_class klass
def find_down_votes_for_class klass
find_votes_for_class klass, :vote_flag => false
end
+
end
end
View
9 spec/voter_spec.rb
@@ -63,50 +63,59 @@
it "should allow the voter to vote up a model" do
@voter.vote_up_for @votable
@votable.up_votes.first.voter.should == @voter
+ @votable.votes.up.first.voter.should == @voter
end
it "should allow the voter to vote down a model" do
@voter.vote_down_for @votable
@votable.down_votes.first.voter.should == @voter
+ @votable.votes.down.first.voter.should == @voter
end
it "should allow the voter to unvote a model" do
@voter.vote_up_for @votable
@voter.unvote_for @votable
@votable.find_votes.size.should == 0
+ @votable.votes.count.should == 0
end
it "should get all of the voters votes" do
@voter.vote_up_for @votable
@voter.find_votes.size.should == 1
+ @voter.votes.up.count.should == 1
end
it "should get all of the voters up votes" do
@voter.vote_up_for @votable
@voter.find_up_votes.size.should == 1
+ @voter.votes.up.count.should == 1
end
it "should get all of the voters down votes" do
@voter.vote_down_for @votable
@voter.find_down_votes.size.should == 1
+ @voter.votes.down.count.should == 1
end
it "should get all of the votes votes for a class" do
@votable.vote :voter => @voter
@votable2.vote :voter => @voter, :vote => false
@voter.find_votes_for_class(Votable).size.should == 2
+ @voter.votes.for_type(Votable).count.should == 2
end
it "should get all of the voters up votes for a class" do
@votable.vote :voter => @voter
@votable2.vote :voter => @voter, :vote => false
@voter.find_up_votes_for_class(Votable).size.should == 1
+ @voter.votes.up.for_type(Votable).count.should == 1
end
it "should get all of the voters down votes for a class" do
@votable.vote :voter => @voter
@votable2.vote :voter => @voter, :vote => false
@voter.find_down_votes_for_class(Votable).size.should == 1
+ @voter.votes.down.for_type(Votable).count.should == 1
end
it "should be contained to instances" do

0 comments on commit 741c9a6

Please sign in to comment.