Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link to parent menu item #8225

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/active_admin/base_controller/menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def current_menu
active_admin_config.navigation_menu
end

def current_menu_item?(item)
item.current?(@current_menu_item)
def current_menu_item?(item, children: true)
item.current?(@current_menu_item, children: children)
end

def set_current_menu_item
Expand Down
5 changes: 3 additions & 2 deletions app/javascript/active_admin/features/main_menu.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Rails from '@rails/ujs';

const toggleMenu = function(event) {
const parent = this.parentNode
const toggleMenu = function() {
const parent = this.closest([`[data-item-id="${this.dataset.parentId}"]`])

if (!("open" in parent.dataset)) {
parent.dataset.open = ""
} else {
Expand Down
28 changes: 18 additions & 10 deletions app/views/active_admin/_main_navigation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,32 @@
<% current_menu.items(self).each do |item| %>
<% children = item.items(self).presence %>
<li <%= current_menu_item?(item) && "data-open" %> class="group" data-item-id="<%= item.id %>">
<% if children %>
<button data-menu-button class="text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white flex items-center w-full rounded-md p-2 gap-x-2 text-sm">
<% label = capture do %>
<% if (url = item.url(self)).present? %>
<%= link_to item.label(self), url, item.html_options.merge(class: "text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white flex items-center w-full rounded-md p-2 gap-x-2 text-sm no-underline #{(current_menu_item?(item, children: false) ? "bg-gray-100 dark:bg-white/5 text-gray-900 dark:text-white selected" : "")}") %>
<% else %>
<%= item.label(self) %>
<svg class="group-data-[open]:rotate-90 group-data-[open]:rtl:-rotate-90 ms-auto h-5 w-5 shrink-0 rtl:-scale-x-100" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M7.21 14.77a.75.75 0 01.02-1.06L11.168 10 7.23 6.29a.75.75 0 111.04-1.08l4.5 4.25a.75.75 0 010 1.08l-4.5 4.25a.75.75 0 01-1.06-.02z" clip-rule="evenodd" />
</svg>
</button>
<% end %>
<% end %>

<% if children %>
<div class="flex gap-2">
<div class="flex-auto"><%= label %></div>
<button data-parent-id="<%= item.id %>" data-menu-button class="bg-gray-100 dark:bg-white/5 text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white flex items-center rounded-full p-2 text-sm">
<svg class="group-data-[open]:rotate-90 group-data-[open]:rtl:-rotate-90 ms-auto h-5 w-5 shrink-0 rtl:-scale-x-100" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M7.21 14.77a.75.75 0 01.02-1.06L11.168 10 7.23 6.29a.75.75 0 111.04-1.08l4.5 4.25a.75.75 0 010 1.08l-4.5 4.25a.75.75 0 01-1.06-.02z" clip-rule="evenodd" />
</svg>
</button>
</div>
<ul role="list" class="mt-1 space-y-1 hidden group-data-[open]:block">
<% children.each do |j| %>
<li data-item-id="<%= j.id %>">
<%= link_to j.label(self), j.url(self), j.html_options.merge(class: "text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white block rounded-md py-1.5 px-2 text-sm no-underline #{(current_menu_item?(j) ? "bg-gray-100 dark:bg-white/5 text-gray-900 dark:text-white selected" : "")}") %>
<%= link_to j.label(self), j.url(self), j.html_options.merge(class: "text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white block rounded-md py-1.5 px-2 ml-4 text-sm no-underline #{(current_menu_item?(j) ? "bg-gray-100 dark:bg-white/5 text-gray-900 dark:text-white selected" : "")}") %>
</li>
<% end %>
</ul>
<% elsif url = item.url(self) %>
<%= link_to item.label(self), url, item.html_options.merge(class: "text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white flex items-center w-full rounded-md p-2 gap-x-2 text-sm no-underline #{(current_menu_item?(item) ? "bg-gray-100 dark:bg-white/5 text-gray-900 dark:text-white selected" : "")}") %>
<% else %>
<%= item.label(self) %>
<%= label %>
<% end %>
</li>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion features/step_definitions/menu_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
end

Then /^I should see a menu parent for "([^"]*)"$/ do |name|
expect(page).to have_css "#main-menu li button", text: name
expect(page).to have_css "#main-menu li a", text: name
end

Then /^I should see a nested menu item for "([^"]*)"$/ do |name|
Expand Down
4 changes: 2 additions & 2 deletions lib/active_admin/menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def include?(item)
end

# Used in the UI to visually distinguish which menu item is selected.
def current?(item)
self == item || include?(item)
def current?(item, children: true)
self == item || (children && include?(item))
end

# Returns sorted array of menu items that should be displayed in this context.
Expand Down
5 changes: 5 additions & 0 deletions spec/support/templates/admin/profiles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true
ActiveAdmin.register Profile do
menu parent: 'Users'
permit_params :user_id, :bio
end
3 changes: 3 additions & 0 deletions spec/support/templates/policies/profile_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true
class ProfilePolicy < ApplicationPolicy
end
6 changes: 4 additions & 2 deletions spec/unit/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@

describe "files in load path" do
it "it should load sorted files" do
expect(application.files.map { |f| File.basename(f) }).to eq(%w(admin_users.rb dashboard.rb stores.rb))
expect(application.files.map { |f| File.basename(f) }).to eq %w(admin_users.rb dashboard.rb profiles.rb stores.rb)
end

it "should load files in the first level directory" do
Expand Down Expand Up @@ -103,7 +103,9 @@
begin
FileUtils.mkdir_p(test_dir)
FileUtils.touch(test_file)
expect(application.files.map { |f| File.basename(f) }).to eq(%w(posts.rb admin_users.rb dashboard.rb stores.rb))
expect(application.files.map { |f| File.basename(f) }).to(
eq(%w(posts.rb admin_users.rb dashboard.rb profiles.rb stores.rb))
)
ensure
FileUtils.remove_entry_secure(test_dir, force: true)
end
Expand Down