Class: Wallaby::Her::ModelServiceProvider

Inherits:
ModelServiceProvider
  • Object
show all
Defined in:
lib/adapters/wallaby/her/model_service_provider.rb

Overview

Model service provider for HER ORM

Instance Method Summary collapse

Instance Method Details

#collection(_params, _authorizer) ⇒ ActiveRecord::Relation

No general practices of how ordering and searching can be done in Her. Therefore, it will just return all

NOTE: pagination free here. Since somewhere might need the collection without any pagination

Parameters:

  • _params (ActionController::Parameters)
  • _authorizer (Ability)

    for now

Returns:

  • (ActiveRecord::Relation)

See Also:

  • ModelServiceProvider#collection


26
27
28
# File 'lib/adapters/wallaby/her/model_service_provider.rb', line 26

def collection(_params, _authorizer)
  @model_class.all
end

#create(resource, params, authorizer) ⇒ Object

Parameters:

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

See Also:

  • ModelServiceProvider#create


58
59
60
# File 'lib/adapters/wallaby/her/model_service_provider.rb', line 58

def create(resource, params, authorizer)
  save __callee__, resource, params, authorizer
end

#destroy(resource, _params, _authorizer) ⇒ Object

Parameters:

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

See Also:

  • ModelServiceProvider#destroy


72
73
74
# File 'lib/adapters/wallaby/her/model_service_provider.rb', line 72

def destroy(resource, _params, _authorizer)
  resource.destroy
end

#find(id, _params, _authorizer) ⇒ Object

Parameters:

  • id (String)

Raises:

  • (ResourceNotFound)

See Also:

  • ModelServiceProvider#find


48
49
50
51
52
53
# File 'lib/adapters/wallaby/her/model_service_provider.rb', line 48

def find(id, _params, _authorizer)
  resource = @model_class.find id
  raise ResourceNotFound, id unless resource

  resource
end

#new(_params, _authorizer) ⇒ Object

See Also:

  • ModelServiceProvider#new


42
43
44
# File 'lib/adapters/wallaby/her/model_service_provider.rb', line 42

def new(_params, _authorizer)
  @model_class.new
end

#paginate(query, _params) ⇒ ActiveRecord::Relation

No general practices of how pagination can be done in Her. Therefore, it will just return all

Parameters:

  • query (ActiveRecord::Relation)
  • _params (ActionController::Parameters)

Returns:

  • (ActiveRecord::Relation)

    paginated query

See Also:

  • ModelServiceProvider#paginate


37
38
39
# File 'lib/adapters/wallaby/her/model_service_provider.rb', line 37

def paginate(query, _params)
  query.all
end

#permit(params, action, authorizer) ⇒ ActionController::Parameters

Only the general fields will be permitted. It’s less possible to guess what nested attributes should be permitted.

Parameters:

  • params (ActionController::Parameters)

Returns:

  • (ActionController::Parameters)

    whitelisted parameters

See Also:

  • ModelServiceProvider#permit


12
13
14
15
# File 'lib/adapters/wallaby/her/model_service_provider.rb', line 12

def permit(params, action, authorizer)
  authorized_fields = authorizer.permit_params action, @model_class
  params.require(param_key).permit(authorized_fields || permitted_fields)
end

#update(resource, params, authorizer) ⇒ Object

Parameters:

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

See Also:

  • ModelServiceProvider#update


65
66
67
# File 'lib/adapters/wallaby/her/model_service_provider.rb', line 65

def update(resource, params, authorizer)
  save __callee__, resource, params, authorizer
end