Sha256: ffee74c5c05136fc299a67ed6b0aa7ca2cf2a09b502e3f9d736bbd8d78ec853d
Contents?: true
Size: 1.58 KB
Versions: 40
Compression:
Stored size: 1.58 KB
Contents
module Graphiti module Util class RemoteSerializer def self.for(base, models) new(base).generate(models) end def initialize(base) @serializer = ::Class.new(base) @serializer.type { @object._type } end def generate(models) models.each do |model| model.to_h.each_pair do |key, value| if key == :_relationships add_relationships(value) elsif add_attribute?(model, key) @serializer.attribute(key) end end end post_process(@serializer, models) @serializer end private def add_relationships(relationship_hash) relationship_hash.each_pair do |name, reldata| @serializer.relationship(name.to_sym) end end def add_attribute?(model, name) disallow = [:_type, :id].include?(name) pre_existing = @serializer.attribute_blocks[name] is_relationship = model._relationships.try(:[], name.to_s) !disallow && !pre_existing && !is_relationship end def post_process(serializer, models) models.each do |model| model.delete_field(:_relationships) # If this isn't set, Array(resources) will return [] # This is important, because jsonapi-serializable makes this call # To see the test failure, remote resource position > department model.instance_variable_set(:@__graphiti_resource, 1) model.instance_variable_set(:@__graphiti_serializer, serializer) end end end end end
Version data entries
40 entries across 40 versions & 1 rubygems