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
-
.request_class(method) ⇒ Class
Used to interrogate the service for the Request class to use for a given method.
Instance Method Summary collapse
-
#request_class(method) ⇒ Class
Used to interrogate the service for the Request class to use for a given method.
-
#request_for(method, entity = nil) ⇒ Request
Build a Request object, prepopulated with the current state of the resource to submit changes to the given
method. -
#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.
-
#with_request(params, request_class) {|request| ... } ⇒ Result
Respond to a Request returning a Result touple of the subject Entities::Entity and Request.
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.
107 108 109 110 111 112 113 114 115 |
# File 'lib/shamu/services/request_support.rb', line 107 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.
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.
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.
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/shamu/services/request_support.rb', line 89 def with_partial_request( params, request_class, &block ) request = request_class.coerce( params ) # Make sure the request captures errors even if the block doesn't # check request.valid? sources = yield( request ) if sources.is_a?( Result ) sources else result request, *Array.wrap( sources ) end 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.
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| next unless request.valid? yield request end end |