Class: Wallaby::ActiveRecord::ModelServiceProvider

Inherits:
ModelServiceProvider
  • Object
show all
Defined in:
lib/adapters/wallaby/active_record/model_service_provider.rb,
lib/adapters/wallaby/active_record/model_service_provider/querier.rb,
lib/adapters/wallaby/active_record/model_service_provider/permitter.rb,
lib/adapters/wallaby/active_record/model_service_provider/validator.rb,
lib/adapters/wallaby/active_record/model_service_provider/normalizer.rb,
lib/adapters/wallaby/active_record/model_service_provider/querier/escaper.rb,
lib/adapters/wallaby/active_record/model_service_provider/querier/wrapper.rb,
lib/adapters/wallaby/active_record/model_service_provider/querier/transformer.rb

Overview

Model service provider for Wallaby::ActiveRecord

Defined Under Namespace

Classes: Normalizer, Permitter, Querier, Validator

Instance Method Summary collapse

Instance Method Details

#collection(params, authorizer) ⇒ ActiveRecord::Relation

Note:

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

Returns relation.

Parameters:

  • params (ActionController::Parameters)
  • authorizer (Ability)

    for now

Returns:

  • (ActiveRecord::Relation)

    relation



20
21
22
23
24
# File 'lib/adapters/wallaby/active_record/model_service_provider.rb', line 20

def collection(params, authorizer)
  query = querier.search(params)
  query = querier.sort(params[:sort], query)
  authorizer.accessible_for(:index, query)
end

#create(resource, params, authorizer) ⇒ Object

Assign resource with new values and store it in database as new record.

Parameters:

  • resource (Object)
  • params (ActionController::Parameters)
  • authorizer (Wallaby::ModelAuthorizer)


56
57
58
# File 'lib/adapters/wallaby/active_record/model_service_provider.rb', line 56

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

#destroy(resource, _params, _authorizer) ⇒ Object

Remove a record from database

Parameters:

  • resource (Object)


70
71
72
# File 'lib/adapters/wallaby/active_record/model_service_provider.rb', line 70

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

#find(id, _params, _authorizer) ⇒ Object

Note:

No mass assignment happens here!

Find the record using id.

Parameters:

  • id (Integer, String)

Returns:

  • (Object)

    persisted resource object

Raises:

  • (Wallaby::ResourceNotFound)

    when record is not found



46
47
48
49
50
# File 'lib/adapters/wallaby/active_record/model_service_provider.rb', line 46

def find(id, _params, _authorizer)
  @model_class.find(id)
rescue ::ActiveRecord::RecordNotFound
  raise ResourceNotFound, id
end

#new(_params, _authorizer) ⇒ Object

Note:

No mass assignment happens here!

Returns new resource object.

Returns:

  • (Object)

    new resource object



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

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

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

Returns paginated query.

Parameters:

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

Returns:

  • (ActiveRecord::Relation)

    paginated query



29
30
31
32
33
# File 'lib/adapters/wallaby/active_record/model_service_provider.rb', line 29

def paginate(query, params)
  per = (params[:per] || Wallaby.controller_configuration.try(:page_size)).to_i
  page = [params[:page].to_i, 1].max # starting from page 1
  query.offset((page - 1) * per).limit(per) # rubocop:disable CodeReuse/ActiveRecord
end

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

Returns allowlisted parameters.

Parameters:

  • params (ActionController::Parameters)
  • action (String, Symbol)
  • authorizer

Returns:

  • (ActionController::Parameters)

    allowlisted parameters



11
12
13
14
# File 'lib/adapters/wallaby/active_record/model_service_provider.rb', line 11

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

Assign resource with new values and store it in database as an update.

Parameters:

  • resource (Object)
  • params (ActionController::Parameters)
  • authorizer (Wallaby::ModelAuthorizer)


64
65
66
# File 'lib/adapters/wallaby/active_record/model_service_provider.rb', line 64

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