Skip to content

Commit

Permalink
Add warning when action/controller style links used (#483)
Browse files Browse the repository at this point in the history
Support for this format was dropped in version 5 and upgrading/reverting
is covered in the linked release notes.

* [x] Add specs
  • Loading branch information
peteryates authored Dec 18, 2023
2 parents 10a240d + bb2eec5 commit 8e16d84
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/helpers/govuk_link_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 }
22 changes: 22 additions & 0 deletions spec/helpers/govuk_link_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8e16d84

Please sign in to comment.