From d18c9e98f9165b2c2b6c9a9bb9a5e4748eb03bd7 Mon Sep 17 00:00:00 2001 From: Sam Pohlenz Date: Wed, 14 Aug 2024 15:44:31 +0930 Subject: [PATCH] Set default target turbo_frame within admin_link_to depending on modal_request? --- app/helpers/trestle/url_helper.rb | 2 +- lib/trestle/resource/toolbar.rb | 2 +- spec/support/contexts/template.rb | 1 + spec/trestle/helpers/url_helper_spec.rb | 1 + spec/trestle/resource/toolbar_spec.rb | 6 +++--- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/helpers/trestle/url_helper.rb b/app/helpers/trestle/url_helper.rb index b3b7d7b7..4a9fa257 100644 --- a/app/helpers/trestle/url_helper.rb +++ b/app/helpers/trestle/url_helper.rb @@ -50,7 +50,7 @@ def admin_link_to(content, instance_or_url=nil, options={}, &block) if MODAL_ACTIONS.include?(action) && admin.respond_to?(:form) && admin.form.modal? options[:data][:controller] ||= "modal-trigger" else - options[:data][:turbo_frame] = "_top" + options[:data][:turbo_frame] ||= (modal_request? ? "modal" : "_top") end link_to(content, path, options) diff --git a/lib/trestle/resource/toolbar.rb b/lib/trestle/resource/toolbar.rb index 1984da37..c919c39f 100644 --- a/lib/trestle/resource/toolbar.rb +++ b/lib/trestle/resource/toolbar.rb @@ -20,7 +20,7 @@ def save(label: t("buttons.save", default: "Save %{model_name}"), **attrs) def delete(label: t("buttons.delete", default: "Delete %{model_name}"), **attrs) return unless action?(:destroy) - defaults = Trestle::Options.new(action: :destroy, style: :danger, icon: "fa fa-trash", data: { turbo_method: "delete", controller: "confirm-delete", confirm_delete_placement_value: "bottom" }) + defaults = Trestle::Options.new(action: :destroy, style: :danger, icon: "fa fa-trash", data: { turbo_method: "delete", turbo_frame: "_top", controller: "confirm-delete", confirm_delete_placement_value: "bottom" }) link(label, instance, defaults.merge(attrs)) end diff --git a/spec/support/contexts/template.rb b/spec/support/contexts/template.rb index d0f0b5cf..edbc8175 100644 --- a/spec/support/contexts/template.rb +++ b/spec/support/contexts/template.rb @@ -7,5 +7,6 @@ template.extend(Trestle::UrlHelper) allow(template).to receive(:admin).and_return(admin) + allow(template).to receive(:modal_request?).and_return(false) end end diff --git a/spec/trestle/helpers/url_helper_spec.rb b/spec/trestle/helpers/url_helper_spec.rb index 59880f92..43811a0d 100644 --- a/spec/trestle/helpers/url_helper_spec.rb +++ b/spec/trestle/helpers/url_helper_spec.rb @@ -7,6 +7,7 @@ let(:form) { double(modal?: false) } let(:admin) { double(form: form) } + let(:modal_request?) { false } before(:each) do allow(Trestle).to receive(:lookup).and_return(nil) diff --git a/spec/trestle/resource/toolbar_spec.rb b/spec/trestle/resource/toolbar_spec.rb index 4371b270..d0e348ca 100644 --- a/spec/trestle/resource/toolbar_spec.rb +++ b/spec/trestle/resource/toolbar_spec.rb @@ -69,15 +69,15 @@ end it "returns a delete link" do - expect(builder.delete).to eq(Trestle::Toolbar::Link.new(template, "Delete Resource", instance, action: :destroy, style: :danger, icon: "fa fa-trash", data: { turbo_method: "delete", controller: "confirm-delete", confirm_delete_placement_value: "bottom" })) + expect(builder.delete).to eq(Trestle::Toolbar::Link.new(template, "Delete Resource", instance, action: :destroy, style: :danger, icon: "fa fa-trash", data: { turbo_method: "delete", turbo_frame: "_top", controller: "confirm-delete", confirm_delete_placement_value: "bottom" })) end it "accepts a custom label" do - expect(builder.delete(label: "Delete Me")).to eq(Trestle::Toolbar::Link.new(template, "Delete Me", instance, action: :destroy, style: :danger, icon: "fa fa-trash", data: { turbo_method: "delete", controller: "confirm-delete", confirm_delete_placement_value: "bottom" })) + expect(builder.delete(label: "Delete Me")).to eq(Trestle::Toolbar::Link.new(template, "Delete Me", instance, action: :destroy, style: :danger, icon: "fa fa-trash", data: { turbo_method: "delete", turbo_frame: "_top", controller: "confirm-delete", confirm_delete_placement_value: "bottom" })) end it "accepts custom options" do - expect(builder.delete(style: :warning, icon: "fas fa-bomb")).to eq(Trestle::Toolbar::Link.new(template, "Delete Resource", instance, action: :destroy, style: :warning, icon: "fas fa-bomb", data: { turbo_method: "delete", controller: "confirm-delete", confirm_delete_placement_value: "bottom" })) + expect(builder.delete(style: :warning, icon: "fas fa-bomb")).to eq(Trestle::Toolbar::Link.new(template, "Delete Resource", instance, action: :destroy, style: :warning, icon: "fas fa-bomb", data: { turbo_method: "delete", turbo_frame: "_top", controller: "confirm-delete", confirm_delete_placement_value: "bottom" })) end end