mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Fix bugs with included resources
Make sure they're cached along with the including resource and remove duplicates
This commit is contained in:
parent
5e560ddbef
commit
9aebc6cb11
@ -5,8 +5,7 @@ module ActiveModel
|
|||||||
def initialize(serializer, options = {})
|
def initialize(serializer, options = {})
|
||||||
super
|
super
|
||||||
serializer.root = true
|
serializer.root = true
|
||||||
@hash = {}
|
@hash = { data: [] }
|
||||||
@top = @options.fetch(:top) { @hash }
|
|
||||||
|
|
||||||
if fields = options.delete(:fields)
|
if fields = options.delete(:fields)
|
||||||
@fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
|
@fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
|
||||||
@ -17,8 +16,14 @@ module ActiveModel
|
|||||||
|
|
||||||
def serializable_hash(options = {})
|
def serializable_hash(options = {})
|
||||||
if serializer.respond_to?(:each)
|
if serializer.respond_to?(:each)
|
||||||
@hash[:data] = serializer.map do |s|
|
serializer.each do |s|
|
||||||
self.class.new(s, @options.merge(top: @top, fieldset: @fieldset)).serializable_hash[:data]
|
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash
|
||||||
|
@hash[:data] << result[:data]
|
||||||
|
|
||||||
|
if result[:included]
|
||||||
|
@hash[:included] ||= []
|
||||||
|
@hash[:included] |= result[:included]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@hash = cached_object do
|
@hash = cached_object do
|
||||||
@ -53,14 +58,14 @@ module ActiveModel
|
|||||||
resource_path = [parent, resource_name].compact.join('.')
|
resource_path = [parent, resource_name].compact.join('.')
|
||||||
|
|
||||||
if include_assoc?(resource_path)
|
if include_assoc?(resource_path)
|
||||||
@top[:included] ||= []
|
@hash[:included] ||= []
|
||||||
|
|
||||||
serializers.each do |serializer|
|
serializers.each do |serializer|
|
||||||
attrs = attributes_for_serializer(serializer, @options)
|
attrs = attributes_for_serializer(serializer, @options)
|
||||||
|
|
||||||
add_resource_links(attrs, serializer, add_included: false)
|
add_resource_links(attrs, serializer, add_included: false)
|
||||||
|
|
||||||
@top[:included].push(attrs) unless @top[:included].include?(attrs)
|
@hash[:included].push(attrs) unless @hash[:included].include?(attrs)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user