Class: Plutonium::Action::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/plutonium/action/base.rb

Overview

Base class for all actions in the Plutonium framework.

Direct Known Subclasses

Interactive, Simple

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, **options) ⇒ Base

Initialize a new action.

Parameters:

  • name (Symbol)

    The name of the action.

  • options (Hash)

    The options for the action.

Options Hash (**options):

  • :label (String)

    The human-readable label for the action.

  • :description (String)

    The human-readable description for the action.

  • :icon (String)

    The icon associated with the action (e.g., ‘fa-edit’ for Font Awesome).

  • :color (Symbol)

    The color associated with the action (e.g., :primary, :secondary, :success, :warning, :danger).

  • :confirmation (String)

    The confirmation message to display before executing the action.

  • :route_options (RouteOptions, Hash)

    The routing options for the action.

  • :turbo_frame (String)

    The Turbo Frame ID for the action (used in Hotwire/Turbo Drive applications).

  • :return_to (String, Symbol)

    Override the return_to URL for this action. If not provided, defaults to current URL.

  • :bulk_action (Boolean) — default: false

    If true, applies to a bulk selection of records (e.g., “Mark Selected as Read”).

  • :collection_record_action (Boolean) — default: false

    If true, applies to records in a collection (e.g., “Edit Record” button in a table).

  • :record_action (Boolean) — default: false

    If true, applies to an individual record (e.g., “Delete” button on a Show page).

  • :resource_action (Boolean) — default: false

    If true, applies to the entire resource and can be used in any context (e.g., “Import from CSV”).

  • :category (Symbol)

    The category of the action. Determines visibility and grouping. Valid values include: @option options [Symbol] :primary Always shown and given prominence in the UI. @option options [Symbol] :secondary Shown in secondary menus or less prominent areas. @option options [Symbol] :danger Actions that require caution, often destructive operations.

  • :position (Integer) — default: 50

    The position of the action in its group. Lower numbers appear first.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/plutonium/action/base.rb', line 43

def initialize(name, **options)
  @name = name.to_sym
  @label = options[:label] || @name.to_s.titleize
  @description = options[:description]
  @icon = options[:icon] || Phlex::TablerIcons::ChevronRight
  @color = options[:color]
  @confirmation = options[:confirmation]
  @route_options = build_route_options(options[:route_options])
  @turbo = options[:turbo]
  @turbo_frame = options[:turbo_frame]
  @return_to = options[:return_to]
  @bulk_action = options[:bulk_action] || false
  @collection_record_action = options[:collection_record_action] || false
  @record_action = options[:record_action] || false
  @resource_action = options[:resource_action] || false
  @category = ActiveSupport::StringInquirer.new((options[:category] || :secondary).to_s)
  @position = options[:position] || 50
  @modal = options[:modal] || :centered
  validate_modal!

  freeze
end

Instance Attribute Details

#categorySymbol? (readonly)

The category of the action.

Returns:

  • (Symbol, nil)

    the current value of category



18
19
20
# File 'lib/plutonium/action/base.rb', line 18

def category
  @category
end

#colorSymbol? (readonly)

The color associated with the action.

Returns:

  • (Symbol, nil)

    the current value of color



18
19
20
# File 'lib/plutonium/action/base.rb', line 18

def color
  @color
end

#confirmationString? (readonly)

The confirmation message for the action.

Returns:

  • (String, nil)

    the current value of confirmation



18
19
20
# File 'lib/plutonium/action/base.rb', line 18

def confirmation
  @confirmation
end

#descriptionObject (readonly)

Returns the value of attribute description.



19
20
21
# File 'lib/plutonium/action/base.rb', line 19

def description
  @description
end

#iconString? (readonly)

The icon associated with the action.

Returns:

  • (String, nil)

    the current value of icon



18
19
20
# File 'lib/plutonium/action/base.rb', line 18

def icon
  @icon
end

#labelString (readonly)

The human-readable label for the action.

Returns:

  • (String)

    the current value of label



18
19
20
# File 'lib/plutonium/action/base.rb', line 18

def label
  @label
end

Returns the value of attribute modal.



19
20
21
# File 'lib/plutonium/action/base.rb', line 19

def modal
  @modal
end

#nameSymbol (readonly)

The name of the action.

Returns:

  • (Symbol)

    the current value of name



18
19
20
# File 'lib/plutonium/action/base.rb', line 18

def name
  @name
end

#positionInteger (readonly)

The position of the action within its category.

Returns:

  • (Integer)

    the current value of position



18
19
20
# File 'lib/plutonium/action/base.rb', line 18

def position
  @position
end

#return_toObject (readonly)

Returns the value of attribute return_to.



19
20
21
# File 'lib/plutonium/action/base.rb', line 19

def return_to
  @return_to
end

#route_optionsRouteOptions (readonly)

The routing options for the action.

Returns:



18
19
20
# File 'lib/plutonium/action/base.rb', line 18

def route_options
  @route_options
end

#turboObject (readonly)

Returns the value of attribute turbo.



19
20
21
# File 'lib/plutonium/action/base.rb', line 19

def turbo
  @turbo
end

#turbo_frameString? (readonly)

The Turbo Frame ID for the action.

Returns:

  • (String, nil)

    the current value of turbo_frame



18
19
20
# File 'lib/plutonium/action/base.rb', line 18

def turbo_frame
  @turbo_frame
end

Instance Method Details

#bulk_action?Boolean

Returns Whether this is a bulk action.

Returns:

  • (Boolean)

    Whether this is a bulk action.



67
68
69
# File 'lib/plutonium/action/base.rb', line 67

def bulk_action?
  @bulk_action
end

#collection_record_action?Boolean

Returns Whether this is a collection record action.

Returns:

  • (Boolean)

    Whether this is a collection record action.



72
73
74
# File 'lib/plutonium/action/base.rb', line 72

def collection_record_action?
  @collection_record_action
end

#permitted_by?(policy) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/plutonium/action/base.rb', line 86

def permitted_by?(policy)
  policy.allowed_to?(:"#{name}?")
end

#record_action?Boolean

Returns Whether this is a record action.

Returns:

  • (Boolean)

    Whether this is a record action.



77
78
79
# File 'lib/plutonium/action/base.rb', line 77

def record_action?
  @record_action
end

#resource_action?Boolean

Returns Whether this is a resource action.

Returns:

  • (Boolean)

    Whether this is a resource action.



82
83
84
# File 'lib/plutonium/action/base.rb', line 82

def resource_action?
  @resource_action
end

#with(**overrides) ⇒ Object

Returns a new Action with the given options merged over this one. Used by the resource definition to derive variants (e.g. dropping ‘turbo_frame` when `modal false` is configured) without mutating the frozen original.



94
95
96
# File 'lib/plutonium/action/base.rb', line 94

def with(**overrides)
  self.class.new(name, **to_options.merge(overrides))
end