From af152c5e8ba6aa800d0e6136d3bae33d0c65a3c0 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Fri, 3 Jan 2025 14:41:18 +0100 Subject: [PATCH] Disable links instead of hiding Closes #49 --- app/helpers/application_helper.rb | 61 ++++++++++++++++---------- app/views/default/units/_unit.html.erb | 12 ++--- app/views/layouts/application.html.erb | 6 +-- app/views/units/_unit.html.erb | 4 +- 4 files changed, 50 insertions(+), 33 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 39e3c38..3a32aa6 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -138,29 +138,28 @@ module ApplicationHelper end.join.html_safe end - [:button_to, :link_to, :link_to_unless_current].each do |method_name| - class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 - def image_#{method_name}(name, image = nil, options = nil, html_options = {}, &block) - name = name.to_s - name = svg_tag("pictograms/\#{image}") + name if image + def image_button_to(name, image = nil, options = nil, html_options = {}) + name, html_options = link_or_button_options(:button, name, image, html_options) + button_to name, options, html_options + end - html_options[:class] = class_names( - html_options[:class], - 'button', - dangerous: html_options[:method] == :delete - ) - if html_options[:onclick]&.is_a? Hash - html_options[:onclick] = "return confirm('\#{html_options[:onclick][:confirm]}');" - end + def image_link_to(name, image = nil, options = nil, html_options = {}) + name, html_options = link_or_button_options(:link, name, image, html_options) + link_to name, options, html_options + end - if __method__.start_with?('image_link_to') && - !(html_options[:onclick] || html_options.dig(:data, :turbo_stream)) - name = name + '...' - end + DISABLED_ATTRIBUTES = {disabled: true, aria: {disabled: true}, tabindex: -1} - send :#{method_name}, name, options, html_options, &block - end - RUBY_EVAL + def image_button_to_if(condition, name, image = nil, options = nil, html_options = {}) + name, html_options = link_or_button_options(:button, name, image, html_options) + html_options = html_options.deep_merge DISABLED_ATTRIBUTES unless condition + button_to name, options, html_options + end + + def image_link_to_unless_current(name, image = nil, options = nil, html_options = {}) + name, html_options = link_or_button_options(:link, name, image, html_options) + html_options = html_options.deep_merge DISABLED_ATTRIBUTES if current_page?(options) + link_to name, options, html_options end def render_errors(record) @@ -188,7 +187,25 @@ module ApplicationHelper "Turbo.renderStreamMessage('#{j(render partial: partial, locals: locals)}'); return false;" end - def disabled_attributes(disabled) - disabled ? {disabled: true, aria: {disabled: true}, tabindex: -1} : {} + private + + def link_or_button_options(type, name, image = nil, html_options) + name = (image ? svg_tag("pictograms/#{image}") : '') + name.to_s + + html_options[:class] = class_names( + html_options[:class], + 'button', + dangerous: html_options[:method] == :delete + ) + + if html_options[:onclick]&.is_a? Hash + html_options[:onclick] = "return confirm('\#{html_options[:onclick][:confirm]}');" + end + + if type == :link && !(html_options[:onclick] || html_options.dig(:data, :turbo_stream)) + name += '...' + end + + [name, html_options] end end diff --git a/app/views/default/units/_unit.html.erb b/app/views/default/units/_unit.html.erb index 2fe42bc..810e803 100644 --- a/app/views/default/units/_unit.html.erb +++ b/app/views/default/units/_unit.html.erb @@ -7,17 +7,17 @@ <% unless unit.portable.nil? %> <% if unit.default? %> - <%= image_button_to t('.import'), 'download-outline', import_default_unit_path(unit), - disabled_attributes(!unit.portable?) %> + <%= image_button_to_if unit.portable?, t('.import'), 'download-outline', + import_default_unit_path(unit) %> <% end %> <% if current_user.at_least(:admin) %> <% if unit.default? %> - <%= image_button_to t('.delete'), 'delete-outline', default_unit_path(unit), - method: :delete, **disabled_attributes(!unit.movable?) %> + <%= image_button_to_if unit.movable?, t('.delete'), 'delete-outline', + default_unit_path(unit), method: :delete %> <% else %> - <%= image_button_to t('.export'), 'upload-outline', export_default_unit_path(unit), - disabled_attributes(!unit.portable?) %> + <%= image_button_to_if unit.portable?, t('.export'), 'upload-outline', + export_default_unit_path(unit) %> <% end %> <% end %> <% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index b5c2257..04665b2 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -29,7 +29,7 @@ class: "extendedright" %> <% if user_signed_in? %> <%= image_link_to_unless_current(current_user, "account-wrench-outline", - edit_user_registration_path) {} %> + edit_user_registration_path) %> <% if current_user_disguised? %> <%= image_link_to t(".revert"), "incognito-off", revert_users_path %> <% else %> @@ -37,9 +37,9 @@ method: :delete, data: {turbo: false} %> <% end %> <% else %> - <%= image_link_to_unless_current(t(:sign_in), "login", new_user_session_path) {} %> + <%= image_link_to_unless_current(t(:sign_in), "login", new_user_session_path) %> <%= image_link_to_unless_current(t(:register), "account-plus-outline", - new_user_registration_path) {} %> + new_user_registration_path) %> <% end %> diff --git a/app/views/units/_unit.html.erb b/app/views/units/_unit.html.erb index ed30e1e..d75a89c 100644 --- a/app/views/units/_unit.html.erb +++ b/app/views/units/_unit.html.erb @@ -17,8 +17,8 @@ id: dom_id(unit, :new, :link), onclick: 'this.blur();', data: {turbo_stream: true} %> <% end %> - <%= image_button_to t('.destroy'), 'delete-outline', unit_path(unit), - method: :delete, **disabled_attributes(!unit.movable?) %> + <%= image_button_to_if unit.movable?, t('.destroy'), 'delete-outline', unit_path(unit), + method: :delete %> <% if unit.movable? %> ⠿