From 01b1f9e21f4d06bd2a20b1779494d69701c5ff9f Mon Sep 17 00:00:00 2001 From: davidtrussler Date: Tue, 17 Sep 2024 17:39:19 +0100 Subject: [PATCH] Add new routes and helper for tabs --- app/controllers/editions_controller.rb | 20 ++++++++ app/helpers/tabbed_nav_helper.rb | 32 +++++++++++++ .../editions/_secondary_navigation.html.erb | 33 +------------ config/routes.rb | 11 ++++- .../helpers/admin/tabbed_nav_helper_test.rb | 47 +++++++++++++++++++ 5 files changed, 110 insertions(+), 33 deletions(-) create mode 100644 app/helpers/tabbed_nav_helper.rb create mode 100644 test/unit/helpers/admin/tabbed_nav_helper_test.rb diff --git a/app/controllers/editions_controller.rb b/app/controllers/editions_controller.rb index 9dca5cede..ddbb55dd5 100644 --- a/app/controllers/editions_controller.rb +++ b/app/controllers/editions_controller.rb @@ -16,6 +16,26 @@ def show render action: "show" end + def metadata + render action: "show" + end + + def history + render action: "show" + end + + def admin + render action: "show" + end + + def linking + render action: "show" + end + + def unpublish + render action: "show" + end + protected def setup_view_paths diff --git a/app/helpers/tabbed_nav_helper.rb b/app/helpers/tabbed_nav_helper.rb new file mode 100644 index 000000000..e733afd7d --- /dev/null +++ b/app/helpers/tabbed_nav_helper.rb @@ -0,0 +1,32 @@ +module TabbedNavHelper + def edition_nav_items(edition) + nav_items = [] + items = %w[edit tagging metadata history admin related_external_links unpublish] + + items.each do |item| + nav_items << standard_nav_items(item, edition) + end + + nav_items.flatten + end + + def standard_nav_items(item, edition) + url = item.eql?("edit") ? url_for([:edition, { id: edition.id }]) : url_for([:edition, { action: item, id: edition.id }]) + + label = Edition::Tab[item].title + href = url + current = request.path == url + + edit_nav_item(label, href, current) + end + + def edit_nav_item(label, href, current) + [ + { + label:, + href:, + current:, + }, + ] + end +end diff --git a/app/views/editions/_secondary_navigation.html.erb b/app/views/editions/_secondary_navigation.html.erb index 9d14c9e4a..0a456c29a 100644 --- a/app/views/editions/_secondary_navigation.html.erb +++ b/app/views/editions/_secondary_navigation.html.erb @@ -1,35 +1,4 @@ <%= render "govuk_publishing_components/components/secondary_navigation", { aria_label: "Document navigation", - items: [ - { - label: "Edit", - href: "#1", - current: true, - }, - { - label: "Tagging", - href: "#2", - }, - { - label: "Metadata", - href: "#3" - }, - { - label: "History and notes", - href: "#1", - current: true, - }, - { - label: "Admin", - href: "#2" - }, - { - label: "Related external links", - href: "#3", - }, - { - label: "Unpublish", - href: "#3" - } - ] + items: edition_nav_items(@edition), } %> diff --git a/config/routes.rb b/config/routes.rb index 342a257a5..7e286645a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,7 +17,16 @@ resources :artefacts, only: %i[new create update] constraints FeatureConstraint.new("design_system_edit") do - resources :editions, only: %i[show index] + resources :editions do + member do + get "metadata" + get "history" + get "admin" + get "related_external_links", to: "editions#linking" + get "tagging", to: "editions#linking" + get "unpublish" + end + end end get "editions/:id" => "legacy_editions#show" diff --git a/test/unit/helpers/admin/tabbed_nav_helper_test.rb b/test/unit/helpers/admin/tabbed_nav_helper_test.rb new file mode 100644 index 000000000..36381bd57 --- /dev/null +++ b/test/unit/helpers/admin/tabbed_nav_helper_test.rb @@ -0,0 +1,47 @@ +require "test_helper" + +class TabbedNavHelperTest < ActionView::TestCase + test "#secondary_navigation_tabs_items for edit edition page" do + resource = FactoryBot.create(:guide_edition, title: "Edit page title", state: "draft") + + expected_output = [ + { + label: "Edit", + href: "/editions/#{resource.id}", + current: false, + }, + { + label: "Tagging", + href: "/editions/#{resource.id}/tagging", + current: false, + }, + { + label: "Metadata", + href: "/editions/#{resource.id}/metadata", + current: false, + }, + { + label: "History and notes", + href: "/editions/#{resource.id}/history", + current: false, + }, + { + label: "Admin", + href: "/editions/#{resource.id}/admin", + current: false, + }, + { + label: "Related external links", + href: "/editions/#{resource.id}/related_external_links", + current: false, + }, + { + label: "Unpublish", + href: "/editions/#{resource.id}/unpublish", + current: false, + }, + ] + + assert_equal expected_output, edition_nav_items(resource) + end +end