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:
@@ -92,5 +92,16 @@ class CommentComponentTest < ViewComponent::TestCase
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "for a comment with a deleted vote" do
|
||||
should "not treat the vote as active" do
|
||||
@user = create(:user)
|
||||
@vote = create(:comment_vote, user: @user, comment: @comment, is_deleted: true, score: 1)
|
||||
render_comment(@comment, current_user: @user)
|
||||
|
||||
assert_css("article.comment[data-is-upvoted=false]")
|
||||
assert_css("article.comment .comment-upvote-link.inactive-link")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,5 +3,6 @@ FactoryBot.define do
|
||||
comment
|
||||
user
|
||||
score {1}
|
||||
is_deleted { false }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -91,24 +91,28 @@ class CommentVotesControllerTest < ActionDispatch::IntegrationTest
|
||||
vote = create(:comment_vote, comment: @comment, user: @user, score: 1)
|
||||
assert_equal(1, vote.comment.reload.score)
|
||||
|
||||
assert_difference("CommentVote.count", 0) do
|
||||
assert_difference("CommentVote.count", 1) do
|
||||
post_auth comment_comment_votes_path(comment_id: @comment.id, score: "1"), @user, xhr: true
|
||||
end
|
||||
|
||||
assert_response :success
|
||||
assert_equal(1, @comment.reload.score)
|
||||
assert_equal(1, @comment.votes.active.count)
|
||||
assert_equal(1, @comment.votes.deleted.count)
|
||||
end
|
||||
|
||||
should "automatically undo existing votes" do
|
||||
create(:comment_vote, comment: @comment, user: @user, score: -1)
|
||||
assert_equal(-1, @comment.reload.score)
|
||||
|
||||
assert_difference("CommentVote.count", 0) do
|
||||
assert_difference("CommentVote.count", 1) do
|
||||
post_auth comment_comment_votes_path(comment_id: @comment.id, score: "1"), @user, xhr: true
|
||||
end
|
||||
|
||||
assert_response :success
|
||||
assert_equal(1, @comment.reload.score)
|
||||
assert_equal(1, @comment.votes.active.count)
|
||||
assert_equal(1, @comment.votes.deleted.count)
|
||||
end
|
||||
|
||||
should "not allow voting on deleted comments" do
|
||||
@@ -138,9 +142,10 @@ class CommentVotesControllerTest < ActionDispatch::IntegrationTest
|
||||
should "allow users to remove their own comment votes" do
|
||||
@vote = create(:comment_vote, user: @user)
|
||||
|
||||
assert_difference("CommentVote.count", -1) do
|
||||
assert_difference("CommentVote.count", 0) do
|
||||
delete_auth comment_comment_votes_path(@vote.comment), @user, xhr: true
|
||||
assert_response :success
|
||||
assert_equal(true, @vote.reload.is_deleted?)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -150,6 +155,22 @@ class CommentVotesControllerTest < ActionDispatch::IntegrationTest
|
||||
assert_difference("CommentVote.count", 0) do
|
||||
delete_auth comment_comment_votes_path(@vote.comment), @user, xhr: true
|
||||
assert_response 404
|
||||
assert_equal(false, @vote.reload.is_deleted?)
|
||||
end
|
||||
end
|
||||
|
||||
context "deleting a vote on a comment that already has deleted votes" do
|
||||
setup do
|
||||
create(:comment_vote, comment: @comment, user: @user, score: 1, is_deleted: true)
|
||||
create(:comment_vote, comment: @comment, user: @user, score: -1, is_deleted: true)
|
||||
end
|
||||
|
||||
should "delete the current active vote" do
|
||||
@vote = create(:comment_vote, comment: @comment, user: @user)
|
||||
delete_auth comment_comment_votes_path(@vote.comment), @user, xhr: true
|
||||
|
||||
assert_response :success
|
||||
assert_equal(true, @vote.reload.is_deleted?)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user