Class: Wallaby::ModelServicer

Inherits:
Object
  • Object
show all
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.

Examples:

Create an application class for Admin Interface usage

class Admin::ApplicationServicer < Wallaby::ModelServicer
  base_class!
end

Instance Attribute Summary collapse

Attributes included from Baseable::ClassMethods

#base_class

Instance Method Summary collapse

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.

Parameters:

Raises:

  • (ArgumentError)

    if model_class is blank



46
47
48
49
50
51
52
53
# File 'lib/servicers/wallaby/model_servicer.rb', line 46

def initialize(model_class, authorizer, 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 = authorizer
  @provider = Map.service_provider_map(@model_class).new(@model_class, @model_decorator)
end

Instance Attribute Details

#authorizerModelAuthorizer (readonly)

Returns:

Since:

  • wallaby-5.2.0



30
31
32
# File 'lib/servicers/wallaby/model_servicer.rb', line 30

def authorizer
  @authorizer
end

#model_classClass (readonly)

Returns:

  • (Class)


20
21
22
# File 'lib/servicers/wallaby/model_servicer.rb', line 20

def model_class
  @model_class
end

#model_decoratorModelDecorator (readonly)

Returns:

Since:

  • wallaby-5.2.0



25
26
27
# File 'lib/servicers/wallaby/model_servicer.rb', line 25

def model_decorator
  @model_decorator
end

#providerModelServiceProvider (readonly)

Returns the instance that does the job.

Returns:

Since:

  • wallaby-5.2.0



35
36
37
# File 'lib/servicers/wallaby/model_servicer.rb', line 35

def provider
  @provider
end

Instance Method Details

#collection(params) ⇒ Enumerable

Note:

This is a template method that can be overridden by subclasses.

Return a collection by querying the datasource (e.g. database, REST API).

Parameters:

  • params (Hash, ActionController::Parameters)

Returns:

  • (Enumerable)

    list of resources



68
69
70
# File 'lib/servicers/wallaby/model_servicer.rb', line 68

def collection(params)
  provider.collection params, authorizer
end

#create(resource, params) ⇒ Object

Note:

This is a template method that can be overridden by subclasses.

To create a resource.

Parameters:

  • resource (Object)
  • params (ActionController::Parameters)

Returns:

  • (Object)

    created resource



102
103
104
# File 'lib/servicers/wallaby/model_servicer.rb', line 102

def create(resource, params)
  provider.create resource, params, authorizer
end

#destroy(resource, params = {}) ⇒ Object

Note:

This is a template method that can be overridden by subclasses.

To delete a resource.

Parameters:

  • resource (Object)
  • params (ActionController::Parameters) (defaults to: {})

Returns:

  • (Object)

    resource



120
121
122
# File 'lib/servicers/wallaby/model_servicer.rb', line 120

def destroy(resource, params = {})
  provider.destroy resource, params, authorizer
end

#find(id, params = {}) ⇒ Object

Note:

This is a template method that can be overridden by subclasses.

To find a resource.

Parameters:

  • id (Object)
  • params (ActionController::Parameters) (defaults to: {})

Returns:

  • (Object)

    found resource



93
94
95
# File 'lib/servicers/wallaby/model_servicer.rb', line 93

def find(id, params = {})
  provider.find id, params, authorizer
end

#new(params = {}) ⇒ Object

Note:

This is a template method that can be overridden by subclasses.

Initialize an instance of the model class.

Parameters:

  • params (ActionController::Parameters) (defaults to: {})

Returns:

  • (Object)

    initialized resource



84
85
86
# File 'lib/servicers/wallaby/model_servicer.rb', line 84

def new(params = {})
  provider.new params, authorizer
end

#paginate(query, params) ⇒ Enumerable

Note:

This is a template method that can be overridden by subclasses.

Paginate given #collection.

Parameters:

  • query (Enumerable)
  • params (ActionController::Parameters)

Returns:

  • (Enumerable)

    list of resources



78
# File 'lib/servicers/wallaby/model_servicer.rb', line 78

delegate :paginate, to: :provider

#permit(params, action = nil) ⇒ ActionController::Parameters

Note:

This is a template method that can be overridden by subclasses.

Allowlist parameters for mass assignment.

Parameters:

  • params (Hash, ActionController::Parameters)
  • action (String, Symbol, nil) (defaults to: nil)

Returns:

  • (ActionController::Parameters)

    permitted params



60
61
62
# File 'lib/servicers/wallaby/model_servicer.rb', line 60

def permit(params, action = nil)
  provider.permit params, action, authorizer
end

#update(resource, params) ⇒ Object

Note:

This is a template method that can be overridden by subclasses.

To update a resource.

Parameters:

  • resource (Object)
  • params (ActionController::Parameters)

Returns:

  • (Object)

    resource



111
112
113
# File 'lib/servicers/wallaby/model_servicer.rb', line 111

def update(resource, params)
  provider.update resource, params, authorizer
end

#userObject

Returns:

  • (Object)

Since:

  • wallaby-5.2.0



40
# File 'lib/servicers/wallaby/model_servicer.rb', line 40

delegate :user, to: :authorizer