Files
grecia/app/components/admin/action_component.rb
Javi Martín 51a0bce58c Add information about budget actions
Both the calculate winners and delete actions benefit from some kind of
hint.

The "calculate winners" hint informs administrators that results won't
be publicly available unless the "show results" option is enabled.

The delete action was redirecting with an error message when the budget
couldn't be deleted; IMHO it's better to disable it and inform
administrators why it's disabled. Alternatively we could remove the
button completely; however, users might be looking for a way to delete a
budget and wouldn't find any hint about it.

We're now removing the "Delete" action from the budgets index table,
since most of the time it isn't possible to delete a budget and so the
action takes up space and we get little gain in return. We could keep
the "Delete" icon just for budgets which can be deleted; however, the
alignment of the table rows would suffer, making it harder to find the
intended action.
2021-10-25 18:34:17 +02:00

92 lines
2.2 KiB
Ruby

class Admin::ActionComponent < ApplicationComponent
include Admin::Namespace
attr_reader :action, :record, :options
def initialize(action, record, **options)
@action = action
@record = record
@options = options
end
private
def button?
options[:method] && options[:method] != :get
end
def text
action_key = if action == :destroy
:delete
else
action
end
options[:text] || t("admin.actions.#{action_key}")
end
def path
options[:path] || default_path
end
def html_options
{
class: html_class,
id: (dom_id(record, action) if record.respond_to?(:to_key)),
"aria-describedby": describedby,
"aria-label": label,
data: {
confirm: confirmation_text,
disable_with: (text if button?)
}
}.merge(options.except(:"aria-describedby", :"aria-label", :class, :confirm, :path, :text))
end
def html_class
"admin-action #{options[:class] || "#{action.to_s.gsub("_", "-")}-link"}".strip
end
def describedby
if options[:"aria-describedby"] == true
"#{dom_id(record, action)}_descriptor"
else
options[:"aria-describedby"]
end
end
def label
if options[:"aria-label"] == true
t("admin.actions.label", action: text, name: record_name)
else
options[:"aria-label"]
end
end
def confirmation_text
if options[:confirm] == true
if action == :destroy
t("admin.actions.confirm_delete", name: record_name)
else
t("admin.actions.confirm_action", action: text, name: record_name)
end
else
options[:confirm]
end
end
def record_name
if record.respond_to?(:human_name)
record.human_name
else
record.to_s.humanize
end
end
def default_path
if %i[answers configure destroy preview show].include?(action.to_sym)
namespaced_polymorphic_path(namespace, record)
else
namespaced_polymorphic_path(namespace, record, { action: action }.merge(request.query_parameters))
end
end
end