Includes links inside of linked resources

According to
http://jsonapi.org/format/#document-structure-resource-objects,

> Resource objects have the same internal structure, regardless of
> whether they represent primary or linked resources.

And then
http://jsonapi.org/format/#document-structure-resource-object-attributes,

> There are four reserved keys in resource objects:
>
> "id"
> "type"
> "href"
> "links"

This commits includes `links` inside of linked resources.
This commit is contained in:
Alexandre de Oliveira
2014-11-12 15:12:29 -02:00
parent 7592e838ee
commit 91b3fba509
5 changed files with 141 additions and 55 deletions

View File

@@ -19,69 +19,57 @@ module ActiveModel
else
@hash[@root] = attributes_for_serializer(serializer, @options)
serializer.each_association do |name, association, opts|
@hash[@root][:links] ||= {}
if association.respond_to?(:each)
add_links(name, association, opts)
else
add_link(name, association, opts)
end
end
add_resource_links(@hash[@root], serializer)
end
@hash
end
def add_links(name, serializers, options)
if serializers.first
type = serializers.first.object.class.to_s.underscore.pluralize
end
private
def add_links(resource, name, serializers)
type = serialized_object_type(serializers)
resource[:links] ||= {}
if name.to_s == type || !type
@hash[@root][:links][name] ||= []
@hash[@root][:links][name] += serializers.map{|serializer| serializer.id.to_s }
resource[:links][name] ||= []
resource[:links][name] += serializers.map{|serializer| serializer.id.to_s }
else
@hash[@root][:links][name] ||= {}
@hash[@root][:links][name][:type] = type
@hash[@root][:links][name][:ids] ||= []
@hash[@root][:links][name][:ids] += serializers.map{|serializer| serializer.id.to_s }
end
unless serializers.none? || @options[:embed] == :ids
serializers.each do |serializer|
add_linked(name, serializer)
end
resource[:links][name] ||= {}
resource[:links][name][:type] = type
resource[:links][name][:ids] ||= []
resource[:links][name][:ids] += serializers.map{|serializer| serializer.id.to_s }
end
end
def add_link(name, serializer, options)
def add_link(resource, name, serializer)
resource[:links] ||= {}
resource[:links][name] = nil
if serializer
type = serializer.object.class.to_s.underscore
type = serialized_object_type(serializer)
if name.to_s == type || !type
@hash[@root][:links][name] = serializer.id.to_s
resource[:links][name] = serializer.id.to_s
else
@hash[@root][:links][name] ||= {}
@hash[@root][:links][name][:type] = type
@hash[@root][:links][name][:id] = serializer.id.to_s
resource[:links][name] ||= {}
resource[:links][name][:type] = type
resource[:links][name][:id] = serializer.id.to_s
end
unless @options[:embed] == :ids
add_linked(name, serializer)
end
else
@hash[@root][:links][name] = nil
end
end
def add_linked(resource, serializer, parent = nil)
resource_path = [parent, resource].compact.join('.')
if include_assoc? resource_path
plural_name = resource.to_s.pluralize.to_sym
def add_linked(resource_name, serializer, parent = nil)
resource_path = [parent, resource_name].compact.join('.')
if include_assoc?(resource_path)
plural_name = resource_name.to_s.pluralize.to_sym
attrs = [attributes_for_serializer(serializer, @options)].flatten
@top[:linked] ||= {}
@top[:linked][plural_name] ||= []
attrs.each do |attrs|
add_resource_links(attrs, serializer, add_linked: false)
@top[:linked][plural_name].push(attrs) unless @top[:linked][plural_name].include?(attrs)
end
end
@@ -91,8 +79,6 @@ module ActiveModel
end if include_nested_assoc? resource_path
end
private
def attributes_for_serializer(serializer, options)
if serializer.respond_to?(:each)
result = []
@@ -124,6 +110,36 @@ module ActiveModel
s.match(/^#{assoc.gsub('.', '\.')}/)
end
end
def serialized_object_type(serializer)
return false unless Array(serializer).first
type_name = Array(serializer).first.object.class.to_s.underscore
if serializer.respond_to?(:first)
type_name.pluralize
else
type_name
end
end
def add_resource_links(attrs, serializer, options = {})
options[:add_linked] = options.fetch(:add_linked, true)
Array(serializer).first.each_association do |name, association, opts|
attrs[:links] ||= {}
if association.respond_to?(:each)
add_links(attrs, name, association)
else
add_link(attrs, name, association)
end
if @options[:embed] != :ids && options[:add_linked]
Array(association).each do |association|
add_linked(name, association)
end
end
end
end
end
end
end