Module: Shamu::Rails::Entity
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/shamu/rails/entity.rb
Overview
Manages loading an entity as part of a controller action. See Entity.entity for details.
Class Method Summary collapse
-
.entity(entity_class, through: nil, as: nil, list: nil, only: nil, except: nil, param: :id, list_param: nil, action: nil, param_key: nil) ⇒ Object
Declare an entity dependency to be resolved before the requested controller action.
Instance Method Summary collapse
-
#request_params(param_key) ⇒ Hash
Get the raw request hash params for the given parameter key.
Class Method Details
.entity(entity_class, through: nil, as: nil, list: nil, only: nil, except: nil, param: :id, list_param: nil, action: nil, param_key: nil) ⇒ Object
Declare an entity dependency to be resolved before the requested controller action. Shamu will attempt to load an entity through the service and make it available to the controller as an attribute and a helper method.
Adds a method named after the entity excluding the namespace and "Entity" suffix (Users::UserEntity => #user). It also makes an entity_request method available for mutating actions such as new, create, update, edit, etc.
class UsersController < ApplicationController
service :users_service, Users::UsersService
entity Users::UserEntity
def show
render json: { name: user.name, id: user.id }
end
def update
result = users_service.update( user_request )
respond_with result
end
end
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/shamu/rails/entity.rb', line 115 def entity( entity_class, through: nil, as: nil, list: nil, only: nil, except: nil, param: :id, list_param: nil, action: nil, param_key: nil ) # rubocop:disable Metrics/LineLength as ||= entity_as_name( entity_class ) through ||= :"#{ as.to_s.pluralize }_service" list ||= as.to_s.pluralize.to_sym define_entity_method( as, through, param ) define_entities_method( list, through, list_param ) define_entity_request_method( as, through, param_key ) before_action do load_entity( method: as, list_method: list, action: action, only: only && Array( only ), except: except && Array( except ) ) end end |
Instance Method Details
#request_params(param_key) ⇒ Hash
Get the raw request hash params for the given parameter key.
36 37 38 39 40 41 42 43 |
# File 'lib/shamu/rails/entity.rb', line 36 def request_params( param_key ) strong_param = :"#{ param_key }_params" if respond_to?( strong_param, true ) send( strong_param ) else params[ param_key ] end end |