Make Associations in root work with ArraySerializer

Closes #414
This commit is contained in:
Santiago Pastorino
2013-10-31 16:51:57 -02:00
parent 5a92e00b51
commit 5598bb0f79
2 changed files with 59 additions and 0 deletions

View File

@@ -37,5 +37,27 @@ module ActiveModel
end
end
alias_method :serializable_object, :serializable_array
def serializable_data
embedded_in_root_associations.merge!(super)
end
def embedded_in_root_associations
hash = {}
@object.map do |item|
serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
associations = serializer_class._associations
serializer = serializer_class.new(item, @options)
included_associations = serializer.filter(associations.keys)
associations.each do |(name, association)|
if included_associations.include? name
if association.embed_in_root?
hash[association.embedded_key] = serializer.serialize association
end
end
end
end
hash
end
end
end