Module: VoteFu::Concerns::Voter::InstanceMethods
- Defined in:
- lib/vote_fu/concerns/voter.rb
Instance Method Summary collapse
-
#downvote(voteable, scope: nil) ⇒ VoteFu::Vote
Downvote a voteable (-1).
-
#toggle_vote(voteable, scope: nil) ⇒ VoteFu::Vote?
Toggle vote: remove if exists, upvote if not.
-
#unvote(voteable, scope: nil) ⇒ VoteFu::Vote?
Remove a vote.
-
#upvote(voteable, scope: nil) ⇒ VoteFu::Vote
Upvote a voteable (+1).
-
#vote_count(direction = nil) ⇒ Integer
Count of votes cast.
-
#vote_direction_for(voteable, scope: nil) ⇒ Symbol?
Get the vote direction for an item.
-
#vote_on(voteable, value:, scope: nil) ⇒ VoteFu::Vote
Cast a vote on a voteable.
-
#vote_value_for(voteable, scope: nil) ⇒ Integer?
Get the vote value for an item.
-
#voted_items(klass, scope: nil) ⇒ ActiveRecord::Relation
Get all items of a class that this voter voted on.
-
#voted_on?(voteable, direction: nil, scope: nil) ⇒ Boolean
Check if voted on an item.
Instance Method Details
#downvote(voteable, scope: nil) ⇒ VoteFu::Vote
Downvote a voteable (-1)
161 162 163 |
# File 'lib/vote_fu/concerns/voter.rb', line 161 def downvote(voteable, scope: nil) vote_on(voteable, value: -1, scope: scope) end |
#toggle_vote(voteable, scope: nil) ⇒ VoteFu::Vote?
Toggle vote: remove if exists, upvote if not
179 180 181 182 183 184 185 186 187 |
# File 'lib/vote_fu/concerns/voter.rb', line 179 def toggle_vote(voteable, scope: nil) existing = find_vote_for(voteable, scope: scope) if existing existing.destroy nil else upvote(voteable, scope: scope) end end |
#unvote(voteable, scope: nil) ⇒ VoteFu::Vote?
Remove a vote
170 171 172 |
# File 'lib/vote_fu/concerns/voter.rb', line 170 def unvote(voteable, scope: nil) find_vote_for(voteable, scope: scope)&.destroy end |
#upvote(voteable, scope: nil) ⇒ VoteFu::Vote
Upvote a voteable (+1)
152 153 154 |
# File 'lib/vote_fu/concerns/voter.rb', line 152 def upvote(voteable, scope: nil) vote_on(voteable, value: 1, scope: scope) end |
#vote_count(direction = nil) ⇒ Integer
Count of votes cast
241 242 243 244 245 246 247 248 |
# File 'lib/vote_fu/concerns/voter.rb', line 241 def vote_count(direction = nil) votes = cast_votes case direction when :up, :positive then votes.up.count when :down, :negative then votes.down.count else votes.count end end |
#vote_direction_for(voteable, scope: nil) ⇒ Symbol?
Get the vote direction for an item
222 223 224 |
# File 'lib/vote_fu/concerns/voter.rb', line 222 def vote_direction_for(voteable, scope: nil) find_vote_for(voteable, scope: scope)&.direction end |
#vote_on(voteable, value:, scope: nil) ⇒ VoteFu::Vote
Cast a vote on a voteable
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/vote_fu/concerns/voter.rb', line 135 def vote_on(voteable, value:, scope: nil) validate_vote!(voteable, value) existing = find_vote_for(voteable, scope: scope) if existing handle_existing_vote(existing, value) else cast_votes.create!(voteable: voteable, value: value, scope: scope) end end |
#vote_value_for(voteable, scope: nil) ⇒ Integer?
Get the vote value for an item
213 214 215 |
# File 'lib/vote_fu/concerns/voter.rb', line 213 def vote_value_for(voteable, scope: nil) find_vote_for(voteable, scope: scope)&.value end |
#voted_items(klass, scope: nil) ⇒ ActiveRecord::Relation
Get all items of a class that this voter voted on
231 232 233 234 235 |
# File 'lib/vote_fu/concerns/voter.rb', line 231 def voted_items(klass, scope: nil) klass.joins(:received_votes) .where(vote_fu_votes: { voter: self, scope: scope }) .distinct end |
#voted_on?(voteable, direction: nil, scope: nil) ⇒ Boolean
Check if voted on an item
195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/vote_fu/concerns/voter.rb', line 195 def voted_on?(voteable, direction: nil, scope: nil) vote = find_vote_for(voteable, scope: scope) return false unless vote case direction when nil then true when :up, :positive then vote.up? when :down, :negative then vote.down? when Integer then vote.value == direction else false end end |