Skip to content

Commit

Permalink
Update devolved nations component to accept type property
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-kirwan committed Oct 12, 2021
1 parent 7c968a1 commit d4d7d1c
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
useful summary for people upgrading their application, not a replication
of the commit log.

## Unreleased

* Update devolved nations component to accept type property ([PR #2337](https://github.com/alphagov/govuk_publishing_components/pull/2337))

## 27.6.0

* Pass cookie consent to Digital Identity via query parameter ([PR #2344](https://github.com/alphagov/govuk_publishing_components/pull/2344))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(local_assigns)
devolved_nations_helper = GovukPublishingComponents::Presenters::DevolvedNationsHelper.new(local_assigns)

applies_to ||= t('components.devolved_nations.applies_to')
applies_to ||= t("components.devolved_nations.applies_to")
heading_level ||= 2
%>

Expand All @@ -15,7 +15,7 @@
<% if devolved_nations_helper.nations_with_urls.any? %>
<%= content_tag :ul, class: "govuk-list govuk-!-margin-top-1 govuk-!-margin-bottom-0" do -%>
<% devolved_nations_helper.nations_with_urls.each do |k, v| %>
<%= content_tag(:li, link_to("Guidance for #{t("components.devolved_nations.#{k}")}", v[:alternative_url], class: "govuk-link")) %>
<%= content_tag(:li, link_to(devolved_nations_helper.alternative_content_text(k), v[:alternative_url], class: "govuk-link")) %>
<% end %>
<% end %>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
name: Devolved Nations (experimental)
description: A banner for linking to guidance for other nations.
description: A banner for linking to alternative content for other nations.
body: |
The component replaces uses of the important metadata component for guidance for other nations.
The component replaces uses of the important metadata component for alternative content for other nations.
The component can display:
* nations that the guidance relates to
* nations that the alternative content relates to
* a list of alternative URLs where applicable
shared_accessibility_criteria:
- link
accessibility_criteria: |
The component must:
- Provide context for link text to highlight guidance is available.
- Provide context for link text to highlight alternative content is available.
examples:
default:
data:
Expand All @@ -35,18 +35,22 @@ examples:
applicable: true
wales:
applicable: true
applies_to_one_nation_individual_guidance_available:
applies_to_one_nation_individual_publication_available:
description: If no content type, or an invalid type, is specified then the default alternative content type is displayed as "Publication" e.g. "Publication for Northern Ireland"
data:
national_applicability:
england:
applicable: true
northern_ireland:
applicable: false
alternative_url: /
scotland:
applicable: false
alternative_url: /
wales:
applicable: false
alternative_url: /
applies_to_one_nation_individual_guidance_available:
applies_to_three_nations_individual_publication_available:
data:
national_applicability:
england:
Expand All @@ -55,25 +59,31 @@ examples:
applicable: false
alternative_url: /
scotland:
applicable: false
alternative_url: /
applicable: true
wales:
applicable: true
applies_to_one_nation_individual_consultation_available:
description: Specify alternative type for the content e.g. Consultation
data:
national_applicability:
england:
applicable: true
northern_ireland:
applicable: false
alternative_url: /
applies_to_three_nations_individual_guidance_available:
type: consultation
applies_to_one_nation_individual_guidance_available:
description: Specify alternative type for the content e.g. Guidance
data:
national_applicability:
england:
applicable: true
northern_ireland:
applicable: false
alternative_url: /
scotland:
applicable: true
wales:
applicable: true
type: detailed_guide
specific_heading level:
description: Use a different heading level for the main link title. Defaults to H2 if not passed.
description: Use a different heading level for the main link title. Defaults to `H2` if not passed.
data:
heading_level: 3
national_applicability:
Expand Down
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ en:
connectors:
last_word: " and "
two_words: " and "
type:
consultation: Consultation for %{nation}
detailed_guide: Guidance for %{nation}
guidance: Guidance for %{nation}
publication: Publication for %{nation}
england: England
northern_ireland: Northern Ireland
scotland: Scotland
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module GovukPublishingComponents
module Presenters
class DevolvedNationsHelper
attr_reader :national_applicability
attr_reader :national_applicability, :type

def initialize(local_assigns)
@national_applicability = local_assigns[:national_applicability]
@type = local_assigns[:type] || "publication"
end

def applicable_nations_title_text
Expand All @@ -25,6 +26,16 @@ def nations_with_urls
.present?
end
end

def alternative_content_text(name)
nation = I18n.t("components.devolved_nations.#{name}")

if I18n.exists?("components.devolved_nations.type.#{@type}")
I18n.t("components.devolved_nations.type.#{@type}", nation: nation)
else
I18n.t("components.devolved_nations.type.publication", nation: nation)
end
end
end
end
end
69 changes: 60 additions & 9 deletions spec/components/devolved_nations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,41 +47,41 @@ def component_name
assert_select ".gem-c-devolved-nations > h2", text: "Applies to England, Scotland and Wales"
end

it "renders a devolved nations component, which applies to one nation, with individual guidance available, correctly" do
it "renders a devolved nations component, which applies to one nation, with individual publication available, correctly" do
render_component(
national_applicability: {
england: {
applicable: true,
},
northern_ireland: {
applicable: false,
alternative_url: "/guidance-northern-ireland",
alternative_url: "/publication-northern-ireland",
},
scotland: {
applicable: false,
alternative_url: "/guidance-scotland",
alternative_url: "/publication-scotland",
},
wales: {
applicable: false,
alternative_url: "/guidance-wales",
alternative_url: "/publication-wales",
},
},
)
assert_select ".gem-c-devolved-nations > h2", text: "Applies to England"
assert_select ".gem-c-devolved-nations > ul > li:nth-child(1) > [href='/guidance-northern-ireland']", text: "Guidance for Northern Ireland"
assert_select ".gem-c-devolved-nations > ul > li:nth-child(2) > [href='/guidance-scotland']", text: "Guidance for Scotland"
assert_select ".gem-c-devolved-nations > ul > li:nth-child(3) > [href='/guidance-wales']", text: "Guidance for Wales"
assert_select ".gem-c-devolved-nations > ul > li:nth-child(1) > [href='/publication-northern-ireland']", text: "Publication for Northern Ireland"
assert_select ".gem-c-devolved-nations > ul > li:nth-child(2) > [href='/publication-scotland']", text: "Publication for Scotland"
assert_select ".gem-c-devolved-nations > ul > li:nth-child(3) > [href='/publication-wales']", text: "Publication for Wales"
end

it "renders a devolved nations component, which applies to three nations, with individual guidance available, correctly" do
it "renders a devolved nations component, which applies to three nations, with individual publication available, correctly" do
render_component(
national_applicability: {
england: {
applicable: true,
},
northern_ireland: {
applicable: false,
alternative_url: "/guidance-northern-ireland",
alternative_url: "/publication-northern-ireland",
},
scotland: {
applicable: true,
Expand All @@ -92,9 +92,60 @@ def component_name
},
)
assert_select ".gem-c-devolved-nations > h2", text: "Applies to England, Scotland and Wales"
assert_select ".gem-c-devolved-nations > ul > li:nth-child(1) > [href='/publication-northern-ireland']", text: "Publication for Northern Ireland"
end

it "renders a devolved nations component, which applies to one nation, with individual consultation available, correctly" do
render_component(
national_applicability: {
england: {
applicable: true,
},
northern_ireland: {
applicable: false,
alternative_url: "/consultation-northern-ireland",
},
},
type: "consultation",
)
assert_select ".gem-c-devolved-nations > h2", text: "Applies to England"
assert_select ".gem-c-devolved-nations > ul > li:nth-child(1) > [href='/consultation-northern-ireland']", text: "Consultation for Northern Ireland"
end

it "renders a devolved nations component, which applies to one nation, with individual guidance available, correctly" do
render_component(
national_applicability: {
england: {
applicable: true,
},
northern_ireland: {
applicable: false,
alternative_url: "/guidance-northern-ireland",
},
},
type: "detailed_guide",
)
assert_select ".gem-c-devolved-nations > h2", text: "Applies to England"
assert_select ".gem-c-devolved-nations > ul > li:nth-child(1) > [href='/guidance-northern-ireland']", text: "Guidance for Northern Ireland"
end

it "renders a devolved nations component, which applies to one nation, with individual pubilcation available, when invalid type provided, correctly" do
render_component(
national_applicability: {
england: {
applicable: true,
},
northern_ireland: {
applicable: false,
alternative_url: "/publication-northern-ireland",
},
},
type: "invalid_type",
)
assert_select ".gem-c-devolved-nations > h2", text: "Applies to England"
assert_select ".gem-c-devolved-nations > ul > li:nth-child(1) > [href='/publication-northern-ireland']", text: "Publication for Northern Ireland"
end

it "renders a devolved nations component with different heading levels" do
render_component(
heading_level: 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,38 @@
expect(devolved_nations_helper.applicable_nations_title_text).to eql("England, Scotland and Wales")
end

it "returns title text, which contains one nation, and guidance urls for three nations, correctly" do
it "returns title text, which contains one nation, and publication urls for three nations, correctly" do
devolved_nations_helper = GovukPublishingComponents::Presenters::DevolvedNationsHelper.new(national_applicability: {
england: {
applicable: true,
},
northern_ireland: {
applicable: false,
alternative_url: "/guidance-northern-ireland",
alternative_url: "/publication-northern-ireland",
},
scotland: {
applicable: false,
alternative_url: "/guidance-scotland",
alternative_url: "/publication-scotland",
},
wales: {
applicable: false,
alternative_url: "/guidance-wales",
alternative_url: "/publication-wales",
},
})
expect(devolved_nations_helper.applicable_nations_title_text).to eql("England")
expect(devolved_nations_helper.nations_with_urls[:northern_ireland][:alternative_url]).to eql("/guidance-northern-ireland")
expect(devolved_nations_helper.nations_with_urls[:scotland][:alternative_url]).to eql("/guidance-scotland")
expect(devolved_nations_helper.nations_with_urls[:wales][:alternative_url]).to eql("/guidance-wales")
expect(devolved_nations_helper.nations_with_urls[:northern_ireland][:alternative_url]).to eql("/publication-northern-ireland")
expect(devolved_nations_helper.nations_with_urls[:scotland][:alternative_url]).to eql("/publication-scotland")
expect(devolved_nations_helper.nations_with_urls[:wales][:alternative_url]).to eql("/publication-wales")
end

it "returns title text, which contains one nation, and guidance urls for three nations, correctly" do
it "returns title text, which contains one nation, and publication urls for three nations, correctly" do
devolved_nations_helper = GovukPublishingComponents::Presenters::DevolvedNationsHelper.new(national_applicability: {
england: {
applicable: true,
},
northern_ireland: {
applicable: false,
alternative_url: "/guidance-northern-ireland",
alternative_url: "/publication-northern-ireland",
},
scotland: {
applicable: true,
Expand All @@ -77,7 +77,58 @@
},
})
expect(devolved_nations_helper.applicable_nations_title_text).to eql("England, Scotland and Wales")
expect(devolved_nations_helper.nations_with_urls[:northern_ireland][:alternative_url]).to eql("/guidance-northern-ireland")
expect(devolved_nations_helper.nations_with_urls[:northern_ireland][:alternative_url]).to eql("/publication-northern-ireland")
end

it "returns title text, which contains one nation, and consultation url for one nation, correctly" do
devolved_nations_helper = GovukPublishingComponents::Presenters::DevolvedNationsHelper.new(
national_applicability: {
england: {
applicable: true,
},
northern_ireland: {
applicable: false,
alternative_url: "/consultation-northern-ireland",
},
},
type: "consultation",
)
expect(devolved_nations_helper.applicable_nations_title_text).to eql("England")
expect(devolved_nations_helper.alternative_content_text("northern_ireland")).to eql("Consultation for Northern Ireland")
end

it "returns title text, which contains one nation, and guidance url for one nation, correctly" do
devolved_nations_helper = GovukPublishingComponents::Presenters::DevolvedNationsHelper.new(
national_applicability: {
england: {
applicable: true,
},
northern_ireland: {
applicable: false,
alternative_url: "/guidance-northern-ireland",
},
},
type: "detailed_guide",
)
expect(devolved_nations_helper.applicable_nations_title_text).to eql("England")
expect(devolved_nations_helper.alternative_content_text("northern_ireland")).to eql("Guidance for Northern Ireland")
end

it "returns title text, which contains one nation, and guidance url for one nation, when invalid type provided, correctly" do
devolved_nations_helper = GovukPublishingComponents::Presenters::DevolvedNationsHelper.new(
national_applicability: {
england: {
applicable: true,
},
northern_ireland: {
applicable: false,
alternative_url: "/publication-northern-ireland",
},
},
type: "invalid_type",
)
expect(devolved_nations_helper.applicable_nations_title_text).to eql("England")
expect(devolved_nations_helper.alternative_content_text("northern_ireland")).to eql("Publication for Northern Ireland")
end
end
end

0 comments on commit d4d7d1c

Please sign in to comment.