Refactors of the Attribute adapter. Adds support for nested associations specified from the include key in the controller. Adds some tests and some method documentation

This commit is contained in:
L. Preston Sego III
2015-08-31 12:12:08 -04:00
parent 94cee192a9
commit a74ea189cd
5 changed files with 289 additions and 31 deletions

View File

@@ -9,40 +9,12 @@ module ActiveModel
def serializable_hash(options = nil)
options ||= {}
if serializer.respond_to?(:each)
result = serializer.map { |s| Attributes.new(s, instance_options).serializable_hash(options) }
serializable_hash_for_collection(options)
else
hash = {}
core = cache_check(serializer) do
serializer.attributes(options)
end
serializer.associations(@include_tree).each do |association|
serializer = association.serializer
association_options = association.options
if serializer.respond_to?(:each)
array_serializer = serializer
hash[association.key] = array_serializer.map do |item|
cache_check(item) do
item.attributes(association_options)
end
end
else
hash[association.key] =
if serializer && serializer.object
cache_check(serializer) do
serializer.attributes(options)
end
elsif association_options[:virtual_value]
association_options[:virtual_value]
end
end
end
result = core.merge hash
serializable_hash_for_single_resource(options)
end
result
end
def fragment_cache(cached_hash, non_cached_hash)
@@ -51,10 +23,43 @@ module ActiveModel
private
def serializable_hash_for_collection(options)
serializer.map { |s| Attributes.new(s, instance_options).serializable_hash(options) }
end
def serializable_hash_for_single_resource(options)
resource = resource_object_for(options)
relationships = resource_relationships(options)
resource.merge!(relationships)
end
def resource_relationships(options)
relationships = {}
serializer.associations(@include_tree).each do |association|
relationships[association.key] = relationship_value_for(association, options)
end
relationships
end
def relationship_value_for(association, options)
return association.options[:virtual_value] if association.options[:virtual_value]
return unless association.serializer && association.serializer.object
opts = instance_options.merge(include: @include_tree[association.key])
Attributes.new(association.serializer, opts).serializable_hash(options)
end
# no-op: Attributes adapter does not include meta data, because it does not support root.
def include_meta(json)
json
end
def resource_object_for(options)
cache_check(serializer) do
serializer.attributes(options)
end
end
end
end
end

View File

@@ -1,16 +1,48 @@
module ActiveModel
class Serializer
# TODO: description of this class, and overview of how it's used
class IncludeTree
module Parsing
module_function
# Translates a comma separated list of dot separated paths (JSON API 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)
# TODO: Needs comment walking through the process of what all this is doing.
included.delete(' ').split(',').reduce({}) do |hash, path|
include_tree = path.split('.').reverse_each.reduce({}) { |a, e| { e.to_sym => a } }
hash.deep_merge!(include_tree)
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: {} } } }`.
#
# @example
# `[:author, :comments => [:author]]`
#
# would become
#
# `{:author => {}, :comments => { author: {} } }`
#
# @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
@@ -47,6 +79,8 @@ module ActiveModel
# @return [IncludeTree]
#
def self.from_include_args(included)
return included if included.is_a?(IncludeTree)
new(Parsing.include_args_to_hash(included))
end