Class: IronAdmin::ToolAction

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_admin/tool_action.rb

Overview

Value object representing a declared action on a Tool.

Stores action metadata: display label, icon, confirmation settings, form fields to collect before execution, and authorization condition.

Examples:

ToolAction.new(
  name: :run_task,
  label: "Run Task",
  icon: "play",
  confirm: true,
  form_fields: [{ name: :task_name, type: :select, options: %w[db:migrate db:seed] }],
  condition: ->(user) { user.admin? }
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, label: nil, icon: nil, confirm: false, confirm_message: nil, form_fields: [], condition: nil) ⇒ ToolAction

Returns a new instance of ToolAction.



40
41
42
43
44
45
46
47
48
# File 'lib/iron_admin/tool_action.rb', line 40

def initialize(name:, label: nil, icon: nil, confirm: false, confirm_message: nil, form_fields: [], condition: nil)
  @name = name.to_sym
  @label = label || name.to_s.humanize
  @icon = icon
  @confirm = confirm
  @confirm_message = confirm_message
  @form_fields = coerce_form_fields(form_fields)
  @condition = condition
end

Instance Attribute Details

#conditionProc? (readonly)

Returns Condition proc that receives current_user, returns boolean.

Returns:

  • (Proc, nil)

    Condition proc that receives current_user, returns boolean



38
39
40
# File 'lib/iron_admin/tool_action.rb', line 38

def condition
  @condition
end

#confirmBoolean (readonly)

Returns Whether to show confirmation dialog.

Returns:

  • (Boolean)

    Whether to show confirmation dialog



29
30
31
# File 'lib/iron_admin/tool_action.rb', line 29

def confirm
  @confirm
end

#confirm_messageString? (readonly)

Returns Custom confirmation message.

Returns:

  • (String, nil)

    Custom confirmation message



32
33
34
# File 'lib/iron_admin/tool_action.rb', line 32

def confirm_message
  @confirm_message
end

#form_fieldsArray<ActionField> (readonly)

Returns Form fields to collect before execution.

Returns:

  • (Array<ActionField>)

    Form fields to collect before execution



35
36
37
# File 'lib/iron_admin/tool_action.rb', line 35

def form_fields
  @form_fields
end

#iconString? (readonly)

Returns Heroicon name.

Returns:

  • (String, nil)

    Heroicon name



26
27
28
# File 'lib/iron_admin/tool_action.rb', line 26

def icon
  @icon
end

#labelString (readonly)

Returns Display label.

Returns:

  • (String)

    Display label



23
24
25
# File 'lib/iron_admin/tool_action.rb', line 23

def label
  @label
end

#nameSymbol (readonly)

Returns Action method name.

Returns:

  • (Symbol)

    Action method name



20
21
22
# File 'lib/iron_admin/tool_action.rb', line 20

def name
  @name
end

Instance Method Details

#allowed?(user) ⇒ Boolean

Checks if the action is allowed for the given user.

Parameters:

  • user (Object, nil)

    Current user

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/iron_admin/tool_action.rb', line 59

def allowed?(user)
  return true if condition.nil?

  condition.call(user)
end

#has_form?Boolean

Returns Whether this action has form fields to collect.

Returns:

  • (Boolean)

    Whether this action has form fields to collect



51
52
53
# File 'lib/iron_admin/tool_action.rb', line 51

def has_form? # rubocop:disable Naming/PredicatePrefix
  form_fields.any?
end