Allow deleting comments if article has already been deleted (#5889) [deploy]
This commit is contained in:
parent
22c643669a
commit
52759c9427
2 changed files with 27 additions and 3 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue