comments: allow votes to be soft deleted.

Make it so that when a user removes their own vote, the vote is soft
deleted (the is_deleted flag is set) instead of hard deleted.

Changes:

* Add is_deleted flag to comment votes.
* Relax uniqueness constraint so you can have multiple deleted votes on
  the same comment. You can still only have one active vote on the comment.
* Add `soft_delete` method to Deletable concern.
This commit is contained in:
evazion
2021-03-29 19:40:37 -05:00
parent 55129b1819
commit 6b91e55283
11 changed files with 125 additions and 23 deletions

View File

@@ -13,7 +13,11 @@ class CommentVotesController < ApplicationController
@comment.with_lock do
@comment_vote = authorize CommentVote.new(comment: @comment, score: params[:score], user: CurrentUser.user)
CommentVote.where(comment: @comment, user: CurrentUser.user).destroy_all
CommentVote.active.where(comment: @comment, user: CurrentUser.user).each do |vote|
vote.soft_delete!(updater: CurrentUser.user)
end
@comment_vote.save
end
@@ -22,8 +26,9 @@ class CommentVotesController < ApplicationController
end
def destroy
@comment_vote = authorize CommentVote.find_by!(comment_id: params[:comment_id], user: CurrentUser.user)
@comment_vote.destroy
# XXX should find by comment vote id.
@comment_vote = authorize CommentVote.active.find_by!(comment_id: params[:comment_id], user: CurrentUser.user)
@comment_vote.soft_delete(updater: CurrentUser.user)
respond_with(@comment_vote)
end