Allow deleting comments if article has already been deleted (#5889) [deploy]

This commit is contained in:
Michael Kohl 2020-02-06 05:10:19 +07:00 committed by GitHub
parent 22c643669a
commit 52759c9427
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View file

@ -137,14 +137,16 @@ class CommentsController < ApplicationController
# DELETE /comments/1.json
def destroy
authorize @comment
@commentable_path = @comment.commentable.path
if @comment.is_childless?
@comment.destroy
else
@comment.deleted = true
@comment.save!
end
redirect_to URI.parse(@commentable_path).path, notice: "Comment was successfully deleted."
redirect = @comment.commentable&.path || user_path(current_user)
# NOTE: Brakeman doesn't like redirecting to a path, because of a "possible
# unprotected redirect". Using URI.parse().path is the recommended workaround.
redirect_to URI.parse(redirect).path, notice: "Comment was successfully deleted."
end
def delete_confirm

View file

@ -6,7 +6,7 @@ RSpec.describe "Comments", type: :request do
let(:article) { create(:article, user_id: user.id) }
let(:podcast) { create(:podcast) }
let(:podcast_episode) { create(:podcast_episode, podcast_id: podcast.id) }
let(:comment) do
let!(:comment) do
create(:comment,
commentable_id: article.id,
commentable_type: "Article",
@ -270,4 +270,26 @@ RSpec.describe "Comments", type: :request do
describe "PATCH /comments/:comment_id/unhide" do
include_examples "PATCH /comments/:comment_id/hide or unhide", path: "unhide", hidden: "false"
end
describe "DELETE /comments/:comment_id" do
before { sign_in user }
it "deletes a comment if the article is still present" do
delete "/comments/#{comment.id}"
expect(Comment.find_by(id: comment.id)).to be_nil
expect(response).to redirect_to(comment.commentable.path)
expect(flash[:notice]).to eq("Comment was successfully deleted.")
end
it "deletes a comment if the article has been deleted" do
article.destroy!
delete "/comments/#{comment.id}"
expect(Comment.find_by(id: comment.id)).to be_nil
expect(response).to redirect_to(user_path(user))
expect(flash[:notice]).to eq("Comment was successfully deleted.")
end
end
end