Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/helpers/admin_nav_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def active?(controller_path, request_path)

# The groups the user can see, each with at least one link.
def admin_nav_groups(user = current_user)
# The sidebar, the command bar and the crumb all read the groups, so build
# them once per request and user.
(@admin_nav_groups ||= {})[user&.id] ||= build_admin_nav_groups(user)
end

def build_admin_nav_groups(user)
# An impersonating admin acts as the member, and the identity area
# refuses impersonation, so the links would only refuse.
return [] if user.nil? || impersonating?
Expand Down Expand Up @@ -83,6 +89,19 @@ def admin_nav_groups(user = current_user)

def admin_nav_link_active?(link) = link.active?(controller_path, request.path)

# The middle crumb of the top bar: the sidebar link of the section this page
# is under, so a detail page leads back to its list. Nil on the section's
# own page and on the home page, where the crumb would repeat the title or
# the first crumb. A form that fails re-renders at the list URL by POST, so
# only a GET of the list URL counts as the section's own page.
def admin_section_crumb
link = admin_nav_groups.flat_map(&:links).find { |entry| admin_nav_link_active?(entry) }
return if link.nil? || link.path == admin_home_path
return if request.get? && link.path == request.path

link
end

# The identity area entry for the top bar and the home page, as
# [label, path, icon], or nil.
def identity_home_link(user = current_user)
Expand Down
4 changes: 4 additions & 0 deletions app/views/layouts/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
</button>
<div class="admin-topbar-crumb">
<%= link_to (controller_path.start_with?("identity/") ? "Identity" : "Admin"), admin_home_path, class: "link-quiet" %>
<% if (section = admin_section_crumb) %>
<span class="mx-1 text-ink-faint">/</span>
<%= link_to section.label, section.path, class: "link-quiet" %>
<% end %>
<% if content_for?(:title) %>
<span class="mx-1 text-ink-faint">/</span>
<strong><%= content_for(:title) %></strong>
Expand Down
39 changes: 39 additions & 0 deletions spec/requests/admin_nav_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,45 @@ def current_link = Nokogiri::HTML(response.body).at_css('aside[aria-label="Admin
expect(response.body).not_to include(identity_verifications_path, identity_governance_path)
end

def crumbs = Nokogiri::HTML(response.body).css(".admin-topbar-crumb a, .admin-topbar-crumb strong").map { |node| node.text.strip }

it "puts the section in the top bar crumbs of a page under it" do
sign_in_via_magic_link(create(:user, :admin, :verified))
application = Doorkeeper::Application.create!(name: "Quilt Staging", redirect_uri: "https://quilt.example.com/cb", scopes: "profile")

get admin_oauth_application_path(application)

expect(crumbs).to eq(["Admin", "OAuth apps", "Quilt Staging"])
expect(Nokogiri::HTML(response.body).at_css(".admin-topbar-crumb a[href='#{admin_oauth_applications_path}']")).to be_present
end

it "leaves the section out of the crumbs on the section's own page" do
sign_in_via_magic_link(create(:user, :admin, :verified))

get admin_oauth_applications_path

expect(crumbs).to eq(["Admin", "OAuth apps"])
expect(Nokogiri::HTML(response.body).at_css(".admin-topbar-crumb a[href='#{admin_oauth_applications_path}']")).to be_nil
end

it "keeps the section crumb when a failed form re-renders at the list URL" do
sign_in_via_magic_link(create(:user, :admin, :verified))

post admin_users_path, params: { user: { first_name: "", email: "" } }

expect(response).to have_http_status(:unprocessable_content)
expect(crumbs).to include("Users")
end

it "leaves out a section crumb that would repeat the home crumb" do
sign_in_via_magic_link(create(:user, :verified, identity_team: true))

get identity_verification_path(create(:verification))

expect(crumbs.first).to eq("Identity")
expect(crumbs).not_to include("Verification")
end

it "highlights the group link for a page under it" do
sign_in_via_magic_link(create(:user, :identity_team, :verified))

Expand Down
Loading