Sha256: 8b0f24533626ba05fa57968adc2c1511bd4af4f2d495cd461b48353b4e6c9077

Contents?: true

Size: 1.96 KB

Versions: 14

Compression:

Stored size: 1.96 KB

Contents

require 'jsonapi/serializable/resource/conditional_fields'

module JsonapiCompliable
  module Extensions
    # Only render a given attribute when the user specifically requests it.
    # Useful for computationally-expensive attributes that are not required
    # on every request.
    #
    # This class handles the serialization, but you may also want to run
    # code during scoping (for instance, to eager load associations referenced
    # by this extra attribute. See (Resource.extra_field).
    #
    # @example Basic Usage
    #   # Will only be rendered on user request, ie
    #   # /people?extra_fields[people]=net_worth
    #   extra_attribute :net_worth
    #
    # @example Eager Loading
    #   class PersonResource < ApplicationResource
    #     # If the user requests the 'net_worth' attribute, make sure
    #     # 'assets' are eager loaded
    #     extra_field :net_worth do |scope|
    #       scope.includes(:assets)
    #     end
    #   end
    #
    #   class SerializablePerson < JSONAPI::Serializable::Resource
    #     # ... code ...
    #     extra_attribute :net_worth do
    #       @object.assets.sum(&:value)
    #     end
    #   end
    #
    # @see Resource.extra_field
    module ExtraAttribute
      def self.included(klass)
        klass.extend ClassMethods
      end

      module ClassMethods
        # @param [Symbol] name the name of the attribute
        # @param [Hash] options the options passed on to vanilla to .attribute
        def extra_attribute(name, options = {}, &blk)
          allow_field = proc {
            if options[:if]
              next false unless instance_eval(&options[:if])
            end

            @extra_fields[@_type] && @extra_fields[@_type].include?(name)
          }

          attribute name, if: allow_field, &blk
        end
      end
    end
  end
end

JSONAPI::Serializable::Resource.class_eval do
  prepend JSONAPI::Serializable::Resource::ConditionalFields
  include JsonapiCompliable::Extensions::ExtraAttribute
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
jsonapi_compliable-0.7.6 lib/jsonapi_compliable/extensions/extra_attribute.rb
jsonapi_compliable-0.7.5 lib/jsonapi_compliable/extensions/extra_attribute.rb
jsonapi_compliable-0.7.4 lib/jsonapi_compliable/extensions/extra_attribute.rb
jsonapi_compliable-0.7.3 lib/jsonapi_compliable/extensions/extra_attribute.rb
jsonapi_compliable-0.7.2 lib/jsonapi_compliable/extensions/extra_attribute.rb
jsonapi_compliable-0.6.13 lib/jsonapi_compliable/extensions/extra_attribute.rb
jsonapi_compliable-0.6.12 lib/jsonapi_compliable/extensions/extra_attribute.rb
jsonapi_compliable-0.6.11 lib/jsonapi_compliable/extensions/extra_attribute.rb
jsonapi_compliable-0.6.10 lib/jsonapi_compliable/extensions/extra_attribute.rb
jsonapi_compliable-0.6.9 lib/jsonapi_compliable/extensions/extra_attribute.rb
jsonapi_compliable-0.6.8 lib/jsonapi_compliable/extensions/extra_attribute.rb
jsonapi_compliable-0.6.7 lib/jsonapi_compliable/extensions/extra_attribute.rb
jsonapi_compliable-0.6.6 lib/jsonapi_compliable/extensions/extra_attribute.rb
jsonapi_compliable-0.6.5 lib/jsonapi_compliable/extensions/extra_attribute.rb