class JsonapiCompliable::Scoping::ExtraFields
Apply logic when an extra field is requested. Useful for eager loading associations used to compute the extra field.
Given a Resource
class PersonResource < ApplicationResource extra_field :net_worth do |scope| scope.includes(:assets) end end
And a corresponding serializer:
class SerializablePerson < JSONAPI::Serializable::Resource extra_attribute :net_worth do @object.assets.sum(&:value) end end
When the user requests the extra field 'net_worth':
GET /people?extra_fields[people]=net_worth
The assets
will be eager loaded and the 'net_worth' attribute will be present in the response. If this field is not explicitly requested, none of this logic fires.
Public Instance Methods
apply()
click to toggle source
Loop through all requested extra fields. If custom scoping logic is define for that field, run it. Otherwise, do nothing.
@return the scope object we are chaining/modofying
# File lib/jsonapi_compliable/scoping/extra_fields.rb, line 36 def apply each_extra_field do |callable| @scope = callable.call(@scope, resource.context) end @scope end
Private Instance Methods
each_extra_field() { |callable| ... }
click to toggle source
# File lib/jsonapi_compliable/scoping/extra_fields.rb, line 46 def each_extra_field resource.extra_fields.each_pair do |name, callable| if extra_fields.include?(name) yield callable end end end
extra_fields()
click to toggle source
# File lib/jsonapi_compliable/scoping/extra_fields.rb, line 54 def extra_fields query_hash[:extra_fields][resource.type] || [] end