Module: VoteFu::Concerns::Voter::InstanceMethods

Defined in:
lib/vote_fu/concerns/voter.rb

Instance Method Summary collapse

Instance Method Details

#downvote(voteable, scope: nil) ⇒ VoteFu::Vote

Downvote a voteable (-1)

Parameters:

  • voteable (ActiveRecord::Base)

    The item to vote on

  • scope (Symbol, nil) (defaults to: nil)

    Optional voting scope

Returns:



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

Parameters:

  • voteable (ActiveRecord::Base)

    The item to toggle

  • scope (Symbol, nil) (defaults to: nil)

    Optional voting scope

Returns:



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

Parameters:

  • voteable (ActiveRecord::Base)

    The item to unvote

  • scope (Symbol, nil) (defaults to: nil)

    Optional voting scope

Returns:



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)

Parameters:

  • voteable (ActiveRecord::Base)

    The item to vote on

  • scope (Symbol, nil) (defaults to: nil)

    Optional voting scope

Returns:



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

Parameters:

  • direction (Symbol, nil) (defaults to: nil)

    :up, :down, or nil for all

Returns:

  • (Integer)


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

Parameters:

  • voteable (ActiveRecord::Base)

    The item

  • scope (Symbol, nil) (defaults to: nil)

    Optional voting scope

Returns:

  • (Symbol, nil)

    :up, :down, :neutral, or nil



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

Parameters:

  • voteable (ActiveRecord::Base)

    The item to vote on

  • value (Integer)

    Vote value (positive for up, negative for down)

  • scope (Symbol, nil) (defaults to: nil)

    Optional voting scope

Returns:

Raises:



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

Parameters:

  • voteable (ActiveRecord::Base)

    The item

  • scope (Symbol, nil) (defaults to: nil)

    Optional voting scope

Returns:

  • (Integer, nil)


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

Parameters:

  • klass (Class)

    The voteable class

  • scope (Symbol, nil) (defaults to: nil)

    Optional voting scope

Returns:

  • (ActiveRecord::Relation)


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

Parameters:

  • voteable (ActiveRecord::Base)

    The item to check

  • direction (Symbol, Integer, nil) (defaults to: nil)

    :up, :down, or specific value

  • scope (Symbol, nil) (defaults to: nil)

    Optional voting scope

Returns:

  • (Boolean)


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