diff --git a/app/helpers/admin_nav_helper.rb b/app/helpers/admin_nav_helper.rb index 89d5296..e4f5895 100644 --- a/app/helpers/admin_nav_helper.rb +++ b/app/helpers/admin_nav_helper.rb @@ -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? @@ -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) diff --git a/app/views/layouts/admin.html.erb b/app/views/layouts/admin.html.erb index 790fd85..45c66bc 100644 --- a/app/views/layouts/admin.html.erb +++ b/app/views/layouts/admin.html.erb @@ -25,6 +25,10 @@
<%= link_to (controller_path.start_with?("identity/") ? "Identity" : "Admin"), admin_home_path, class: "link-quiet" %> + <% if (section = admin_section_crumb) %> + / + <%= link_to section.label, section.path, class: "link-quiet" %> + <% end %> <% if content_for?(:title) %> / <%= content_for(:title) %> diff --git a/spec/requests/admin_nav_spec.rb b/spec/requests/admin_nav_spec.rb index 5eec19d..3d7a6ed 100644 --- a/spec/requests/admin_nav_spec.rb +++ b/spec/requests/admin_nav_spec.rb @@ -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))