mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Associations refactoring
* Move all associations related code from Serializer class to Associations module * Introduce Reflection class hierarchy * Introduce Association class * Rid off Serializer#each_association * Introduce Serializer#associations enumerator
This commit is contained in:
@@ -7,7 +7,7 @@ module ActiveModel
|
||||
def serializable_hash(options = nil)
|
||||
options ||= {}
|
||||
if serializer.respond_to?(:each)
|
||||
@result = serializer.map{|s| FlattenJson.new(s).serializable_hash(options) }
|
||||
@result = serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }
|
||||
else
|
||||
@hash = {}
|
||||
|
||||
@@ -15,24 +15,26 @@ module ActiveModel
|
||||
serializer.attributes(options)
|
||||
end
|
||||
|
||||
serializer.each_association do |key, association, opts|
|
||||
if association.respond_to?(:each)
|
||||
array_serializer = association
|
||||
@hash[key] = array_serializer.map do |item|
|
||||
serializer.associations.each do |association|
|
||||
serializer = association.serializer
|
||||
opts = association.options
|
||||
|
||||
if serializer.respond_to?(:each)
|
||||
array_serializer = serializer
|
||||
@hash[association.key] = array_serializer.map do |item|
|
||||
cache_check(item) do
|
||||
item.attributes(opts)
|
||||
end
|
||||
end
|
||||
else
|
||||
if association && association.object
|
||||
@hash[key] = cache_check(association) do
|
||||
association.attributes(options)
|
||||
@hash[association.key] =
|
||||
if serializer && serializer.object
|
||||
cache_check(serializer) do
|
||||
serializer.attributes(options)
|
||||
end
|
||||
elsif opts[:virtual_value]
|
||||
opts[:virtual_value]
|
||||
end
|
||||
elsif opts[:virtual_value]
|
||||
@hash[key] = opts[:virtual_value]
|
||||
else
|
||||
@hash[key] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
@result = @core.merge @hash
|
||||
|
||||
@@ -75,8 +75,10 @@ module ActiveModel
|
||||
end
|
||||
|
||||
serializers.each do |serializer|
|
||||
serializer.each_association do |key, association, opts|
|
||||
add_included(key, association, resource_path) if association
|
||||
serializer.associations.each do |association|
|
||||
serializer = association.serializer
|
||||
|
||||
add_included(association.key, serializer, resource_path) if serializer
|
||||
end if include_nested_assoc? resource_path
|
||||
end
|
||||
end
|
||||
@@ -131,22 +133,26 @@ module ActiveModel
|
||||
def add_resource_relationships(attrs, serializer, options = {})
|
||||
options[:add_included] = options.fetch(:add_included, true)
|
||||
|
||||
serializer.each_association do |key, association, opts|
|
||||
serializer.associations.each do |association|
|
||||
key = association.key
|
||||
serializer = association.serializer
|
||||
opts = association.options
|
||||
|
||||
attrs[:relationships] ||= {}
|
||||
|
||||
if association.respond_to?(:each)
|
||||
add_relationships(attrs, key, association)
|
||||
if serializer.respond_to?(:each)
|
||||
add_relationships(attrs, key, serializer)
|
||||
else
|
||||
if opts[:virtual_value]
|
||||
add_relationship(attrs, key, nil, opts[:virtual_value])
|
||||
else
|
||||
add_relationship(attrs, key, association)
|
||||
add_relationship(attrs, key, serializer)
|
||||
end
|
||||
end
|
||||
|
||||
if options[:add_included]
|
||||
Array(association).each do |association|
|
||||
add_included(key, association)
|
||||
Array(serializer).each do |serializer|
|
||||
add_included(key, serializer)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user