diff --git a/app/helpers/trestle/url_helper.rb b/app/helpers/trestle/url_helper.rb index dcda4149..b3b7d7b7 100644 --- a/app/helpers/trestle/url_helper.rb +++ b/app/helpers/trestle/url_helper.rb @@ -47,7 +47,7 @@ def admin_link_to(content, instance_or_url=nil, options={}, &block) # Determine link data options options[:data] ||= {} - if MODAL_ACTIONS.include?(action) && admin.form.modal? + if MODAL_ACTIONS.include?(action) && admin.respond_to?(:form) && admin.form.modal? options[:data][:controller] ||= "modal-trigger" else options[:data][:turbo_frame] = "_top" diff --git a/lib/trestle/toolbar/menu.rb b/lib/trestle/toolbar/menu.rb index 74807960..546eebf0 100644 --- a/lib/trestle/toolbar/menu.rb +++ b/lib/trestle/toolbar/menu.rb @@ -28,13 +28,17 @@ def render_items end class Builder - delegate :admin_link_to, :content_tag, to: :@template + delegate :admin_link_to, :content_tag, :tag, to: :@template def initialize(menu, template) @menu, @template = menu, template end def link(content, instance_or_url=nil, options={}, &block) + if instance_or_url.is_a?(Hash) + instance_or_url, options = nil, instance_or_url + end + options[:class] = Array(options[:class]) options[:class] << "dropdown-item" @@ -42,16 +46,15 @@ def link(content, instance_or_url=nil, options={}, &block) end def header(text) - item(class: "dropdown-header") { text } + item { content_tag(:h6, text, class: "dropdown-header") } end def divider - item(class: "divider") + item { tag(:hr, class: "dropdown-divider") } end def item(options={}, &block) - opts = { role: "presentation" }.merge(options) - item = block_given? ? content_tag(:li, opts, &block) : content_tag(:li, "", opts) + item = block_given? ? content_tag(:li, options, &block) : content_tag(:li, "", options) @menu.items << item diff --git a/sandbox/app/admin/articles_admin.rb b/sandbox/app/admin/articles_admin.rb index 98a40218..8350e302 100644 --- a/sandbox/app/admin/articles_admin.rb +++ b/sandbox/app/admin/articles_admin.rb @@ -13,8 +13,10 @@ end hook "index.toolbar.secondary" do |t| - t.link "Batch Action (GET)", action: :batch_get, style: :info, data: { controller: "batch-action" } - t.link "Batch Action (POST)", action: :batch_post, style: :warning, data: { controller: "confirm batch-action", turbo_method: :post } + t.dropdown "Batch Actions", style: :info do |l| + l.link "Batch Action (GET)", action: :batch_get, data: { controller: "batch-action" } + l.link "Batch Action (POST)", action: :batch_post, data: { controller: "confirm batch-action", turbo_method: :post } + end end hook "new.toolbar.secondary" do |t| diff --git a/spec/trestle/toolbar/item_spec.rb b/spec/trestle/toolbar/item_spec.rb index 19014f81..f49d4dbb 100644 --- a/spec/trestle/toolbar/item_spec.rb +++ b/spec/trestle/toolbar/item_spec.rb @@ -44,16 +44,24 @@ shared_examples "a toolbar item with a dropdown" do |tag, attrs| include_context "template" + let(:admin) { double } + let(:options) { {} } let(:block) do ->(d) { d.header "Header" d.link "Link", "#" + d.link "Disabled Link", class: "disabled", admin: :test d.divider } end + before(:each) { + allow(Trestle).to receive(:lookup).with(:test).and_return(admin) + allow(admin).to receive(:path).and_return("/admin/test") + } + it "renders the button within a button group" do expect(subject.to_s).to have_tag(".btn-group", with: { role: "group" }) do with_tag "#{tag}.btn.btn-default" @@ -62,11 +70,18 @@ it "renders the block items within a dropdown menu" do expect(subject.to_s).to have_tag(".btn-group", with: { role: "group" }) do - with_tag "li.dropdown-header", text: "Header", with: { role: "presentation" } - with_tag "li", with: { role: "presentation" } do + with_tag "li" do + with_tag "h6", text: "Header", with: { class: "dropdown-header" } + end + with_tag "li" do with_tag "a", text: "Link", with: { href: "#", class: "dropdown-item" } end - with_tag "li.divider", with: { role: "presentation" } + with_tag "li" do + with_tag "a", text: "Disabled Link", with: { href: "/admin/test", class: "disabled dropdown-item" } + end + with_tag "li" do + with_tag "hr", class: "dropdown-divider" + end end end end