Skip to content

Commit

Permalink
Fix resource deletion when Referer header is absent
Browse files Browse the repository at this point in the history
  • Loading branch information
spohlenz committed Oct 23, 2024
1 parent 6da5a05 commit c1a4127
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,12 @@ def update
end

def destroy
deleting_referer = URI(request.referer).path == admin.instance_path(instance)

if delete_instance
respond_to do |format|
flash[:message] = flash_message("destroy.success", title: "Success!", message: "The %{lowercase_model_name} was successfully deleted.")

format.html { redirect_to_return_location(:destroy, instance, status: :see_other) { admin.path(:index) } }
format.turbo_stream { flash.discard } unless deleting_referer
format.turbo_stream { flash.discard } unless referer_is_instance_path?
format.json { head :no_content }

yield format if block_given?
Expand All @@ -138,6 +136,11 @@ def destroy
end
end
end

private
def referer_is_instance_path?
request.referer && URI(request.referer).path == admin.instance_path(instance)
end
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/integration/resource_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

describe Trestle::ResourceController, type: :request do
include FeatureHelper

let(:resource) { PostsAdmin }

describe "delete record without referer" do
let(:post) { create_test_post }

it "does not raise an exception" do
delete resource.instance_path(post)
end
end
end

0 comments on commit c1a4127

Please sign in to comment.