Bugfix: include nested has_many association

Currently, doing `include: author.bio` would work correctly, but not for
has_many associations such as `include: author.roles`. This fixes it.

The problem was basically that we were not handling arrays for has_many linked,
as happens for ArraySerializers.
This commit is contained in:
Alexandre de Oliveira
2014-11-11 14:35:00 -02:00
parent 33e8d09ad0
commit 971f501e55
6 changed files with 59 additions and 5 deletions

View File

@@ -77,10 +77,13 @@ module ActiveModel
resource_path = [parent, resource].compact.join('.')
if include_assoc? resource_path
plural_name = resource.to_s.pluralize.to_sym
attrs = attributes_for_serializer(serializer, @options)
attrs = [attributes_for_serializer(serializer, @options)].flatten
@top[:linked] ||= {}
@top[:linked][plural_name] ||= []
@top[:linked][plural_name].push attrs unless @top[:linked][plural_name].include? attrs
attrs.each do |attrs|
@top[:linked][plural_name].push(attrs) unless @top[:linked][plural_name].include?(attrs)
end
end
serializer.each_association do |name, association, opts|
@@ -91,9 +94,19 @@ module ActiveModel
private
def attributes_for_serializer(serializer, options)
attributes = serializer.attributes(options)
attributes[:id] = attributes[:id].to_s if attributes[:id]
attributes
if serializer.respond_to?(:each)
result = []
serializer.each do |object|
attributes = object.attributes(options)
attributes[:id] = attributes[:id].to_s if attributes[:id]
result << attributes
end
else
result = serializer.attributes(options)
result[:id] = result[:id].to_s if result[:id]
end
result
end
def include_assoc?(assoc)