Add a :link_to_or_disabled helper method

This commit is contained in:
Cillian O'Ruanaidh
2025-08-29 14:21:14 +01:00
committed by Filipe
parent c057c72321
commit 6f7a547e15
4 changed files with 50 additions and 4 deletions

View File

@@ -1,6 +1,29 @@
# frozen_string_literal: true
module LinkHelper
def link_to_or_disabled(name = nil, options = nil, html_options = nil, &block)
html_options, options, name = options, name, block if block_given?
html_options ||= {}
if !!html_options.delete(:disabled)
# https://www.scottohara.me/blog/2021/05/28/disabled-links.html
html_options.merge!(
'aria-disabled': true,
class: (html_options[:class].to_s.split + ["disabled"]).uniq.join(" "),
role: "link"
)
if block_given?
content_tag("a", name, **html_options, &block)
else
content_tag("a", name, **html_options)
end
elsif block_given?
link_to options, html_options, &block
else
link_to name, options, html_options
end
end
def link_to_service(baseurl, name, html_options = {}, &)
return if name.blank?

View File

@@ -1,5 +1,3 @@
.row.links
%a.continue-shopping.button.secondary{ @insufficient_stock_lines.any? ? { disabled: "disabled" } : { href: current_shop_products_path } }
= t :orders_edit_continue
%a#checkout-link.button.primary.right{ @insufficient_stock_lines.any? ? { disabled: "disabled" } : { href: main_app.checkout_path } }
= t :orders_edit_checkout
= link_to_or_disabled t(:orders_edit_continue), current_shop_products_path, class: "continue-shopping button secondary", disabled: @insufficient_stock_lines.any?
= link_to_or_disabled t(:orders_edit_checkout), main_app.checkout_path, class: "button primary right", disabled: @insufficient_stock_lines.any?, id: "checkout-link"

View File

@@ -182,6 +182,10 @@ a.button {
text-decoration: none;
}
a[aria-disabled] {
pointer-events: none;
}
.button.disruptive::before {
margin-right: 3px;

View File

@@ -9,4 +9,25 @@ RSpec.describe LinkHelper do
expect(helper.ext_url("http://example.com/", "bla")).to eq("http://example.com/bla")
end
end
describe "link_to_or_disabled" do
it "behaves like the standard :link_to method e.g. it accepts the same arguments and accepts
blocks, etc." do
expect(helper.link_to_or_disabled("Go", "http://example.com/")).to eq(
"<a href=\"http://example.com/\">Go</a>"
)
expect(helper.link_to_or_disabled("Go", "http://example.com/", class: "button")).to eq(
"<a class=\"button\" href=\"http://example.com/\">Go</a>"
)
expect(helper.link_to_or_disabled("http://example.com/") { "Go" }).to eq(
"<a href=\"http://example.com/\">Go</a>"
)
end
it "accepts an additional boolean :disabled argument, which if true renders a disabled link" do
expect(helper.link_to_or_disabled("Go", "http://example.com/", disabled: true)).to eq(
"<a aria-disabled=\"true\" class=\"disabled\" role=\"link\">Go</a>"
)
end
end
end