Skip to content

Commit

Permalink
Set default target turbo_frame within admin_link_to depending on moda…
Browse files Browse the repository at this point in the history
…l_request?
  • Loading branch information
spohlenz committed Aug 14, 2024
1 parent 544db39 commit d18c9e9
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/helpers/trestle/url_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/trestle/resource/toolbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions spec/support/contexts/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions spec/trestle/helpers/url_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions spec/trestle/resource/toolbar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d18c9e9

Please sign in to comment.