comments: allow swapping votes.
Allow users to upvote a comment, then downvote it, without raising an error or having to manually remove the upvote first. The upvote is automatically removed and replaced by the downvote. Changes to the /comment_votes API: * `POST /comment_votes` and `DELETE /comment_votes` now return a comment vote instead of a comment. * The `score` param in `POST /comment_votes` is now 1 or -1, not `up` or `down.`
This commit is contained in:
@@ -44,39 +44,7 @@ class Comment < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
module VoteMethods
|
||||
def vote!(val, voter = CurrentUser.user)
|
||||
numerical_score = (val == "up") ? 1 : -1
|
||||
vote = votes.create!(user: voter, score: numerical_score)
|
||||
|
||||
if vote.is_positive?
|
||||
update_column(:score, score + 1)
|
||||
elsif vote.is_negative?
|
||||
update_column(:score, score - 1)
|
||||
end
|
||||
|
||||
return vote
|
||||
end
|
||||
|
||||
def unvote!
|
||||
vote = votes.where("user_id = ?", CurrentUser.user.id).first
|
||||
|
||||
if vote
|
||||
if vote.is_positive?
|
||||
update_column(:score, score - 1)
|
||||
else
|
||||
update_column(:score, score + 1)
|
||||
end
|
||||
|
||||
vote.destroy
|
||||
else
|
||||
raise CommentVote::Error.new("You have not voted for this comment")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
include VoteMethods
|
||||
|
||||
def validate_creator_is_not_limited
|
||||
if creator.is_comment_limited? && !do_not_bump_post?
|
||||
|
||||
Reference in New Issue
Block a user