Sha256: 1b0fba0aa72d9c93e3d34703244d9b411569f0832a876f5bb903a825dde828bf

Contents?: true

Size: 1.51 KB

Versions: 32

Compression:

Stored size: 1.51 KB

Contents

module JsonapiCompliable
  # 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.
  #
  # @see Resource.extra_field
  # @see Extensions::ExtraAttribute
  class Scoping::ExtraFields < Scoping::Base
    # 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
    def apply
      each_extra_field do |callable|
        @scope = callable.call(@scope)
      end

      @scope
    end

    private

    def each_extra_field
      resource.extra_fields.each_pair do |name, callable|
        if extra_fields.include?(name)
          yield callable
        end
      end
    end

    def extra_fields
      query_hash[:extra_fields][resource.type] || []
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
jsonapi_compliable-0.11.1 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.11.0 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.10.9 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.10.7 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.10.6 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.10.5 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.10.4 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.10.3 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.10.2 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.10.1 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.10.0 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.9.2 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.9.1 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.9.0 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.8.0 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.7.9 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.7.8 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.7.7 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.7.6 lib/jsonapi_compliable/scoping/extra_fields.rb
jsonapi_compliable-0.7.5 lib/jsonapi_compliable/scoping/extra_fields.rb