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:
evazion
2021-01-21 01:02:22 -06:00
parent c31f2003d9
commit 9efb374ae5
12 changed files with 181 additions and 133 deletions

View File

@@ -1,13 +1,15 @@
class CommentVote < ApplicationRecord
class Error < StandardError; end
belongs_to :comment
belongs_to :user
validates_presence_of :score
validates_uniqueness_of :user_id, :scope => :comment_id, :message => "have already voted for this comment"
validate :validate_comment_can_be_down_voted
validates_inclusion_of :score, :in => [-1, 1], :message => "must be 1 or -1"
after_create :update_score_after_create
after_destroy :update_score_after_destroy
def self.visible(user)
if user.is_moderator?
all
@@ -37,6 +39,18 @@ class CommentVote < ApplicationRecord
score == -1
end
def update_score_after_create
comment.with_lock do
comment.update_columns(score: comment.score + score)
end
end
def update_score_after_destroy
comment.with_lock do
comment.update_columns(score: comment.score - score)
end
end
def self.available_includes
[:comment, :user]
end