Sha256: 7126484ab2f8c290bba318affe8d1268fd09a90f789425659de448227ddccbf0
Contents?: true
Size: 1.74 KB
Versions: 30
Compression:
Stored size: 1.74 KB
Contents
module Graphiti class Serializer < JSONAPI::Serializable::Resource include Graphiti::Extensions::BooleanAttribute include Graphiti::Extensions::ExtraAttribute include Graphiti::SerializableHash prepend Graphiti::SerializableTempId # Keep track of what attributes have been applied by the Resource, # via .attribute, and which have been applied by a custom serializer # class/file. # This way, we can ensure attributes NOT applied by a resource still # go through type checking/coercion class_attribute :attributes_applied_via_resource class_attribute :extra_attributes_applied_via_resource self.attributes_applied_via_resource = [] self.extra_attributes_applied_via_resource = [] def self.inherited(klass) super klass.class_eval do extend JSONAPI::Serializable::Resource::ConditionalFields end end def as_jsonapi(*) super.tap do |hash| strip_relationships!(hash) if strip_relationships? end end # Temporary fix until fixed upstream # https://github.com/jsonapi-rb/jsonapi-serializable/pull/102 def requested_relationships(fields) @_relationships end # Allow access to resource methods def method_missing(id, *args, &blk) if @resource.respond_to?(id, true) @resource.send(id, *args, &blk) else super end end private def strip_relationships!(hash) hash[:relationships].select! do |name, payload| payload.has_key?(:data) end if hash[:relationships] end def strip_relationships? return false unless Graphiti.config.links_on_demand params = Graphiti.context[:object].params || {} [false, nil, 'false'].include?(params[:links]) end end end
Version data entries
30 entries across 30 versions & 1 rubygems