mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Add key option to serializer associations
This commit is contained in:
@@ -15,23 +15,23 @@ module ActiveModel
|
||||
serializer.attributes(options)
|
||||
end
|
||||
|
||||
serializer.each_association do |name, association, opts|
|
||||
serializer.each_association do |key, association, opts|
|
||||
if association.respond_to?(:each)
|
||||
array_serializer = association
|
||||
@hash[name] = array_serializer.map do |item|
|
||||
@hash[key] = array_serializer.map do |item|
|
||||
cache_check(item) do
|
||||
item.attributes(opts)
|
||||
end
|
||||
end
|
||||
else
|
||||
if association && association.object
|
||||
@hash[name] = cache_check(association) do
|
||||
@hash[key] = cache_check(association) do
|
||||
association.attributes(options)
|
||||
end
|
||||
elsif opts[:virtual_value]
|
||||
@hash[name] = opts[:virtual_value]
|
||||
@hash[key] = opts[:virtual_value]
|
||||
else
|
||||
@hash[name] = nil
|
||||
@hash[key] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -75,8 +75,8 @@ module ActiveModel
|
||||
end
|
||||
|
||||
serializers.each do |serializer|
|
||||
serializer.each_association do |name, association, opts|
|
||||
add_included(name, association, resource_path) if association
|
||||
serializer.each_association do |key, association, opts|
|
||||
add_included(key, association, resource_path) if association
|
||||
end if include_nested_assoc? resource_path
|
||||
end
|
||||
end
|
||||
@@ -131,22 +131,22 @@ module ActiveModel
|
||||
def add_resource_relationships(attrs, serializer, options = {})
|
||||
options[:add_included] = options.fetch(:add_included, true)
|
||||
|
||||
serializer.each_association do |name, association, opts|
|
||||
serializer.each_association do |key, association, opts|
|
||||
attrs[:relationships] ||= {}
|
||||
|
||||
if association.respond_to?(:each)
|
||||
add_relationships(attrs, name, association)
|
||||
add_relationships(attrs, key, association)
|
||||
else
|
||||
if opts[:virtual_value]
|
||||
add_relationship(attrs, name, nil, opts[:virtual_value])
|
||||
add_relationship(attrs, key, nil, opts[:virtual_value])
|
||||
else
|
||||
add_relationship(attrs, name, association)
|
||||
add_relationship(attrs, key, association)
|
||||
end
|
||||
end
|
||||
|
||||
if options[:add_included]
|
||||
Array(association).each do |association|
|
||||
add_included(name, association)
|
||||
add_included(key, association)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user