From bb2eec5791244ffe72130653308a60af8959b686 Mon Sep 17 00:00:00 2001 From: Peter Yates Date: Wed, 13 Dec 2023 11:22:00 +0000 Subject: [PATCH] Add warning when action/controller style links used Support for this format was dropped in version 5 and upgrading/reverting is covered in the linked release notes. --- app/helpers/govuk_link_helper.rb | 14 ++++++++++++++ spec/helpers/govuk_link_helper_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/app/helpers/govuk_link_helper.rb b/app/helpers/govuk_link_helper.rb index be2965f0..c18ff642 100644 --- a/app/helpers/govuk_link_helper.rb +++ b/app/helpers/govuk_link_helper.rb @@ -88,12 +88,18 @@ def button_attributes(disabled) end def extract_link_args(new_tab: false, inverse: false, muted: false, no_underline: false, no_visited_state: false, text_colour: false, **kwargs) + Rails.logger.warn(actions_warning_message(kwargs.fetch(:action))) if kwargs.key?(:action) + Rails.logger.warn(controller_warning_message(kwargs.fetch(:controller))) if kwargs.key?(:controller) + link_classes = extract_link_classes(inverse: inverse, muted: muted, no_underline: no_underline, no_visited_state: no_visited_state, text_colour: text_colour) { **link_classes, **new_tab_args(new_tab) }.deep_merge_html_attributes(kwargs) end def extract_button_link_args(new_tab: false, disabled: false, inverse: false, secondary: false, warning: false, **kwargs) + Rails.logger.warn(actions_warning_message(kwargs.fetch(:action))) if kwargs.key?(:action) + Rails.logger.warn(controller_warning_message(kwargs.fetch(:controller))) if kwargs.key?(:controller) + button_classes = extract_button_classes(inverse: inverse, secondary: secondary, warning: warning) { **button_classes, **button_attributes(disabled), **new_tab_args(new_tab) }.deep_merge_html_attributes(kwargs) @@ -139,6 +145,14 @@ def build_text(text, visually_hidden_prefix:, visually_hidden_suffix:) safe_join([govuk_visually_hidden(prefix), text, govuk_visually_hidden(suffix)].compact) end + + def actions_warning_message(value) + "action: '#{value}' parameter detected Support for old style controller/action links has been removed. See https://github.com/x-govuk/govuk-components/releases/tag/v5.0.0" + end + + def controller_warning_message(value) + "controller: '#{value}' parameter detected. Support for old style controller/action links has been removed. See https://github.com/x-govuk/govuk-components/releases/tag/v5.0.0" + end end ActiveSupport.on_load(:action_view) { include GovukLinkHelper } diff --git a/spec/helpers/govuk_link_helper_spec.rb b/spec/helpers/govuk_link_helper_spec.rb index ee4b3d65..b98ecfbe 100644 --- a/spec/helpers/govuk_link_helper_spec.rb +++ b/spec/helpers/govuk_link_helper_spec.rb @@ -141,6 +141,28 @@ def url_for(path) expect(subject).to have_tag("a", text: "hello", with: { href: "/world", class: "govuk-link", lang: "en-GB", dir: "ltr" }) end end + + context "when legacy arguments are passed" do + before { allow(Rails.logger).to receive(:warn).and_return(true) } + + describe "actions" do + let(:kwargs) { { action: "some-action" } } + + specify "triggers a warning about using legacy action: param" do + subject + expect(Rails.logger).to have_received(:warn).once.with(/action: 'some-action' parameter detected/) + end + end + + describe "controller" do + let(:kwargs) { { controller: "some-controller" } } + + specify "triggers a warning about using legacy controller: param" do + subject + expect(Rails.logger).to have_received(:warn).once.with(/controller: 'some-controller' parameter detected/) + end + end + end end describe "govuk_mail_to" do