Class: Cuprum::Rails::Actions::ResourceAction

Inherits:
Cuprum::Rails::Action show all
Includes:
ParameterValidation
Defined in:
lib/cuprum/rails/actions/resource_action.rb

Overview

Abstract base class for resourceful actions.

Each ResourceAction defines a series of steps used to validate and process the action. Each step is performed in order:

  • The #find_required_entities step locates any dependent entities and ensures that the entities exist. For example, it may find the requested entity for a show action, or the parent entity for a create action on a nested resource.

  • The #perform_action step contains the core logic of the action. For example, in a destroy action it would take the entity (located in the previous step) and remove it from the collection.

  • The #build_response step generates the result returned by a successful request. For example, for a show action on a nested resource, it might build a passing result with both the requested and parent resources.

If any of the steps fail, either by returning a failing result or by raising an exception, the action will immediately stop execution and return the result, or wrap the exception in a failing result with a Cuprum::Errors::UncaughtException error.

Direct Known Subclasses

Create, Destroy, Edit, Index, New, Show, Update

Instance Attribute Summary collapse

Attributes inherited from Cuprum::Rails::Action

#options, #repository, #request

Instance Method Summary collapse

Methods inherited from Cuprum::Rails::Action

#params

Instance Attribute Details

#resourceCuprum::Rails::Resource (readonly)

Returns the controller resource.

Returns:



55
56
57
# File 'lib/cuprum/rails/actions/resource_action.rb', line 55

def resource
  @resource
end

Instance Method Details

#call(request: , resource: , repository: nil, **options) ⇒ Cuprum::Result

Performs the controller action.

Subclasses should implement a #process method with the :request keyword, which accepts an ActionDispatch::Request instance.

Parameters:

  • request (ActionDispatch::Request) (defaults to: )

    the Rails request.

  • resource (Cuprum::Rails::Resource) (defaults to: )

    the controller resource.

  • repository (Cuprum::Collections::Repository) (defaults to: nil)

    the repository containing the data collections for the application or scope.

  • options (Hash<Symbol, Object>)

    additional options for the action.

Returns:

  • (Cuprum::Result)

    the result of the action.



# File 'lib/cuprum/rails/actions/resource_action.rb', line 33

#collectionCuprum::Rails::Collection

Returns the collection for the resource class.

Returns:



48
49
50
51
52
# File 'lib/cuprum/rails/actions/resource_action.rb', line 48

def collection
  @collection ||= repository.find_or_create(
    qualified_name: resource.qualified_name
  )
end

#resource_idObject

Returns the primary key for the resource.

Returns:

  • (Object)

    the primary key for the resource.



58
59
60
# File 'lib/cuprum/rails/actions/resource_action.rb', line 58

def resource_id
  @resource_id ||= params['id']
end

#resource_paramsHash

Returns the permitted params for the resource.

Returns:

  • (Hash)

    the permitted params for the resource.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cuprum/rails/actions/resource_action.rb', line 63

def resource_params
  return @resource_params if @resource_params

  resource_params = params.fetch(resource.singular_name, {})

  return resource_params unless resource_params.is_a?(Hash)

  @resource_params =
    resource_params
      .select { |key, _| permitted_attributes.include?(key) }
      .to_h
end