Module: Shamu::Services::RequestSupport

Extended by:
ActiveSupport::Concern
Included in:
Auditing::Support
Defined in:
lib/shamu/services/request_support.rb

Overview

Include into services that support mutating resources to add basic #with_request and Request conventions.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.request_class(method) ⇒ Class

Used to interrogate the service for the Shamu::Services::Request class to use for a given method.

Combine with Request#init_from to prepare a request for use in a rails form ready to modify an existing entity.

Parameters:

  • method (Symbol)

    on the service that will be called.

Returns:

  • (Class)

    a class that inherits from Request.



103
104
105
106
107
108
109
110
111
# File 'lib/shamu/services/request_support.rb', line 103

def request_class( method )
  result = request_class_by_name( method ) \
           || request_class_by_alias( method ) \
           || request_class_default

  result ||= superclass.request_class( method ) if superclass.respond_to?( :request_class )

  result || fail( IncompleteSetupError, "No Shamu::Services::Request classes defined for '#{ name }'." )
end

Instance Method Details

#request_class(method) ⇒ Class

Used to interrogate the service for the Shamu::Services::Request class to use for a given method.

Combine with Request#init_from to prepare a request for use in a rails form ready to modify an existing entity.

Parameters:

  • method (Symbol)

    on the service that will be called.

Returns:

  • (Class)

    a class that inherits from Request.



17
18
19
# File 'lib/shamu/services/request_support.rb', line 17

def request_class( method )
  self.class.request_class( method )
end

#request_for(method, entity = nil) ⇒ Request

Build a Shamu::Services::Request object, prepopulated with the current state of the resource to submit changes to the given method.

Parameters:

  • method (Symbol)

    that will be called with the generated request.

  • entity (Entities::Entity) (defaults to: nil)

    optional entity that will modified.

Returns:



27
28
29
30
31
32
# File 'lib/shamu/services/request_support.rb', line 27

def request_for( method, entity = nil )
  request    = request_class( method ).new( entity )
  request.id = entity.id if entity && request.attribute?( :id )

  request
end

#with_partial_request(params, request_class, &block) ⇒ Result

Behaves the same as #with_request but always executes the block even if the params are not yet valid. Allows the block to populate the request with missing information before validating.

Parameters:

  • params (Request, Hash)

    of the request.

  • request_class (Class)

    to coerce params to.

Returns:

See Also:



89
90
91
92
93
94
95
96
97
# File 'lib/shamu/services/request_support.rb', line 89

def with_partial_request( params, request_class, &block )
  request = request_class.coerce( params )
  sources = yield( request )

  result = Result.coerce( sources, request: request )
  request.complete( result.valid? )

  result
end

#with_request(params, request_class) {|request| ... } ⇒ Result

Respond to a Shamu::Services::Request returning a Shamu::Services::Result touple of the subject Entities::Entity and Shamu::Services::Request.

Before processing the params will be coerced and validated. If the request is invalid, the method will immediately return without yielding to the block.

If the block yields an Entities::Entity it will be assigned as the Shamu::Services::Result#entity in the returned Shamu::Services::Result object.

Examples:

def process_order( params )
  with_request params, ProcesOrderRequest do |request|
    order = Models::Order.find( request.id )

    # Custom validation
    next error( :base, "can't do that" ) if order.state == 'processed'

    request.apply_to( order )

    # If DB only validations fail, return errors
    next order unless order.save

    # All good, return an entity for the order
    scorpion.fetch OrderEntity, { order: order }, {}
  end
end

Parameters:

  • params (Request, Hash)

    of the request.

  • request_class (Class)

    to coerce params to.

Yields:

  • (request)

Yield Parameters:

  • request (Request)

    coerced and validated from params.

Yield Returns:

  • (Entities::Entity, #errors)

    the entity manipulated during the request or an object that responds to #errors.

Returns:



72
73
74
75
76
77
78
# File 'lib/shamu/services/request_support.rb', line 72

def with_request( params, request_class, &block )
  with_partial_request params, request_class do |request, *args|
    next unless request.valid?

    yield request, *args
  end
end