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

Instance Method Summary collapse

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

Parameters:

  • entity_class (Class)

    an Entities::Entity class to be loaded.

  • through (Symbol) (defaults to: nil)

    the name of the service to fetch the entity from. If not set, guesses the name of the service from the entity class.

  • as (Symbol) (defaults to: nil)

    the name of the method to expose the entity through.

  • list (Symbol) (defaults to: nil)

    the name of the method to expose the list of entities for index actions.

  • only (Array<Symbol>) (defaults to: nil)

    load the entity only for the given actions.

  • except (Array<Symbol>) (defaults to: nil)

    load the entity except for the given actions.

  • param (Symbol) (defaults to: :id)

    the request param that holds the id of the entity.

  • list_param (Symbol) (defaults to: nil)

    request param that hols list scope params.

  • param_key (Symbol) (defaults to: nil)

    request param that holds the attributes used to populate the service change request.

  • action (Symbol) (defaults to: nil)

    override the default action detection. For example always use :show for a secondary or root entity that is not being modified in an :update request.



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.

Parameters:

  • param_key (Symbol)

    key of the entity params to fetch.

Returns:

  • (Hash)

    the params



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