From b975d8e564bfc3b6f7e17fc27f3dbe1b35fdc76a Mon Sep 17 00:00:00 2001 From: Peter Yates Date: Fri, 23 Feb 2024 15:03:33 +0000 Subject: [PATCH] Add govuk_footer_link_to helper Co-authored-by: paulrobertlloyd --- .../govuk_component/footer_component.rb | 4 +- app/helpers/govuk_link_helper.rb | 6 +++ guide/content/components/footer.slim | 3 +- guide/lib/examples/footer_helpers.rb | 51 +++++++------------ spec/helpers/govuk_link_helper_spec.rb | 17 +++++++ 5 files changed, 45 insertions(+), 36 deletions(-) diff --git a/app/components/govuk_component/footer_component.rb b/app/components/govuk_component/footer_component.rb index 1421661e..79e639a0 100644 --- a/app/components/govuk_component/footer_component.rb +++ b/app/components/govuk_component/footer_component.rb @@ -67,9 +67,9 @@ def build_meta_links(links) case links when Array - links.map { |link| raw(link_to(link[:text], link[:href], class: "#{brand}-footer__link", **link.fetch(:attr, {}))) } + links.map { |link| govuk_footer_link_to(link[:text], link[:href], **link.fetch(:attr, {})) } when Hash - links.map { |text, href| raw(link_to(text, href, class: "#{brand}-footer__link")) } + links.map { |text, href| govuk_footer_link_to(text, href) } else fail(ArgumentError, 'meta links must be a hash or array of hashes') unless links.is_a?(Hash) end diff --git a/app/helpers/govuk_link_helper.rb b/app/helpers/govuk_link_helper.rb index dada74fe..84a098b5 100644 --- a/app/helpers/govuk_link_helper.rb +++ b/app/helpers/govuk_link_helper.rb @@ -57,6 +57,12 @@ def govuk_breadcrumb_link_to(name, href = nil, **kwargs, &block) link_to(name, href, **link_args, &block) end + def govuk_footer_link_to(name, href = nil, **kwargs, &block) + link_args = { class: "#{brand}-footer__link" }.deep_merge_html_attributes(kwargs) + + link_to(name, href, **link_args, &block) + end + def govuk_link_classes(inverse: false, muted: false, no_underline: false, no_visited_state: false, text_colour: false) if [text_colour, inverse, muted].count(true) > 1 fail("links can be only be one of text_colour, inverse or muted") diff --git a/guide/content/components/footer.slim b/guide/content/components/footer.slim index 8fd4c9ad..c70e4e4b 100644 --- a/guide/content/components/footer.slim +++ b/guide/content/components/footer.slim @@ -36,7 +36,8 @@ p The footer provides copyright, licensing and other information about your code: footer_with_custom_meta_html) do markdown: - The `meta_html` slot allows any custom HTML to be placed above the licence information. + The `meta_html` slot allows any custom HTML to be placed above the licence information. Use + the `govuk_footer_link_to` helper to build footer links. == render('/partials/example.*', caption: "Footer with entirely custom content", diff --git a/guide/lib/examples/footer_helpers.rb b/guide/lib/examples/footer_helpers.rb index 52f8ccb7..01bc06bd 100644 --- a/guide/lib/examples/footer_helpers.rb +++ b/guide/lib/examples/footer_helpers.rb @@ -35,7 +35,8 @@ def footer_with_custom_meta_html <<~FOOTER_META_HTML = render GovukComponent::FooterComponent.new do |footer| - footer.with_meta_html do - | Built with love by x-govuk. + | Built with love by + = govuk_footer_link_to("X-GOVUK", "https://x-govuk.github.io/") FOOTER_META_HTML end @@ -73,43 +74,27 @@ def footer_with_navigation h2.govuk-footer__heading.govuk-heading-m Section one ul.govuk-footer__list.govuk-footer__list--columns-2 - li: a.govuk-footer__link href="#" - | First - li: a.govuk-footer__link href="#" - | Second - li: a.govuk-footer__link href="#" - | Third - li: a.govuk-footer__link href="#" - | Fourth - li: a.govuk-footer__link href="#" - | Fifth - li: a.govuk-footer__link href="#" - | Sixth + li == govuk_footer_link_to("First", "#") + li == govuk_footer_link_to("Second", "#") + li == govuk_footer_link_to("Third", "#") + li == govuk_footer_link_to("Fourth", "#") + li == govuk_footer_link_to("Fifth", "#") + li == govuk_footer_link_to("Sixth", "#") .govuk-footer__section.govuk-grid-column-two-thirds h2.govuk-footer__heading.govuk-heading-m Section two ul.govuk-footer__list.govuk-footer__list--columns-3 - li: a.govuk-footer__link href="#" - | First - li: a.govuk-footer__link href="#" - | Second - li: a.govuk-footer__link href="#" - | Third - li: a.govuk-footer__link href="#" - | Fourth - li: a.govuk-footer__link href="#" - | Fifth - li: a.govuk-footer__link href="#" - | Sixth - li: a.govuk-footer__link href="#" - | Seventh - li: a.govuk-footer__link href="#" - | Eighth - li: a.govuk-footer__link href="#" - | Nineth - li: a.govuk-footer__link href="#" - | Tenth + li == govuk_footer_link_to("First", "#") + li == govuk_footer_link_to("Second", "#") + li == govuk_footer_link_to("Third", "#") + li == govuk_footer_link_to("Fourth", "#") + li == govuk_footer_link_to("Fifth", "#") + li == govuk_footer_link_to("Sixth", "#") + li == govuk_footer_link_to("Seventh", "#") + li == govuk_footer_link_to("Eighth", "#") + li == govuk_footer_link_to("Ninth", "#") + li == govuk_footer_link_to("Tenth", "#") FOOTER_WITH_NAVIGATION end end diff --git a/spec/helpers/govuk_link_helper_spec.rb b/spec/helpers/govuk_link_helper_spec.rb index 31684199..aad98308 100644 --- a/spec/helpers/govuk_link_helper_spec.rb +++ b/spec/helpers/govuk_link_helper_spec.rb @@ -657,4 +657,21 @@ def url_for(path) end end end + + describe "govuk_footer_link_to" do + let(:kwargs) { {} } + subject { govuk_footer_link_to("hello", "/world", **kwargs) } + + specify "creates a footer link with the correct class" do + expect(subject).to have_tag("a", with: { class: "govuk-footer__link", href: "/world" }, text: "hello") + end + + context "when extra classes and attributes are provided" do + let(:kwargs) { { class: "deep-orange", lang: "nl" } } + + specify "creates a footer link with the additional attributes and classes" do + expect(subject).to have_tag("a", with: { class: %w(govuk-footer__link deep-orange), href: "/world", lang: "nl" }, text: "hello") + end + end + end end