class Cuprum::Rails::ControllerAction

@api private

Implements a controller action.

@note This class should not be initialized directly. Instead, use the

Cuprum::Rails::Controller.action class method to define an action.

Attributes

action_class[R]

@return [Class] the class of the action command.

action_name[R]

@return [String, Symbol] the name of the action.

configuration[R]

@return [Cuprum::Rails::Controllers::Configuration] the configuration for

the originating controller.
serializers[R]

@return [Hash<Class, Object>, Hash<Symbol, Hash<Class, Object>>] the

serializers for converting result values into serialized data.

Public Class Methods

new( configuration, action_class:, action_name:, member_action: false, serializers: {} ) click to toggle source

@param configuration [Cuprum::Rails::Controllers::Configuration] the

configuration for the originating controller.

@param action_class [Class] The class of the action command. Must be

constructible with keyword :resource.

@param action_name [String, Symbol] The name of the action. @param member_action [Boolean] True if the action acts on a collection

item, not on the collection as a whole.

@param serializers

[Hash<Class, Object>, Hash<Symbol, Hash<Class, Object>>] The serializers
for converting result values into serialized data.
# File lib/cuprum/rails/controller_action.rb, line 27
def initialize(
  configuration,
  action_class:,
  action_name:,
  member_action: false,
  serializers:   {}
)
  @configuration = configuration
  @action_class  = action_class
  @action_name   = action_name
  @member_action = !!member_action # rubocop:disable Style/DoubleNegation
  @serializers   = serializers
end

Public Instance Methods

call(request) click to toggle source

Executes the controller action.

  1. Initializes the action command with the resource.

  2. Calls the command with the request.

  3. Builds the responder with the resource and action metadata.

  4. Calls the responder with the action result.

@param request [ActionDispatch::Request] The request to process.

@return [#call] The response object.

# File lib/cuprum/rails/controller_action.rb, line 83
def call(request)
  responder = build_responder(request)
  action    = action_class.new(resource: resource)
  result    = action.call(request: request)

  responder.call(result)
end
member_action?() click to toggle source

@return [Boolean] true if the action acts on a collection item, not on the

collection as a whole.
# File lib/cuprum/rails/controller_action.rb, line 93
def member_action?
  @member_action
end

Private Instance Methods

build_responder(request) click to toggle source
# File lib/cuprum/rails/controller_action.rb, line 99
def build_responder(request)
  responder_class = responder_for(request.format)

  responder_class.new(
    action_name:   action_name,
    member_action: member_action?,
    resource:      resource,
    serializers:   merge_serializers_for(request.format)
  )
end
merge_serializers_for(format) click to toggle source
# File lib/cuprum/rails/controller_action.rb, line 110
def merge_serializers_for(format)
  scoped_serializers(configuration.serializers, format: format)
    .merge(scoped_serializers(serializers, format: format))
end
scoped_serializers(serializers, format:) click to toggle source
# File lib/cuprum/rails/controller_action.rb, line 115
def scoped_serializers(serializers, format:)
  serializers
    .select { |key, _| key.is_a?(Class) }
    .merge(serializers.fetch(format, {}))
end