Sha256: 6465d5fe6225acb2c4004a6cdcdeb2b3f974addfb74f615436bd0733a378a45e

Contents?: true

Size: 1.59 KB

Versions: 57

Compression:

Stored size: 1.59 KB

Contents

module Graphiti
  module JsonapiSerializableExt
    # This library looks up a serializer based on the record's class name
    # This wouldn't work for us, since a model may be associated with
    # multiple resources.
    # Instead, this variable is assigned when the query is resolved
    # To ensure we always render with the *resource* serializer
    module RendererOverrides
      def _build(object, exposures, klass)
        resource = object.instance_variable_get(:@__graphiti_resource)
        klass = object.instance_variable_get(:@__graphiti_serializer)
        klass.new(exposures.merge(object: object, resource: resource))
      end
    end

    # See above comment
    module RelationshipOverrides
      def data
        @_resources_block = proc do
          resources = yield
          if resources.nil?
            nil
          elsif resources.respond_to?(:to_ary)
            Array(resources).map do |obj|
              klass = obj.instance_variable_get(:@__graphiti_serializer)
              resource = obj.instance_variable_get(:@__graphiti_resource)
              klass.new(@_exposures.merge(object: obj, resource: resource))
            end
          else
            klass = resources.instance_variable_get(:@__graphiti_serializer)
            resource = resources.instance_variable_get(:@__graphiti_resource)
            klass.new(@_exposures.merge(object: resources, resource: resource))
          end
        end
      end
    end

    JSONAPI::Serializable::Relationship
      .send(:prepend, RelationshipOverrides)
    JSONAPI::Serializable::Renderer
      .send(:prepend, RendererOverrides)
  end
end

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
graphiti-1.0.rc.21 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.19 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.18 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.17 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.16 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.15 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.14 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.12 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.11 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.10 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.9 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.8 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.7 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.6 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.5 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.4 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.3 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.2 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.rc.1 lib/graphiti/jsonapi_serializable_ext.rb
graphiti-1.0.beta.23 lib/graphiti/jsonapi_serializable_ext.rb