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
@return [Class] the class of the action command.
@return [String, Symbol] the name of the action.
@return [Cuprum::Rails::Controllers::Configuration] the configuration for
the originating controller.
@return [Hash<Class, Object>, Hash<Symbol, Hash<Class, Object>>] the
serializers for converting result values into serialized data.
Public Class Methods
@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
Executes the controller action.
-
Initializes the action command with the resource.
-
Calls the command with the request.
-
Builds the responder with the resource and action metadata.
-
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
@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
# 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
# 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
# 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