Skip to content

Commit

Permalink
Add govuk_footer_link_to helper
Browse files Browse the repository at this point in the history
Co-authored-by: paulrobertlloyd <me+git@paulrobertlloyd.com>
  • Loading branch information
peteryates and paulrobertlloyd committed Feb 23, 2024
1 parent ee6e044 commit b975d8e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
4 changes: 2 additions & 2 deletions app/components/govuk_component/footer_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/helpers/govuk_link_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion guide/content/components/footer.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
51 changes: 18 additions & 33 deletions guide/lib/examples/footer_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions spec/helpers/govuk_link_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b975d8e

Please sign in to comment.