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

Returns a new instance of Base.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/plutonium/action/base.rb', line 11

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
  @kanban_drop = options[:kanban_drop] || false
  @category = ActiveSupport::StringInquirer.new((options[:category] || :secondary).to_s)
  @position = options[:position] || 50
  @modal_mode = options[:modal]
  @modal_size = options[:size]
  @condition = options[:condition]
  validate_modal_mode!
  validate_modal_size!

  freeze
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



9
10
11
# File 'lib/plutonium/action/base.rb', line 9

def category
  @category
end

#colorObject (readonly)

Returns the value of attribute color.



9
10
11
# File 'lib/plutonium/action/base.rb', line 9

def color
  @color
end

#conditionObject (readonly)

Returns the value of attribute condition.



9
10
11
# File 'lib/plutonium/action/base.rb', line 9

def condition
  @condition
end

#confirmationObject (readonly)

Returns the value of attribute confirmation.



9
10
11
# File 'lib/plutonium/action/base.rb', line 9

def confirmation
  @confirmation
end

#descriptionObject (readonly)

Returns the value of attribute description.



9
10
11
# File 'lib/plutonium/action/base.rb', line 9

def description
  @description
end

#iconObject (readonly)

Returns the value of attribute icon.



9
10
11
# File 'lib/plutonium/action/base.rb', line 9

def icon
  @icon
end

#labelObject (readonly)

Returns the value of attribute label.



9
10
11
# File 'lib/plutonium/action/base.rb', line 9

def label
  @label
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/plutonium/action/base.rb', line 9

def name
  @name
end

#positionObject (readonly)

Returns the value of attribute position.



9
10
11
# File 'lib/plutonium/action/base.rb', line 9

def position
  @position
end

#return_toObject (readonly)

Returns the value of attribute return_to.



9
10
11
# File 'lib/plutonium/action/base.rb', line 9

def return_to
  @return_to
end

#route_optionsObject (readonly)

Returns the value of attribute route_options.



9
10
11
# File 'lib/plutonium/action/base.rb', line 9

def route_options
  @route_options
end

#turboObject (readonly)

Returns the value of attribute turbo.



9
10
11
# File 'lib/plutonium/action/base.rb', line 9

def turbo
  @turbo
end

Instance Method Details

#bulk_action?Boolean

Returns:

  • (Boolean)


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

def bulk_action? = @bulk_action

#collection_record_action?Boolean

Returns:

  • (Boolean)


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

def collection_record_action? = @collection_record_action

#condition_met?(view_context, record: nil) ⇒ Boolean

Display-only visibility gate, mirroring the condition: proc on inputs/displays/columns. Returns true when no condition is set.

The proc is evaluated against a ConditionContext: object/record is the contextual record (nil for resource/bulk actions), and every other call delegates to the view context (current_user, params, request, allowed_to?, resource_record!, …).

NOT an authorization boundary — a hidden action still has a live route; keep authorization in the policy.

Returns:

  • (Boolean)


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

def condition_met?(view_context, record: nil)
  return true if @condition.nil?
  ConditionContext.new(view_context, record).instance_exec(&@condition)
end

#kanban_drop?Boolean

True when this action was auto-registered for a kanban column's enter_interaction. Such actions exist only so their policy method, form, and params machinery are wired up — they are reachable by dropping a card, never rendered as a normal toolbar/row button.

Returns:

  • (Boolean)


76
# File 'lib/plutonium/action/base.rb', line 76

def kanban_drop? = @kanban_drop

Resolves to the definition's modal_mode when unset on the action.



39
40
41
42
# File 'lib/plutonium/action/base.rb', line 39

def modal_mode(definition = nil)
  return @modal_mode if @modal_mode || definition.nil?
  definition.modal_mode
end

Resolves to the definition's modal_size when unset on the action.



45
46
47
48
# File 'lib/plutonium/action/base.rb', line 45

def modal_size(definition = nil)
  return @modal_size if @modal_size || definition.nil?
  definition.modal_size
end

#permitted_by?(policy) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#record_action?Boolean

Returns:

  • (Boolean)


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

def record_action? = @record_action

#resource_action?Boolean

Returns:

  • (Boolean)


70
# File 'lib/plutonium/action/base.rb', line 70

def resource_action? = @resource_action

#turbo_frame(definition = nil) ⇒ Object

Downgrades the remote-modal frame to nil when the definition has modal false, so the link navigates as a full page instead of targeting a frame that won't exist. Other frames pass through.

The canonical :show action carries no explicit frame; instead it reads the definition's show_in so a resource can open its show page in a modal (show_in :modal) or full-page (:page, default) without the caller threading a frame. This is deliberately independent of modal_mode (which styles :new/:edit) — show is always centered.



59
60
61
62
63
64
65
# File 'lib/plutonium/action/base.rb', line 59

def turbo_frame(definition = nil)
  return nil if definition && targets_remote_modal? && definition.modal_mode == false
  if definition && name == :show && @turbo_frame.nil?
    return (definition.show_in == :modal) ? Plutonium::REMOTE_MODAL_FRAME : nil
  end
  @turbo_frame
end

#with(**overrides) ⇒ Object

Returns a new Action with the given options merged over this one.



98
99
100
# File 'lib/plutonium/action/base.rb', line 98

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