Class: Plutonium::Action::Interactive::Factory

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

Overview

Factory for creating Interactive actions

Class Method Summary collapse

Class Method Details

.build_route_options(name, action_type, immediate) ⇒ RouteOptions

Build the route options for the action

Parameters:

  • The name of the action

  • The type of the action

  • Whether the action is executed immediately

Returns:

  • The route options for the action



115
116
117
118
119
120
121
# File 'lib/plutonium/action/interactive.rb', line 115

def self.build_route_options(name, action_type, immediate)
  RouteOptions.new(
    method: immediate ? :post : :get,
    action: action_type,
    interactive_action: name
  )
end

.create(name, interaction:, **options) ⇒ Interactive

Create a new Interactive action based on the interaction type

Parameters:

  • The name of the action

  • The interaction class

  • Additional options for the action

Returns:

  • A new Interactive action instance



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

def self.create(name, interaction:, **options)
  attribute_names = symbolized_attribute_names(interaction)
  action_type = determine_action_type(attribute_names)
  input_fields = determine_input_fields(attribute_names)
  action_options = determine_action_options(action_type)
  immediate = options.fetch(:immediate) { input_fields.blank? }
  route_options = build_route_options(name, action_type, immediate)

  Interactive.new(
    name,
    interaction: interaction,
    immediate: immediate,
    route_options: route_options,
    turbo: interaction.turbo,
    **action_options,
    **options
  )
end

.determine_action_options(action_type) ⇒ Hash

Determine the action options based on the action type

Parameters:

  • The type of the action

Returns:

  • The action options



100
101
102
103
104
105
106
107
# File 'lib/plutonium/action/interactive.rb', line 100

def self.determine_action_options(action_type)
  {
    bulk_action: action_type == :interactive_collection_action,
    record_action: action_type == :interactive_record_action,
    collection_record_action: action_type == :interactive_record_action,
    resource_action: action_type == :interactive_resource_action
  }
end

.determine_action_type(attribute_names) ⇒ Symbol

Determine the action type based on the interaction’s attributes

Parameters:

  • Symbolized attribute names

Returns:

  • The determined action type



78
79
80
81
82
83
84
85
86
# File 'lib/plutonium/action/interactive.rb', line 78

def self.determine_action_type(attribute_names)
  if attribute_names.include?(:resource)
    :interactive_record_action
  elsif attribute_names.include?(:resources)
    :interactive_collection_action
  else
    :interactive_resource_action
  end
end

.determine_input_fields(attribute_names) ⇒ Array<Symbol>

Determine the input fields for the action

Parameters:

  • Symbolized attribute names

Returns:

  • The input fields



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

def self.determine_input_fields(attribute_names)
  attribute_names - [:resource, :resources]
end

.symbolized_attribute_names(interaction) ⇒ Array<Symbol>

Get symbolized attribute names for the interaction

Parameters:

  • The interaction class

Returns:

  • Symbolized attribute names



70
71
72
# File 'lib/plutonium/action/interactive.rb', line 70

def self.symbolized_attribute_names(interaction)
  interaction.attribute_names.map(&:to_sym)
end