lib/make_voteable/voter.rb in make_voteable-0.1.0 vs lib/make_voteable/voter.rb in make_voteable-0.1.1

- old
+ new

@@ -38,19 +38,24 @@ Voting.transaction do save 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+. # Raises an AlreadyVotedError if the voter already down voted the voteable. # Changes an up vote to a down vote if the the voter already up voted the voteable. @@ -77,19 +82,24 @@ Voting.transaction do save 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+. # Raises a NotVotedError if the voter didn't voted for the voteable. def unvote(voteable) @@ -110,19 +120,24 @@ Voting.transaction do save 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+. def voted?(voteable) check_voteable(voteable) @@ -132,17 +147,19 @@ # Returns true if the voter up voted the +voteable+. def up_voted?(voteable) check_voteable(voteable) voting = fetch_voting(voteable) + return false if voting.nil? return true if voting.has_attribute?(:up_vote) && voting.up_vote false end # Returns true if the voter down voted the +voteable+. def down_voted?(voteable) check_voteable(voteable) voting = fetch_voting(voteable) + return false if voting.nil? return true if voting.has_attribute?(:up_vote) && !voting.up_vote false end private