Class: Wallaby::ModelServicer
- Inherits:
-
Object
- Object
- Wallaby::ModelServicer
- Extended by:
- Baseable::ClassMethods
- Defined in:
- lib/servicers/wallaby/model_servicer.rb
Overview
This is the base servicer class to provider data source related operations for given/associated model. In general, it works together with #authorizer to ensure that all operations are legitmate.
For best practice, please create an application servicer class (see example) to better control the functions shared between different model servicers.
Instance Attribute Summary collapse
- #authorizer ⇒ ModelAuthorizer readonly
- #model_class ⇒ Class readonly
- #model_decorator ⇒ ModelDecorator readonly
-
#provider ⇒ ModelServiceProvider
readonly
The instance that does the job.
Attributes included from Baseable::ClassMethods
Instance Method Summary collapse
-
#collection(params) ⇒ Enumerable
Return a collection by querying the datasource (e.g. database, REST API).
-
#create(resource, params) ⇒ Object
To create a resource.
-
#destroy(resource, params = {}) ⇒ Object
To delete a resource.
-
#find(id, params = {}) ⇒ Object
To find a resource.
-
#initialize(model_class, authorizer, model_decorator) ⇒ ModelServicer
constructor
A new instance of ModelServicer.
-
#new(params = {}) ⇒ Object
Initialize an instance of the model class.
-
#paginate(query, params) ⇒ Enumerable
Paginate given #collection.
-
#permit(params, action = nil) ⇒ ActionController::Parameters
Allowlist parameters for mass assignment.
-
#update(resource, params) ⇒ Object
To update a resource.
- #user ⇒ Object
Methods included from Baseable::ClassMethods
base_class!, base_class?, namespace, namespace=
Constructor Details
#initialize(model_class, authorizer, model_decorator) ⇒ ModelServicer
Returns a new instance of ModelServicer.
46 47 48 49 50 51 52 53 |
# File 'lib/servicers/wallaby/model_servicer.rb', line 46 def initialize(model_class, , model_decorator) @model_class = model_class || self.class.model_class raise ArgumentError, 'Please provide a `model_class`.' unless @model_class @model_decorator = model_decorator @authorizer = @provider = Map.service_provider_map(@model_class).new(@model_class, @model_decorator) end |
Instance Attribute Details
#authorizer ⇒ ModelAuthorizer (readonly)
30 31 32 |
# File 'lib/servicers/wallaby/model_servicer.rb', line 30 def @authorizer end |
#model_class ⇒ Class (readonly)
20 21 22 |
# File 'lib/servicers/wallaby/model_servicer.rb', line 20 def model_class @model_class end |
#model_decorator ⇒ ModelDecorator (readonly)
25 26 27 |
# File 'lib/servicers/wallaby/model_servicer.rb', line 25 def model_decorator @model_decorator end |
#provider ⇒ ModelServiceProvider (readonly)
Returns the instance that does the job.
35 36 37 |
# File 'lib/servicers/wallaby/model_servicer.rb', line 35 def provider @provider end |
Instance Method Details
#collection(params) ⇒ Enumerable
This is a template method that can be overridden by subclasses.
Return a collection by querying the datasource (e.g. database, REST API).
68 69 70 |
# File 'lib/servicers/wallaby/model_servicer.rb', line 68 def collection(params) provider.collection params, end |
#create(resource, params) ⇒ Object
This is a template method that can be overridden by subclasses.
To create a resource.
102 103 104 |
# File 'lib/servicers/wallaby/model_servicer.rb', line 102 def create(resource, params) provider.create resource, params, end |
#destroy(resource, params = {}) ⇒ Object
This is a template method that can be overridden by subclasses.
To delete a resource.
120 121 122 |
# File 'lib/servicers/wallaby/model_servicer.rb', line 120 def destroy(resource, params = {}) provider.destroy resource, params, end |
#find(id, params = {}) ⇒ Object
This is a template method that can be overridden by subclasses.
To find a resource.
93 94 95 |
# File 'lib/servicers/wallaby/model_servicer.rb', line 93 def find(id, params = {}) provider.find id, params, end |
#new(params = {}) ⇒ Object
This is a template method that can be overridden by subclasses.
Initialize an instance of the model class.
84 85 86 |
# File 'lib/servicers/wallaby/model_servicer.rb', line 84 def new(params = {}) provider.new params, end |
#paginate(query, params) ⇒ Enumerable
This is a template method that can be overridden by subclasses.
Paginate given #collection.
78 |
# File 'lib/servicers/wallaby/model_servicer.rb', line 78 delegate :paginate, to: :provider |
#permit(params, action = nil) ⇒ ActionController::Parameters
This is a template method that can be overridden by subclasses.
Allowlist parameters for mass assignment.
60 61 62 |
# File 'lib/servicers/wallaby/model_servicer.rb', line 60 def permit(params, action = nil) provider.permit params, action, end |
#update(resource, params) ⇒ Object
This is a template method that can be overridden by subclasses.
To update a resource.
111 112 113 |
# File 'lib/servicers/wallaby/model_servicer.rb', line 111 def update(resource, params) provider.update resource, params, end |
#user ⇒ Object
40 |
# File 'lib/servicers/wallaby/model_servicer.rb', line 40 delegate :user, to: :authorizer |