From acabc7fe82c494da81aeb89ac7458ac9ec2e5ea1 Mon Sep 17 00:00:00 2001 From: Jon Kirwan <87758239+jon-kirwan@users.noreply.github.com> Date: Wed, 15 Sep 2021 09:36:33 +0100 Subject: [PATCH] Implement devolved nations component --- app/assets/stylesheets/application.scss | 1 + .../content_item/national_applicability.rb | 37 +------------------ app/views/content_items/consultation.html.erb | 2 +- .../content_items/detailed_guide.html.erb | 2 +- app/views/content_items/publication.html.erb | 3 +- test/integration/consultation_test.rb | 3 +- test/integration/detailed_guide_test.rb | 13 ++++--- test/integration/publication_test.rb | 22 ++++++----- .../presenters/consultation_presenter_test.rb | 5 ++- .../detailed_guide_presenter_test.rb | 13 +++++-- test/presenters/publication_presenter_test.rb | 13 ++++--- test/test_helper.rb | 9 +++++ 12 files changed, 58 insertions(+), 65 deletions(-) diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 82301e493..cfb6adb7a 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -14,6 +14,7 @@ $govuk-new-link-styles: true; @import 'govuk_publishing_components/components/contents-list'; @import 'govuk_publishing_components/components/contextual-sidebar'; @import 'govuk_publishing_components/components/details'; +@import 'govuk_publishing_components/components/devolved-nations'; @import 'govuk_publishing_components/components/document-list'; @import 'govuk_publishing_components/components/error-message'; @import 'govuk_publishing_components/components/error-summary'; diff --git a/app/presenters/content_item/national_applicability.rb b/app/presenters/content_item/national_applicability.rb index 0da882cd3..a841c5461 100644 --- a/app/presenters/content_item/national_applicability.rb +++ b/app/presenters/content_item/national_applicability.rb @@ -2,43 +2,8 @@ module ContentItem module NationalApplicability include Metadata - def applies_to - return nil unless national_applicability - - all_nations = national_applicability.values - applicable_nations = all_nations.select { |n| n["applicable"] } - inapplicable_nations = all_nations - applicable_nations - - applies_to = applicable_nations.map { |n| n["label"] }.to_sentence - - if inapplicable_nations.any? - nations_with_alt_urls = inapplicable_nations.select { |n| n["alternative_url"].present? } - if nations_with_alt_urls.any? - alternate_links = nations_with_alt_urls - .map { |n| view_context.link_to(n["label"], n["alternative_url"], rel: :external, class: "govuk-link govuk-link--inverse") } - .to_sentence - - applies_to += " (see #{translated_schema_name(nations_with_alt_urls.count)} for #{alternate_links})" - end - end - - applies_to - end - - def important_metadata - super.tap do |m| - m.merge!("Applies to" => applies_to) - end - end - - private - - def translated_schema_name(count) - I18n.t("content_item.schema_name.#{schema_name}", count: count).downcase - end - def national_applicability - content_item["details"]["national_applicability"] + content_item["details"]["national_applicability"]&.deep_symbolize_keys end end end diff --git a/app/views/content_items/consultation.html.erb b/app/views/content_items/consultation.html.erb index 40c2e1ec1..6f9935f76 100644 --- a/app/views/content_items/consultation.html.erb +++ b/app/views/content_items/consultation.html.erb @@ -17,7 +17,7 @@
- <%= render 'components/important-metadata', items: @content_item.important_metadata %> + <%= render "govuk_publishing_components/components/devolved_nations", national_applicability: @content_item.national_applicability || {} %> <% if @content_item.unopened? %> <% content_item_unopened = capture do %> diff --git a/app/views/content_items/detailed_guide.html.erb b/app/views/content_items/detailed_guide.html.erb index ea394cd50..4d4af535a 100644 --- a/app/views/content_items/detailed_guide.html.erb +++ b/app/views/content_items/detailed_guide.html.erb @@ -40,7 +40,7 @@
- <%= render 'components/important-metadata', items: @content_item.important_metadata %> + <%= render "govuk_publishing_components/components/devolved_nations", national_applicability: @content_item.national_applicability || {} %> <%= render "components/contents-list-with-body", contents: @content_item.contents do %> <%= render "govuk_publishing_components/components/print_link", { diff --git a/app/views/content_items/publication.html.erb b/app/views/content_items/publication.html.erb index 0211200a8..a7147c5e2 100644 --- a/app/views/content_items/publication.html.erb +++ b/app/views/content_items/publication.html.erb @@ -27,7 +27,8 @@
- <%= render 'components/important-metadata', items: @content_item.important_metadata %> + <%= render "govuk_publishing_components/components/devolved_nations", national_applicability: @content_item.national_applicability || {} %> +
<%= render "attachments", title: t("publication.documents", count: 5), # This should always be pluralised. diff --git a/test/integration/consultation_test.rb b/test/integration/consultation_test.rb index 9fb6883e8..ac6c44cab 100644 --- a/test/integration/consultation_test.rb +++ b/test/integration/consultation_test.rb @@ -127,8 +127,7 @@ class ConsultationTest < ActionDispatch::IntegrationTest test "consultation that only applies to a set of nations" do setup_and_visit_content_item("consultation_outcome_with_feedback") - - assert_has_important_metadata("Applies to": "England") + assert_has_devolved_nations_component("Applies to England") end test "ways to respond renders" do diff --git a/test/integration/detailed_guide_test.rb b/test/integration/detailed_guide_test.rb index ba8008b12..67f8561ac 100644 --- a/test/integration/detailed_guide_test.rb +++ b/test/integration/detailed_guide_test.rb @@ -46,16 +46,17 @@ class DetailedGuideTest < ActionDispatch::IntegrationTest test "detailed guide that only applies to a set of nations" do setup_and_visit_content_item("national_applicability_detailed_guide") - assert_has_important_metadata("Applies to:": "England") + assert_has_devolved_nations_component("Applies to England") end test "detailed guide that only applies to a set of nations, with alternative urls" do setup_and_visit_content_item("national_applicability_alternative_url_detailed_guide") - - assert_has_important_metadata( - 'Applies to:': - "England, Scotland, and Wales (see guidance for Northern Ireland)", - ) + assert_has_devolved_nations_component("Applies to England, Scotland and Wales", [ + { + text: "Northern Ireland", + alternative_url: "http://www.dardni.gov.uk/news-dard-pa022-a-13-new-procedure-for", + }, + ]) end test "translated detailed guide" do diff --git a/test/integration/publication_test.rb b/test/integration/publication_test.rb index 6e4f02af5..b4b552386 100644 --- a/test/integration/publication_test.rb +++ b/test/integration/publication_test.rb @@ -76,15 +76,19 @@ class PublicationTest < ActionDispatch::IntegrationTest test "renders 'Applies to' block in important metadata when there are excluded nations" do setup_and_visit_content_item("statistics_publication") - - assert_has_important_metadata( - "Applies to": { - "England (see publications for Northern Ireland, Scotland, and Wales)": nil, - "Northern Ireland": - "http://www.dsdni.gov.uk/index/stats_and_research/stats-publications/stats-housing-publications/housing_stats.htm", - "Scotland": "http://www.scotland.gov.uk/Topics/Statistics/Browse/Housing-Regeneration/HSfS", - "Wales": "http://wales.gov.uk/topics/statistics/headlines/housing2012/121025/?lang=en", + assert_has_devolved_nations_component("Applies to England", [ + { + text: "Northern Ireland", + alternative_url: "http://www.dsdni.gov.uk/index/stats_and_research/stats-publications/stats-housing-publications/housing_stats.htm", }, - ) + { + text: "Scotland", + alternative_url: "http://www.scotland.gov.uk/Topics/Statistics/Browse/Housing-Regeneration/HSfS", + }, + { + text: "Wales", + alternative_url: "http://wales.gov.uk/topics/statistics/headlines/housing2012/121025/?lang=en", + }, + ]) end end diff --git a/test/presenters/consultation_presenter_test.rb b/test/presenters/consultation_presenter_test.rb index 995241b7d..4bdc140cf 100644 --- a/test/presenters/consultation_presenter_test.rb +++ b/test/presenters/consultation_presenter_test.rb @@ -96,7 +96,10 @@ def schema_name presented = presented_item("consultation_outcome_with_feedback") assert example["details"].include?("national_applicability") - assert_equal presented.applies_to, "England" + assert_equal(presented.national_applicability[:england][:applicable], true) + assert_equal(presented.national_applicability[:northern_ireland][:applicable], false) + assert_equal(presented.national_applicability[:scotland][:applicable], false) + assert_equal(presented.national_applicability[:wales][:applicable], false) end test "presents ways to respond" do diff --git a/test/presenters/detailed_guide_presenter_test.rb b/test/presenters/detailed_guide_presenter_test.rb index be23553d9..99d18ac0d 100644 --- a/test/presenters/detailed_guide_presenter_test.rb +++ b/test/presenters/detailed_guide_presenter_test.rb @@ -50,15 +50,22 @@ def schema_name presented = presented_item("national_applicability_detailed_guide") assert example["details"].include?("national_applicability") - assert_equal presented.applies_to, "England" + assert_equal(presented.national_applicability[:england][:applicable], true) + assert_equal(presented.national_applicability[:northern_ireland][:applicable], false) + assert_equal(presented.national_applicability[:scotland][:applicable], false) + assert_equal(presented.national_applicability[:wales][:applicable], false) end - test "content can apply only to a set of nations with alternative urls" do + test "content can apply only to a set of nations (with alternative URLs where applicable)" do example = schema_item("national_applicability_alternative_url_detailed_guide") presented = presented_item("national_applicability_alternative_url_detailed_guide") assert example["details"].include?("national_applicability") - assert_equal presented.applies_to, 'England, Scotland, and Wales (see guidance for Northern Ireland)' + assert_equal(presented.national_applicability[:england][:applicable], true) + assert_equal(presented.national_applicability[:northern_ireland][:applicable], false) + assert_equal(presented.national_applicability[:northern_ireland][:alternative_url], "http://www.dardni.gov.uk/news-dard-pa022-a-13-new-procedure-for") + assert_equal(presented.national_applicability[:scotland][:applicable], true) + assert_equal(presented.national_applicability[:wales][:applicable], true) end test "context in title is overridden to display as guidance" do diff --git a/test/presenters/publication_presenter_test.rb b/test/presenters/publication_presenter_test.rb index af5148c69..b5cc1b306 100644 --- a/test/presenters/publication_presenter_test.rb +++ b/test/presenters/publication_presenter_test.rb @@ -61,14 +61,17 @@ def schema_name assert_equal '', presented.withdrawal_notice_component[:time] end - test "content can apply only to a set of nations (with alternative urls when provided)" do + test "content can apply only to a set of nations (with alternative URLs where applicable)" do example = schema_item("statistics_publication") presented = presented_item("statistics_publication") assert example["details"].include?("national_applicability") - assert_equal( - presented.applies_to, - 'England (see publications for Northern Ireland, Scotland, and Wales)', - ) + assert_equal(presented.national_applicability[:england][:applicable], true) + assert_equal(presented.national_applicability[:northern_ireland][:applicable], false) + assert_equal(presented.national_applicability[:northern_ireland][:alternative_url], "http://www.dsdni.gov.uk/index/stats_and_research/stats-publications/stats-housing-publications/housing_stats.htm") + assert_equal(presented.national_applicability[:scotland][:applicable], false) + assert_equal(presented.national_applicability[:scotland][:alternative_url], "http://www.scotland.gov.uk/Topics/Statistics/Browse/Housing-Regeneration/HSfS") + assert_equal(presented.national_applicability[:wales][:applicable], false) + assert_equal(presented.national_applicability[:wales][:alternative_url], "http://wales.gov.uk/topics/statistics/headlines/housing2012/121025/?lang=en") end end diff --git a/test/test_helper.rb b/test/test_helper.rb index fcc887260..4ff8a73b6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -148,6 +148,15 @@ def assert_has_important_metadata(metadata) end end + def assert_has_devolved_nations_component(text, nations = nil) + within(".gem-c-devolved-nations") do + assert page.has_text?(text) + nations&.each do |nation| + assert page.has_link?("Guidance for #{nation[:text]}", href: nation[:alternative_url]) + end + end + end + def assert_footer_has_published_dates(first_published = nil, last_updated = nil, history_link: false) assert_has_published_dates(first_published, last_updated, history_link: history_link) end