FlattenJson adapter no longer inherits Json adapter

This commit is contained in:
Benjamin Fleischer 2015-09-04 04:09:58 -05:00
parent 24a5f3843e
commit ceef214f1e
3 changed files with 47 additions and 39 deletions

View File

@ -112,12 +112,14 @@ module ActiveModel
end end
end end
private
def meta def meta
serializer.meta if serializer.respond_to?(:meta) serializer.meta if serializer.respond_to?(:meta)
end end
def meta_key def meta_key
serializer.meta_key || 'meta' serializer.meta_key || 'meta'.freeze
end end
def root def root

View File

@ -1,6 +1,44 @@
class ActiveModel::Serializer::Adapter::FlattenJson < ActiveModel::Serializer::Adapter::Json class ActiveModel::Serializer::Adapter::FlattenJson < ActiveModel::Serializer::Adapter::Json
def serializable_hash(options = {}) def serializable_hash(options = nil)
super.each_value.first options ||= {}
if serializer.respond_to?(:each)
result = serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }
else
hash = {}
core = cache_check(serializer) do
serializer.attributes(options)
end
serializer.associations.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
end
result
end
def fragment_cache(cached_hash, non_cached_hash)
Json::FragmentCache.new.fragment_cache(cached_hash, non_cached_hash)
end end
private private

View File

@ -4,44 +4,12 @@ class ActiveModel::Serializer::Adapter::Json < ActiveModel::Serializer::Adapter
def serializable_hash(options = nil) def serializable_hash(options = nil)
options ||= {} options ||= {}
if serializer.respond_to?(:each) { root => FlattenJson.new(serializer).serializable_hash(options) }
result = serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }
else
hash = {}
core = cache_check(serializer) do
serializer.attributes(options)
end
serializer.associations.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
end
{ root => result }
end end
private
def fragment_cache(cached_hash, non_cached_hash) def fragment_cache(cached_hash, non_cached_hash)
ActiveModel::Serializer::Adapter::Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash) ActiveModel::Serializer::Adapter::Json::FragmentCache.new.fragment_cache(cached_hash, non_cached_hash)
end end
end end