Extract method to render an admin table action
This way it will be easier to change the behavior of all table actions, like adding ARIA attributes. In the past, when we changed the behavior of the `link_to` method, we had to change all table action classes.
This commit is contained in:
47
app/components/admin/action_component.rb
Normal file
47
app/components/admin/action_component.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
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 text
|
||||
options[:text] || t("admin.actions.#{action}")
|
||||
end
|
||||
|
||||
def path
|
||||
options[:path] || default_path
|
||||
end
|
||||
|
||||
def html_options
|
||||
{
|
||||
class: html_class,
|
||||
data: { confirm: confirmation_text }
|
||||
}.merge(options.except(:confirm, :path, :text))
|
||||
end
|
||||
|
||||
def html_class
|
||||
"#{action.to_s.gsub("_", "-")}-link"
|
||||
end
|
||||
|
||||
def confirmation_text
|
||||
if options[:confirm] == true
|
||||
t("admin.actions.confirm")
|
||||
else
|
||||
options[:confirm]
|
||||
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
|
||||
Reference in New Issue
Block a user