app/components/avo/actions_component.rb in avo-3.10.10 vs app/components/avo/actions_component.rb in avo-3.11.0
- old
+ new
@@ -1,88 +1,70 @@
# frozen_string_literal: true
class Avo::ActionsComponent < Avo::BaseComponent
include Avo::ApplicationHelper
- attr_reader :label, :size, :as_row_control
- def initialize(actions: [], resource: nil, view: nil, exclude: [], include: [], style: :outline, color: :primary, label: nil, size: :md, as_row_control: false, icon: nil)
- @actions = actions || []
- @resource = resource
- @view = view
- @exclude = Array(exclude)
- @include = include
- @color = color
- @style = style
- @label = label || I18n.t("avo.actions")
- @size = size
- @icon = icon
- @as_row_control = as_row_control
+ ACTION_FILTER = _Set(_Class(Avo::BaseAction))
+
+ prop :as_row_control, _Boolean, default: false
+ prop :icon, _Nilable(String)
+ prop :size, Avo::ButtonComponent::SIZE, default: :md
+ prop :title, _Nilable(String)
+ prop :color, Symbol, default: :primary
+ prop :include, _Nilable(ACTION_FILTER), default: [].freeze do |include|
+ Array(include).to_set
end
+ prop :label, String do |label|
+ label || I18n.t("avo.actions")
+ end
+ prop :style, Avo::ButtonComponent::STYLE, default: :outline
+ prop :actions, _Array(Avo::BaseAction), default: [].freeze
+ prop :exclude, _Nilable(ACTION_FILTER), default: [].freeze do |exclude|
+ Array(exclude).to_set
+ end
+ prop :resource, _Nilable(Avo::BaseResource)
+ prop :view, _Nilable(Symbol) do |view|
+ view&.to_sym
+ end
- def render?
- actions.present?
+ def after_initialize
+ filter_actions
end
- def actions
- if @exclude.present?
- @actions.reject { |action| action.class.in?(@exclude) }
- elsif @include.present?
- @actions.select { |action| action.class.in?(@include) }
- else
- @actions
- end
+ def render?
+ @actions.present?
end
- # When running an action for one record we should do it on a special path.
- # We do that so we get the `record` param inside the action so we can prefill fields.
- def action_path(action)
- return single_record_path(action) if as_row_control
- return many_records_path(action) unless @resource.has_record_id?
+ def filter_actions
+ @actions = @actions.dup
- if on_record_page?
- single_record_path action
- else
- many_records_path action
+ if @exclude.any?
+ @actions.reject! { |action| @exclude.include?(action.class) }
end
+
+ if @include.any?
+ @actions.select! { |action| @include.include?(action.class) }
+ end
end
# How should the action be displayed by default
def is_disabled?(action)
- return false if action.standalone || as_row_control
+ return false if action.standalone || @as_row_control
on_index_page?
end
private
def on_record_page?
- @view.in?(%w[show edit new])
+ @view.in?([:show, :edit, :new])
end
def on_index_page?
!on_record_page?
end
- def single_record_path(action)
- action_url(action, @resource.record_path)
- end
-
- def many_records_path(action)
- action_url(action, @resource.records_path)
- end
-
- def action_url(action, path)
- Avo::Services::URIService.parse(path)
- .append_paths("actions")
- .append_query(
- {
- action_id: action.to_param,
- arguments: Avo::BaseAction.encode_arguments(action.arguments)
- }.compact
- ).to_s
- end
-
def icon(action)
svg action.icon, class: "h-5 shrink-0 mr-1 inline pointer-events-none"
end
def render_item(action)
@@ -100,10 +82,10 @@
label = action.label.is_a?(Hash) ? action.label[:label] : nil
render Avo::DividerComponent.new(label)
end
def render_action_link(action)
- link_to action_path(action),
+ link_to action.link_arguments(resource: @resource).first,
data: action_data_attributes(action),
title: action.action_name,
class: action_css_class(action) do
raw("#{icon(action)} #{action.action_name}")
end