Refactor adapters to implement support for array serialization

This commit is contained in:
Guillermo Iguaran
2014-10-15 17:35:50 -05:00
parent efea975419
commit 557b56a50e
11 changed files with 135 additions and 36 deletions

View File

@@ -3,18 +3,26 @@ module ActiveModel
class Adapter
class Json < Adapter
def serializable_hash(options = {})
@hash = serializer.attributes(options)
if serializer.respond_to?(:each)
@result = serializer.map{|s| self.class.new(s).serializable_hash }
else
@result = serializer.attributes(options)
serializer.each_association do |name, association, options|
if association.respond_to?(:each)
array_serializer = association
@hash[name] = array_serializer.map { |item| item.attributes(options) }
else
@hash[name] = association.attributes(options)
serializer.each_association do |name, association, opts|
if association.respond_to?(:each)
array_serializer = association
@result[name] = array_serializer.map { |item| item.attributes(opts) }
else
@result[name] = association.attributes(options)
end
end
end
@hash
if root = options.fetch(:root, serializer.json_key)
@result = { root => @result }
end
@result
end
end
end