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

@@ -10,11 +10,10 @@ class CommentVoteTest < ActiveSupport::TestCase
context "during validation" do
subject { build(:comment_vote, comment: as(@user) { create(:comment) }) }
should validate_uniqueness_of(:user_id).scoped_to(:comment_id).with_message("have already voted for this comment")
should validate_inclusion_of(:score).in_array([-1, 1]).with_message("must be 1 or -1")
end
should "not allow creating duplicate votes" do
should "not allow creating duplicate active votes" do
v1 = create(:comment_vote, comment: @comment, user: @user)
v2 = build(:comment_vote, comment: @comment, user: @user)
@@ -23,6 +22,16 @@ class CommentVoteTest < ActiveSupport::TestCase
end
end
should "allow creating duplicate deleted votes" do
v1 = create(:comment_vote, comment: @comment, user: @user)
v2 = create(:comment_vote, comment: @comment, user: @user, is_deleted: true)
v3 = create(:comment_vote, comment: @comment, user: @user, is_deleted: true)
assert_equal(true, v1.valid?)
assert_equal(true, v2.valid?)
assert_equal(true, v3.valid?)
end
context "creating" do
context "an upvote" do
should "increment the comment's score" do
@@ -41,14 +50,15 @@ class CommentVoteTest < ActiveSupport::TestCase
end
end
context "destroying" do
context "soft deleting" do
context "an upvote" do
should "decrement the comment's score" do
vote = create(:comment_vote, comment: @comment, score: 1)
assert_equal(1, @comment.reload.score)
vote.destroy
vote.soft_delete(updater: vote.user)
assert_equal(0, @comment.reload.score)
assert_equal(true, vote.is_deleted?)
end
end
@@ -57,8 +67,9 @@ class CommentVoteTest < ActiveSupport::TestCase
vote = create(:comment_vote, comment: @comment, score: -1)
assert_equal(-1, @comment.reload.score)
vote.destroy
vote.soft_delete(updater: vote.user)
assert_equal(0, @comment.reload.score)
assert_equal(true, vote.is_deleted?)
end
end
end