Class: Wallaby::ActiveRecord::ModelServiceProvider
- Inherits:
-
ModelServiceProvider
- Object
- ModelServiceProvider
- Wallaby::ActiveRecord::ModelServiceProvider
- 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
-
#collection(params, authorizer) ⇒ ActiveRecord::Relation
Relation.
-
#create(resource, params, authorizer) ⇒ Object
Assign resource with new values and store it in database as new record.
-
#destroy(resource, _params, _authorizer) ⇒ Object
Remove a record from database.
-
#find(id, _params, _authorizer) ⇒ Object
Find the record using id.
-
#new(_params, _authorizer) ⇒ Object
New resource object.
-
#paginate(query, params) ⇒ ActiveRecord::Relation
Paginated query.
-
#permit(params, action, authorizer) ⇒ ActionController::Parameters
Allowlisted parameters.
-
#update(resource, params, authorizer) ⇒ Object
Assign resource with new values and store it in database as an update.
Instance Method Details
#collection(params, authorizer) ⇒ ActiveRecord::Relation
Pagination free here. Since somewhere might need the collection without any pagination
Returns relation.
20 21 22 23 24 |
# File 'lib/adapters/wallaby/active_record/model_service_provider.rb', line 20 def collection(params, ) query = querier.search(params) query = querier.sort(params[:sort], query) .accessible_for(:index, query) end |
#create(resource, params, authorizer) ⇒ Object
Assign resource with new values and store it in database as new record.
56 57 58 |
# File 'lib/adapters/wallaby/active_record/model_service_provider.rb', line 56 def create(resource, params, ) save(__callee__, resource, params, ) end |
#destroy(resource, _params, _authorizer) ⇒ Object
Remove a record from database
70 71 72 |
# File 'lib/adapters/wallaby/active_record/model_service_provider.rb', line 70 def destroy(resource, _params, ) resource.destroy end |
#find(id, _params, _authorizer) ⇒ Object
No mass assignment happens here!
Find the record using id.
46 47 48 49 50 |
# File 'lib/adapters/wallaby/active_record/model_service_provider.rb', line 46 def find(id, _params, ) @model_class.find(id) rescue ::ActiveRecord::RecordNotFound raise ResourceNotFound, id end |
#new(_params, _authorizer) ⇒ Object
No mass assignment happens here!
Returns new resource object.
37 38 39 |
# File 'lib/adapters/wallaby/active_record/model_service_provider.rb', line 37 def new(_params, ) @model_class.new end |
#paginate(query, params) ⇒ ActiveRecord::Relation
Returns 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.
11 12 13 14 |
# File 'lib/adapters/wallaby/active_record/model_service_provider.rb', line 11 def permit(params, action, ) = .permit_params(action, @model_class) params.require(param_key).permit( || permitted_fields) end |
#update(resource, params, authorizer) ⇒ Object
Assign resource with new values and store it in database as an update.
64 65 66 |
# File 'lib/adapters/wallaby/active_record/model_service_provider.rb', line 64 def update(resource, params, ) save(__callee__, resource, params, ) end |