Skip to content

Commit

Permalink
Fix Comments controller destroy declaration (#6482)
Browse files Browse the repository at this point in the history
* Fix the comments controller methods

The destroy was defined in the create method 🤪

* Add tests for full coverage

---------

Co-authored-by: Javier Julio <jjfutbol@gmail.com>
  • Loading branch information
bliof and javierjulio committed May 11, 2023
1 parent b42c7f3 commit fc6fd8b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/active_admin/orm/active_record/comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ def create
redirect_back fallback_location: active_admin_root
end
end
end

def destroy
destroy! do |success, failure|
success.html do
redirect_back fallback_location: active_admin_root
end
failure.html do
redirect_back fallback_location: active_admin_root
end
def destroy
destroy! do |success, failure|
success.html do
redirect_back fallback_location: active_admin_root
end
failure.html do
redirect_back fallback_location: active_admin_root
end
end
end
Expand Down
53 changes: 53 additions & 0 deletions spec/unit/resource/comments_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true
require "rails_helper"

RSpec.describe "ActiveAdmin Comments", type: :controller do
before do
load_resources { ActiveAdmin.register ActiveAdmin::Comment, as: "Comment" }
@controller = Admin::CommentsController.new
end

describe "#destroy" do
let(:user) { User.create! }
let(:comment) { ActiveAdmin::Comment.create!(body: "body", namespace: :admin, resource: user, author: user) }

context "success" do
it "deletes comments and redirects to root fallback" do
delete :destroy, params: { id: comment.id }

expect(response).to redirect_to admin_root_url
end

it "deletes comments and redirects back" do
request.env["HTTP_REFERER"] = "/admin/users/1"

delete :destroy, params: { id: comment.id }

expect(response).to redirect_to "/admin/users/1"
end
end

context "failure" do
it "does not delete comment on error and redirects to root fallback" do
expect(@controller).to receive(:destroy_resource) do |comment|
comment.errors.add(:body, :invalid)
end

delete :destroy, params: { id: comment.id }

expect(response).to redirect_to admin_root_url
end

it "does not delete comment on error and redirects back" do
request.env["HTTP_REFERER"] = "/admin/users/1"
expect(@controller).to receive(:destroy_resource) do |comment|
comment.errors.add(:body, :invalid)
end

delete :destroy, params: { id: comment.id }

expect(response).to redirect_to "/admin/users/1"
end
end
end
end

0 comments on commit fc6fd8b

Please sign in to comment.