Class: Cuprum::Rails::Actions::ResourceAction
- Inherits:
-
Cuprum::Rails::Action
- Object
- Command
- Cuprum::Rails::Action
- Cuprum::Rails::Actions::ResourceAction
- 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.
Instance Attribute Summary collapse
-
#resource ⇒ Cuprum::Rails::Resource
readonly
The controller resource.
Attributes inherited from Cuprum::Rails::Action
#options, #repository, #request
Instance Method Summary collapse
-
#call(request: , resource: , repository: nil, **options) ⇒ Cuprum::Result
Performs the controller action.
-
#collection ⇒ Cuprum::Rails::Collection
The collection for the resource class.
-
#resource_id ⇒ Object
The primary key for the resource.
-
#resource_params ⇒ Hash
The permitted params for the resource.
Methods inherited from Cuprum::Rails::Action
Instance Attribute Details
#resource ⇒ Cuprum::Rails::Resource (readonly)
Returns the controller resource.
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.
|
# File 'lib/cuprum/rails/actions/resource_action.rb', line 33
|
#collection ⇒ Cuprum::Rails::Collection
Returns the collection for the resource class.
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_id ⇒ Object
Returns 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_params ⇒ Hash
Returns 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 |