Skip to content

Commit

Permalink
Implement devolved nations component
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-kirwan committed Sep 15, 2021
1 parent a342992 commit acabc7f
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 65 deletions.
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
37 changes: 1 addition & 36 deletions app/presenters/content_item/national_applicability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/views/content_items/consultation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= 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 %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/content_items/detailed_guide.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= 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", {
Expand Down
3 changes: 2 additions & 1 deletion app/views/content_items/publication.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<div class="responsive-bottom-margin">
<%= render 'components/important-metadata', items: @content_item.important_metadata %>
<%= render "govuk_publishing_components/components/devolved_nations", national_applicability: @content_item.national_applicability || {} %>

<div class="responsive-bottom-margin">
<%= render "attachments",
title: t("publication.documents", count: 5), # This should always be pluralised.
Expand Down
3 changes: 1 addition & 2 deletions test/integration/consultation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions test/integration/detailed_guide_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions test/integration/publication_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion test/presenters/consultation_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions test/presenters/detailed_guide_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a rel="external" class="govuk-link govuk-link--inverse" href="http://www.dardni.gov.uk/news-dard-pa022-a-13-new-procedure-for">Northern Ireland</a>)'
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
Expand Down
13 changes: 8 additions & 5 deletions test/presenters/publication_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ def schema_name
assert_equal '<time datetime="2015-01-13T13:05:30Z">13 January 2015</time>', 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 <a rel="external" class="govuk-link govuk-link--inverse" href="http://www.dsdni.gov.uk/index/stats_and_research/stats-publications/stats-housing-publications/housing_stats.htm">Northern Ireland</a>, <a rel="external" class="govuk-link govuk-link--inverse" href="http://www.scotland.gov.uk/Topics/Statistics/Browse/Housing-Regeneration/HSfS">Scotland</a>, and <a rel="external" class="govuk-link govuk-link--inverse" href="http://wales.gov.uk/topics/statistics/headlines/housing2012/121025/?lang=en">Wales</a>)',
)
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
9 changes: 9 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit acabc7f

Please sign in to comment.