From d72a8105260009558405920af89f0b77f4b4524e Mon Sep 17 00:00:00 2001 From: David Biddle Date: Fri, 5 Apr 2024 14:33:45 +0100 Subject: [PATCH] Use nested data attributes --- app/helpers/govuk_link_helper.rb | 8 +++++--- spec/helpers/govuk_link_helper_spec.rb | 15 ++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/helpers/govuk_link_helper.rb b/app/helpers/govuk_link_helper.rb index acebf5ad..4f600cc2 100644 --- a/app/helpers/govuk_link_helper.rb +++ b/app/helpers/govuk_link_helper.rb @@ -188,9 +188,11 @@ def controller_warning_message(value) def build_data_attributes(data_module, prevent_double_click: nil) { - "data-module": data_module, - "data-prevent-double-click": prevent_double_click - }.compact + data: { + module: data_module, + "prevent-double-click": prevent_double_click + }.compact + } end end diff --git a/spec/helpers/govuk_link_helper_spec.rb b/spec/helpers/govuk_link_helper_spec.rb index dd2cfde2..5fe28ac7 100644 --- a/spec/helpers/govuk_link_helper_spec.rb +++ b/spec/helpers/govuk_link_helper_spec.rb @@ -181,10 +181,10 @@ def url_for(path) end context "when there are custom attributes" do - let(:kwargs) { { lang: "en-GB", dir: "ltr" } } + let(:kwargs) { { lang: "en-GB", dir: "ltr", data: { module: "custom-module" } } } specify "the custom attributes are present on the link" do - expect(subject).to have_tag("a", text: "hello", with: { href: "/world", class: "govuk-link", lang: "en-GB", dir: "ltr" }) + expect(subject).to have_tag("a", text: "hello", with: { href: "/world", class: "govuk-link", lang: "en-GB", dir: "ltr", "data-module": "custom-module" }) end end @@ -314,10 +314,10 @@ def url_for(path) end context "when there are custom attributes" do - let(:kwargs) { { lang: "en-GB", dir: "ltr" } } + let(:kwargs) { { lang: "en-GB", dir: "ltr", data: { module: "custom-module" } } } specify "the custom attributes are present on the link" do - expect(subject).to have_tag("a", text: "hello", with: { href: "mailto:world@solar.system", class: "govuk-link", lang: "en-GB", dir: "ltr" }) + expect(subject).to have_tag("a", text: "hello", with: { href: "mailto:world@solar.system", class: "govuk-link", lang: "en-GB", dir: "ltr", "data-module": "custom-module" }) end end end @@ -502,10 +502,10 @@ def url_for(path) end context "when there are custom attributes" do - let(:kwargs) { { lang: "en-GB", dir: "ltr" } } + let(:kwargs) { { lang: "en-GB", dir: "ltr", data: { module: "custom-module" } } } specify "the custom attributes are present on the link" do - expect(subject).to have_tag("a", text: "hello", with: { href: "/world", class: "govuk-button", lang: "en-GB", dir: "ltr" }) + expect(subject).to have_tag("a", text: "hello", with: { href: "/world", class: "govuk-button", lang: "en-GB", dir: "ltr", "data-module": "custom-module" }) end end end @@ -661,7 +661,7 @@ def url_for(path) end context "when there are custom attributes" do - let(:kwargs) { { lang: "en-GB", dir: "ltr" } } + let(:kwargs) { { lang: "en-GB", dir: "ltr", data: { module: "custom-module" } } } specify "the custom attributes are present on the button" do expect(subject).to have_tag("form", with: { method: "post", action: "/world" }) do @@ -672,6 +672,7 @@ def url_for(path) class: %w[govuk-button], dir: "ltr", lang: "en-GB", + "data-module": "custom-module" } ) end