Module: MakeVoteable::Voter
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/make_voteable/voter.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#down_vote(voteable) ⇒ Object
Down vote a
voteable
. -
#down_vote!(voteable) ⇒ Object
Down votes the
voteable
, but doesn’t raise an error if the votelable was already down voted. -
#down_voted?(voteable) ⇒ Boolean
Returns true if the voter down voted the
voteable
. -
#unvote(voteable) ⇒ Object
Clears an already done vote on a
voteable
. -
#unvote!(voteable) ⇒ Object
Clears an already done vote on a
voteable
, but doesn’t raise an error if the voteable was not voted. -
#up_vote(voteable) ⇒ Object
Up vote a
voteable
. -
#up_vote!(voteable) ⇒ Object
Up votes the
voteable
, but doesn’t raise an error if the votelable was already up voted. -
#up_voted?(voteable) ⇒ Boolean
Returns true if the voter up voted the
voteable
. -
#voted?(voteable) ⇒ Boolean
Returns true if the voter voted for the
voteable
.
Instance Method Details
#down_vote(voteable) ⇒ Object
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.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/make_voteable/voter.rb', line 62 def down_vote(voteable) check_voteable(voteable) voting = fetch_voting(voteable) if voting unless voting.up_vote raise Exceptions::AlreadyVotedError.new(false) else voting.up_vote = false voteable.up_votes -= 1 self.up_votes -= 1 if has_attribute?(:up_votes) end else voting = Voting.create(:voteable => voteable, :voter => self, :up_vote => false) end voteable.down_votes += 1 self.down_votes += 1 if has_attribute?(:down_votes) Voting.transaction do save voteable.save voting.save end true end |
#down_vote!(voteable) ⇒ Object
Down votes the voteable
, but doesn’t raise an error if the votelable was already down voted. The vote is simply ignored then.
93 94 95 96 97 98 99 100 101 |
# File 'lib/make_voteable/voter.rb', line 93 def down_vote!(voteable) begin down_vote(voteable) success = true rescue Exceptions::AlreadyVotedError success = false end success end |
#down_voted?(voteable) ⇒ Boolean
Returns true if the voter down voted the voteable
.
158 159 160 161 162 163 164 |
# File 'lib/make_voteable/voter.rb', line 158 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 |
#unvote(voteable) ⇒ Object
Clears an already done vote on a voteable
. Raises a NotVotedError if the voter didn’t voted for the voteable.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/make_voteable/voter.rb', line 105 def unvote(voteable) check_voteable(voteable) voting = fetch_voting(voteable) raise Exceptions::NotVotedError unless voting if voting.up_vote voteable.up_votes -= 1 self.up_votes -= 1 if has_attribute?(:up_votes) else voteable.down_votes -= 1 self.down_votes -= 1 if has_attribute?(:down_votes) end Voting.transaction do save voteable.save voting.destroy end true end |
#unvote!(voteable) ⇒ Object
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.
131 132 133 134 135 136 137 138 139 |
# File 'lib/make_voteable/voter.rb', line 131 def unvote!(voteable) begin unvote(voteable) success = true rescue Exceptions::NotVotedError success = false end success end |
#up_vote(voteable) ⇒ Object
Up vote a voteable
. Raises an AlreadyVotedError if the voter already up voted the voteable. Changes a down vote to an up vote if the the voter already down voted the voteable.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/make_voteable/voter.rb', line 18 def up_vote(voteable) check_voteable(voteable) voting = fetch_voting(voteable) if voting if voting.up_vote raise Exceptions::AlreadyVotedError.new(true) else voting.up_vote = true voteable.down_votes -= 1 self.down_votes -= 1 if has_attribute?(:down_votes) end else voting = Voting.create(:voteable => voteable, :voter => self, :up_vote => true) end voteable.up_votes += 1 self.up_votes += 1 if has_attribute?(:up_votes) Voting.transaction do save voteable.save voting.save end true end |
#up_vote!(voteable) ⇒ Object
Up votes the voteable
, but doesn’t raise an error if the votelable was already up voted. The vote is simply ignored then.
49 50 51 52 53 54 55 56 57 |
# File 'lib/make_voteable/voter.rb', line 49 def up_vote!(voteable) begin up_vote(voteable) success = true rescue Exceptions::AlreadyVotedError success = false end success end |
#up_voted?(voteable) ⇒ Boolean
Returns true if the voter up voted the voteable
.
149 150 151 152 153 154 155 |
# File 'lib/make_voteable/voter.rb', line 149 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 |
#voted?(voteable) ⇒ Boolean
Returns true if the voter voted for the voteable
.
142 143 144 145 146 |
# File 'lib/make_voteable/voter.rb', line 142 def voted?(voteable) check_voteable(voteable) voting = fetch_voting(voteable) !voting.nil? end |