Merge pull request #1131 from beauby/jsonapi-include-tree

Extended format for JSONAPI `include` option
This commit is contained in:
Lucas Hosseini
2015-09-13 23:10:05 +02:00
12 changed files with 159 additions and 56 deletions

View File

@@ -7,11 +7,7 @@ class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapt
super
@hash = { data: [] }
@options[:include] ||= []
if @options[:include].is_a?(String)
@options[:include] = @options[:include].split(',')
end
@included = ActiveModel::Serializer::Utils.include_args_to_hash(@options[:include])
fields = options.delete(:fields)
if fields
@fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
@@ -117,48 +113,38 @@ class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapt
end
def included_for(serializer)
serializer.associations.flat_map { |assoc| _included_for(assoc.key, assoc.serializer) }.uniq
included = @included.flat_map do |inc|
association = serializer.associations.find { |assoc| assoc.key == inc.first }
_included_for(association.serializer, inc.second) if association
end
included.uniq
end
def _included_for(resource_name, serializer, parent = nil)
def _included_for(serializer, includes)
if serializer.respond_to?(:each)
serializer.flat_map { |s| _included_for(resource_name, s, parent) }.uniq
serializer.flat_map { |s| _included_for(s, includes) }.uniq
else
return [] unless serializer && serializer.object
result = []
resource_path = [parent, resource_name].compact.join('.')
if include_assoc?(resource_path)
primary_data = primary_data_for(serializer, @options)
relationships = relationships_for(serializer)
primary_data[:relationships] = relationships if relationships.any?
result.push(primary_data)
end
primary_data = primary_data_for(serializer, @options)
relationships = relationships_for(serializer)
primary_data[:relationships] = relationships if relationships.any?
if include_nested_assoc?(resource_path)
non_empty_associations = serializer.associations.select(&:serializer)
included = [primary_data]
non_empty_associations.each do |association|
result.concat(_included_for(association.key, association.serializer, resource_path))
result.uniq!
includes.each do |inc|
association = serializer.associations.find { |assoc| assoc.key == inc.first }
if association
included.concat(_included_for(association.serializer, inc.second))
included.uniq!
end
end
result
included
end
end
def include_assoc?(assoc)
check_assoc("#{assoc}$")
end
def include_nested_assoc?(assoc)
check_assoc("#{assoc}.")
end
def check_assoc(assoc)
@options[:include].any? { |s| s.match(/^#{assoc.gsub('.', '\.')}/) }
end
def add_links(options)
links = @hash.fetch(:links) { {} }
collection = serializer.object

View File

@@ -0,0 +1,35 @@
module ActiveModel::Serializer::Utils
module_function
# Translates a comma separated list of dot separated paths (JSONAPI format) into a Hash.
# Example: `'posts.author, posts.comments.upvotes, posts.comments.author'` would become `{ posts: { author: {}, comments: { author: {}, upvotes: {} } } }`.
#
# @param [String] included
# @return [Hash] a Hash representing the same tree structure
def include_string_to_hash(included)
included.delete(' ').split(',').inject({}) do |hash, path|
hash.deep_merge!(path.split('.').reverse_each.inject({}) { |a, e| { e.to_sym => a } })
end
end
# Translates the arguments passed to the include option into a Hash. The format can be either
# a String (see #include_string_to_hash), an Array of Symbols and Hashes, or a mix of both.
# Example: `posts: [:author, comments: [:author, :upvotes]]` would become `{ posts: { author: {}, comments: { author: {}, upvotes: {} } } }`.
#
# @param [Symbol, Hash, Array, String] included
# @return [Hash] a Hash representing the same tree structure
def include_args_to_hash(included)
case included
when Symbol
{ included => {} }
when Hash
included.each_with_object({}) { |(key, value), hash| hash[key] = include_args_to_hash(value) }
when Array
included.inject({}) { |a, e| a.merge!(include_args_to_hash(e)) }
when String
include_string_to_hash(included)
else
{}
end
end
end