Remove duplication in poll actions tables
We were using the same logic in four different places, so we're creating a new class to handle that logic. Note that I didn't find a way to delegate the `content` method to a `Admin::TableActionsComponent`, so we're delegating the `action` method instead. That means we need to create a method returning an `Admin::TableActionsComponent`. We also need to cache this object; otherwise we were getting an error when calling `actions.action` from the `Admin::Poll::Questions::TableActionsComponent`.
This commit is contained in:
20
app/components/admin/allowed_table_actions_component.rb
Normal file
20
app/components/admin/allowed_table_actions_component.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
class Admin::AllowedTableActionsComponent < ApplicationComponent
|
||||
attr_reader :record, :options
|
||||
delegate :can?, to: :helpers
|
||||
delegate :action, to: :table_actions_component
|
||||
|
||||
def initialize(record, **options)
|
||||
@record = record
|
||||
@options = options
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def actions
|
||||
(options[:actions] || [:edit, :destroy]).select { |action| can?(action, record) }
|
||||
end
|
||||
|
||||
def table_actions_component
|
||||
@table_actions_component ||= Admin::TableActionsComponent.new(record, **options.merge(actions: actions))
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user