Skip to content

Commit

Permalink
Return true on success and false for failure (in non bang method).
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai Schlamp committed Jun 16, 2011
1 parent 12a7d6a commit baae427
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
15 changes: 15 additions & 0 deletions lib/make_voteable/voter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,20 @@ def up_vote(voteable)
voteable.save
voting.save
end

true
end

# Up votes the +voteable+, but doesn't raise an error if the votelable was already up voted.
# The vote is simply ignored then.
def up_vote!(voteable)
begin
up_vote(voteable)
success = true
rescue Exceptions::AlreadyVotedError
success = false
end
success
end

# Down vote a +voteable+.
Expand Down Expand Up @@ -79,15 +84,20 @@ def down_vote(voteable)
voteable.save
voting.save
end

true
end

# Down votes the +voteable+, but doesn't raise an error if the votelable was already down voted.
# The vote is simply ignored then.
def down_vote!(voteable)
begin
down_vote(voteable)
success = true
rescue Exceptions::AlreadyVotedError
success = false
end
success
end

# Clears an already done vote on a +voteable+.
Expand All @@ -112,15 +122,20 @@ def unvote(voteable)
voteable.save
voting.destroy
end

true
end

# Clears an already done vote on a +voteable+, but doesn't raise an error if
# the voteable was not voted. It ignores the unvote then.
def unvote!(voteable)
begin
unvote(voteable)
success = true
rescue Exceptions::NotVotedError
success = false
end
success
end

# Returns true if the voter voted for the +voteable+.
Expand Down
18 changes: 12 additions & 6 deletions spec/lib/make_voteable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
end

it "should get correct vote summary" do
@voter.up_vote(@voteable)
@voter.up_vote(@voteable).should == true
@voteable.votes.should == 1
@voter.down_vote(@voteable)
@voter.down_vote(@voteable).should == true
@voteable.votes.should == -1
@voter.unvote(@voteable)
@voter.unvote(@voteable).should == true
@voteable.votes.should == 0
end

Expand Down Expand Up @@ -83,7 +83,9 @@

it "should only allow a voter to up vote a voteable once without raising an error" do
@voter.up_vote!(@voteable)
lambda { @voter.up_vote!(@voteable) }.should_not raise_error(MakeVoteable::Exceptions::AlreadyVotedError)
lambda {
@voter.up_vote!(@voteable).should == false
}.should_not raise_error(MakeVoteable::Exceptions::AlreadyVotedError)
MakeVoteable::Voting.count.should == 1
end

Expand Down Expand Up @@ -155,7 +157,9 @@

it "should only allow a voter to down vote a voteable once without raising an error" do
@voter.down_vote!(@voteable)
lambda { @voter.down_vote!(@voteable) }.should_not raise_error(MakeVoteable::Exceptions::AlreadyVotedError)
lambda {
@voter.down_vote!(@voteable).should == false
}.should_not raise_error(MakeVoteable::Exceptions::AlreadyVotedError)
MakeVoteable::Voting.count.should == 1
end

Expand Down Expand Up @@ -219,7 +223,9 @@
end

it "should not raise error if voter didn't vote for the voteable and unvote! is called" do
lambda { @voter.unvote!(@voteable) }.should_not raise_error(MakeVoteable::Exceptions::NotVotedError)
lambda {
@voter.unvote!(@voteable).should == false
}.should_not raise_error(MakeVoteable::Exceptions::NotVotedError)
end

it "should raise an error for an invalid voteable" do
Expand Down

0 comments on commit baae427

Please sign in to comment.