Files
grecia/app/components/admin/action_component.rb
Javi Martín 46d8bc4f0e Use a switch to enable/disable budget phases
In the past it would have been confusing to add a way to directly
enable/disable a phase in the phases table because it was in the middle
of the form. So we would have had next to each other controls that don't
do anything until the form is sent and controls which modify the
database immediately. That's why we couldn't add the checkboxes we used
when using the wizard.

Now the phases aren't on the same page as the budget form, so we can
edit them independently. We're using a switch, so it's consistent with
the way we enable/disable features. We could have used checkboxes, but
with checkboxes, users expect they aren't changing anything until they
click on a button to send the form, so we'd have to add a button, and it
might be missed since we're going to add "buttons" for headings and
groups to this page which won't send a form but will be links.

Since we're changing the element with JavaScript after an AJAX call, we
need a way to find the button we're changing. The easiest way is adding
an ID attribute to all admin actions buttons/links.
2021-10-25 18:01:47 +02:00

77 lines
1.8 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
options[:text] || t("admin.actions.#{action}")
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-label": label,
data: {
confirm: confirmation_text,
disable_with: (text if button?)
}
}.merge(options.except(:"aria-label", :class, :confirm, :path, :text))
end
def html_class
"admin-action #{options[:class] || "#{action.to_s.gsub("_", "-")}-link"}".strip
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