Class: JsonapiCompliable::Adapters::Abstract
- Inherits:
-
Object
- Object
- JsonapiCompliable::Adapters::Abstract
- Defined in:
- lib/jsonapi_compliable/adapters/abstract.rb
Overview
Adapters DRY up common resource logic.
For instance, there's no reason to write ActiveRecord logic like this in every Resource:
allow_filter :title do |scope, value|
scope.where(title: value)
end
sort do |scope, att, dir|
scope.order(att => dir)
end
paginate do |scope, current_page, per_page|
scope.page(current_page).per(per_page)
end
This logic can be re-used through an Adapter:
use_adapter JsonapiCompliable::Adapters::ActiveRecord
allow_filter :title
Adapters are pretty simple to write. The corresponding code for the above ActiveRecord adapter, which should look pretty familiar:
class JsonapiCompliable::Adapters::ActiveRecord
def filter(scope, attribute, value)
scope.where(attribute => value)
end
def order(scope, attribute, direction)
scope.order(attribute => direction)
end
def paginate(scope, current_page, per_page)
scope.page(current_page).per(per_page)
end
end
An adapter can have a corresponding sideloading_module
. This
module gets mixed in to a Sideload. In other words,
Resource is to Adapter as
Sideload is to *Adapter#sideloading_module*. Use this
module to define DSL methods that wrap #allow_sideload:
class MyAdapter < JsonapiCompliable::Adapters::Abstract
# ... code ...
def sideloading_module
MySideloadingAdapter
end
end
module MySideloadingAdapter
def belongs_to(association_name)
allow_sideload association_name do
# ... code ...
end
end
end
# And now in your Resource:
class MyResource < ApplicationResource
# ... code ...
use_adapter MyAdapter
belongs_to :my_association
end
If you need the adapter to do nothing, because perhaps the
API you are hitting does not support sorting, use
JsonapiCompliable::Adapters::Null
.
Direct Known Subclasses
Instance Method Summary collapse
-
#associate(parent, child, association_name, association_type) ⇒ Object
Assign these two objects together.
-
#average(scope, attr) ⇒ Float
The average of the scope.
-
#count(scope, attr) ⇒ Numeric
The count of the scope.
-
#create(model_class, create_params) ⇒ Object
The model instance just created.
-
#destroy(model_class, id) ⇒ Object
The model instance just destroyed.
-
#disassociate(parent, child, association_name, association_type) ⇒ Object
Remove the association without destroying objects.
-
#filter(scope, attribute, value) ⇒ Object
The scope.
-
#maximum(scope, attr) ⇒ Numeric
The maximum value of the scope.
-
#minimum(scope, attr) ⇒ Numeric
The maximum value of the scope.
-
#order(scope, attribute, direction) ⇒ Object
The scope.
-
#paginate(scope, current_page, per_page) ⇒ Object
The scope.
-
#resolve(scope) ⇒ Object
Resolve the scope.
-
#sideloading_module ⇒ Object
This module gets mixed in to Sideload classes This is where you define methods like has_many, belongs_to etc that wrap the lower-level Sideload#allow_sideload.
-
#sum(scope, attr) ⇒ Numeric
The sum of the scope.
-
#transaction(model_class) ⇒ Object
This method must
yield
the code to run within the transaction. -
#update(model_class, update_params) ⇒ Object
The model instance just created.
Instance Method Details
#associate(parent, child, association_name, association_type) ⇒ Object
Assign these two objects together.
association_name
and association_type
come from
your sideload configuration:
allow_sideload :the_name, type: the_type do
# ... code.
end
236 237 238 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 236 def associate(parent, child, association_name, association_type) raise 'you must override #associate in an adapter subclass' end |
#average(scope, attr) ⇒ Float
Returns the average of the scope
138 139 140 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 138 def average(scope, attr) raise 'you must override #average in an adapter subclass' end |
#count(scope, attr) ⇒ Numeric
Returns the count of the scope
127 128 129 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 127 def count(scope, attr) raise 'you must override #count in an adapter subclass' end |
#create(model_class, create_params) ⇒ Object
Returns the model instance just created
303 304 305 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 303 def create(model_class, create_params) raise 'you must override #create in an adapter subclass' end |
#destroy(model_class, id) ⇒ Object
Returns the model instance just destroyed
331 332 333 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 331 def destroy(model_class, id) raise 'you must override #destroy in an adapter subclass' end |
#disassociate(parent, child, association_name, association_type) ⇒ Object
Remove the association without destroying objects
This is NOT needed in the standard use case. The standard use case would be:
def update(attrs)
# attrs[:the_foreign_key] is nil, so updating the record disassociates
end
However, sometimes you need side-effect or elsewise non-standard behavior. Consider using acts_as_taggable_on} gem:
# Not actually needed, just an example
def disassociate(parent, child, association_name, association_type)
parent.tag_list.remove(child.name)
end
association_name
and association_type
come from
your sideload configuration:
allow_sideload :the_name, type: the_type do
# ... code.
end
276 277 278 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 276 def disassociate(parent, child, association_name, association_type) raise 'you must override #disassociate in an adapter subclass' end |
#filter(scope, attribute, value) ⇒ Object
Returns the scope
88 89 90 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 88 def filter(scope, attribute, value) raise 'you must override #filter in an adapter subclass' end |
#maximum(scope, attr) ⇒ Numeric
Returns the maximum value of the scope
160 161 162 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 160 def maximum(scope, attr) raise 'you must override #maximum in an adapter subclass' end |
#minimum(scope, attr) ⇒ Numeric
Returns the maximum value of the scope
171 172 173 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 171 def minimum(scope, attr) raise 'you must override #maximum in an adapter subclass' end |
#order(scope, attribute, direction) ⇒ Object
Returns the scope
101 102 103 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 101 def order(scope, attribute, direction) raise 'you must override #order in an adapter subclass' end |
#paginate(scope, current_page, per_page) ⇒ Object
Returns the scope
115 116 117 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 115 def paginate(scope, current_page, per_page) raise 'you must override #paginate in an adapter subclass' end |
#resolve(scope) ⇒ Object
Resolve the scope. This is where you'd actually fire SQL, actually make an HTTP call, etc.
210 211 212 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 210 def resolve(scope) scope end |
#sideloading_module ⇒ Object
This module gets mixed in to Sideload classes This is where you define methods like has_many, belongs_to etc that wrap the lower-level Sideload#allow_sideload
289 290 291 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 289 def sideloading_module Module.new end |
#sum(scope, attr) ⇒ Numeric
Returns the sum of the scope
149 150 151 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 149 def sum(scope, attr) raise 'you must override #sum in an adapter subclass' end |
#transaction(model_class) ⇒ Object
This method must yield
the code to run within the transaction.
This method should roll back the transaction if an error is raised.
187 188 189 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 187 def transaction(model_class) raise 'you must override #transaction in an adapter subclass, it must yield' end |
#update(model_class, update_params) ⇒ Object
Returns the model instance just created
317 318 319 |
# File 'lib/jsonapi_compliable/adapters/abstract.rb', line 317 def update(model_class, update_params) raise 'you must override #update in an adapter subclass' end |