Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add Voter.voted_up_on? and Voter.voted_down_on? methods. #9

Merged
merged 1 commit into from

2 participants

@ejholmes

This adds two more instance methods to Voter; .voted_up_on? and .voted_down_on?, which should be pretty self explanatory.

Example

@user.likes @comment1
@user.dislikes @comment2
# user has not voted on @comment3

@user.voted_up_on? @comment1 # => true
@user.voted_down_on? @comment1 # => false

@user.voted_down_on? @comment2 # => true
@user.voted_up_on? @comment2 # => false

@user.voted_up_on? @comment3 # => false
@user.voted_down_on? @comment3 # => false
@ryanto ryanto merged commit 9d9cb40 into from
@ryanto
Owner

Thanks! Looks great.

I am going to bump the version and push to Rubygems.

@ryanto
Owner

Pushed to rubygems as 0.3.0

gem "acts_as_votable", "~> 0.3.0"
@ejholmes

Much appreciated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
15 README.markdown
@@ -141,6 +141,21 @@ check how the voter voted by using ``voted_as_when_voted_for``.
@user.voted_as_when_voted_for @comment2 # => false, he didnt like it
@user.voted_as_when_voted_for @comment3 # => nil, he has yet to vote
+You can also check whether the voter has voted up or down.
+
+ @user.likes @comment1
+ @user.dislikes @comment2
+ # user has not voted on @comment3
+
+ @user.voted_up_on? @comment1 # => true
+ @user.voted_down_on? @comment1 # => false
+
+ @user.voted_down_on? @comment2 # => true
+ @user.voted_up_on? @comment2 # => false
+
+ @user.voted_up_on? @comment3 # => false
+ @user.voted_down_on? @comment3 # => false
+
### Registered Votes
Voters can only vote once per model. In this example the 2nd vote does not count
View
12 lib/acts_as_votable/voter.rb
@@ -53,6 +53,18 @@ def voted_on? votable
end
alias :voted_for? :voted_on?
+ def voted_up_on? votable
+ votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.name, :vote_flag => true)
+ votes.size > 0
+ end
+ alias :voted_up_for? :voted_up_on?
+
+ def voted_down_on? votable
+ votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.name, :vote_flag => false)
+ votes.size > 0
+ end
+ alias :voted_down_for? :voted_down_on?
+
def voted_as_when_voting_on votable
votes = find_votes(:votable_id => votable.id, :votable_type => votable.class.name)
return nil if votes.size == 0
View
20 spec/voter_spec.rb
@@ -55,6 +55,26 @@
@voter.voted_as_when_voting_on(@votable).should be nil
end
+ it "should return true if voter has voted true" do
+ @votable.vote :voter => @voter
+ @voter.voted_up_on?(@votable).should be true
+ end
+
+ it "should return false if voter has not voted true" do
+ @votable.vote :voter => @voter, :vote => false
+ @voter.voted_up_on?(@votable).should be false
+ end
+
+ it "should return true if the voter has voted false" do
+ @votable.vote :voter => @voter, :vote => false
+ @voter.voted_down_on?(@votable).should be true
+ end
+
+ it "should return false if the voter has not voted false" do
+ @votable.vote :voter => @voter, :vote => true
+ @voter.voted_down_on?(@votable).should be false
+ end
+
it "should provide reserve functionality, voter can vote on votable" do
@voter.vote :votable => @votable, :vote => 'bad'
@voter.voted_as_when_voting_on(@votable).should be false
Something went wrong with that request. Please try again.