lib/dato/json_api_serializer.rb in dato-0.6.12 vs lib/dato/json_api_serializer.rb in dato-0.6.14

- old
+ new

@@ -1,6 +1,7 @@ # frozen_string_literal: true +require 'dato/json_schema_relationships' module Dato class JsonApiSerializer attr_reader :link, :type @@ -49,20 +50,35 @@ relationships.each do |relationship, meta| if resource.key? relationship value = resource[relationship] data = if value - if meta[:collection] - value.map do |id| - { type: meta[:type], id: id.to_s } + if meta[:types].length > 1 + if meta[:collection] + value.map(&:symbolize_keys) + else + value.symbolize_keys end else - { type: meta[:type], id: value.to_s } + type = meta[:types].first + if meta[:collection] + value.map do |id| + { + type: type, + id: id.to_s + } + end + else + { + type: type, + id: value.to_s + } + end end end - result[relationship] = { data: data } + result[relationship] = { data: data } elsif required_relationships.include?(relationship) throw "Required attribute: #{relationship}" end end @@ -74,10 +90,11 @@ return resource.keys.map(&:to_sym) - %i[ item_type id created_at updated_at + creator ] end link_attributes['properties'].keys.map(&:to_sym) end @@ -87,45 +104,13 @@ (link_attributes.required || []).map(&:to_sym) end def relationships - if type == 'item' - if link.rel == :create - return { item_type: { collection: false, type: 'item_type' } } - else - {} - end - end - - return {} unless link_relationships - - link_relationships.properties.each_with_object({}) do |(relationship, schema), acc| - is_collection = schema.properties['data'].type.first == 'array' - - definition = if is_collection - schema.properties['data'].items - elsif schema.properties['data'].type.first == 'object' - schema.properties['data'] - else - schema.properties['data'].any_of.find do |option| - option.type.first == 'object' - end - end - - type = definition.properties['type'] - .pattern.source.gsub(/(^\^|\$$)/, '') - - acc[relationship.to_sym] = { - collection: is_collection, - type: type - } - end + @relationships ||= JsonSchemaRelationships.new(link_relationships).relationships end def required_relationships - return %i[item_type] if type == 'item' - (link_relationships.required || []).map(&:to_sym) end def link_attributes link.schema.properties['data'].properties['attributes']