Skip to content

Commit

Permalink
Add Voter.voted_up_on? and Voter.voted_down_on? methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejholmes committed Apr 26, 2012
1 parent bcb69de commit d0e1f97
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.markdown
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/acts_as_votable/voter.rb
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions spec/voter_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit d0e1f97

Please sign in to comment.