Clean up HasOne array wrapping logic

Introduce private _wrap_in_array option for now.
This commit is contained in:
Adrian Mugnolo and Santiago Pastorino 2013-12-17 15:39:15 -02:00 committed by Santiago Pastorino
parent 28fbb62ec4
commit f14f931957
3 changed files with 11 additions and 24 deletions

View File

@ -133,8 +133,7 @@ end
if association.embed_ids? if association.embed_ids?
hash[association.key] = serialize_ids association hash[association.key] = serialize_ids association
elsif association.embed_objects? elsif association.embed_objects?
associated_data = send(association.name) hash[association.embedded_key] = serialize association
hash[association.embedded_key] = serialize(association, associated_data)
end end
end end
end end
@ -150,14 +149,14 @@ end
associations.each_with_object({}) do |(name, association), hash| associations.each_with_object({}) do |(name, association), hash|
if included_associations.include? name if included_associations.include? name
if association.embed_in_root? if association.embed_in_root?
associated_data = Array(send(association.name)) hash[association.root_key] = serialize association
hash[association.root_key] = serialize(association, associated_data)
end end
end end
end end
end end
def serialize(association, object) def serialize(association)
object = send(association.name)
association.build_serializer(object, scope: scope).serializable_object association.build_serializer(object, scope: scope).serializable_object
end end
@ -174,6 +173,7 @@ end
return nil if object.nil? return nil if object.nil?
hash = attributes hash = attributes
hash.merge! associations hash.merge! associations
@options[:_wrap_in_array] ? [hash] : hash
end end
alias_method :serializable_object, :serializable_hash alias_method :serializable_object, :serializable_hash
end end

View File

@ -38,13 +38,6 @@ module ActiveModel
@serializer_class.new(object, options.merge(@options)) @serializer_class.new(object, options.merge(@options))
end end
private
def use_array_serializer!
@options.merge!(each_serializer: @serializer_class)
@serializer_class = ArraySerializer
end
class HasOne < Association class HasOne < Association
def initialize(name, *args) def initialize(name, *args)
super super
@ -53,12 +46,8 @@ module ActiveModel
end end
def build_serializer(object, options = {}) def build_serializer(object, options = {})
if object.respond_to?(:to_ary) && !(@serializer_class && @serializer_class <= ArraySerializer) @serializer_class ||= Serializer.serializer_for(object) || DefaultSerializer
use_array_serializer! options[:_wrap_in_array] = embed_in_root?
else
@serializer_class ||= Serializer.serializer_for(object) || DefaultSerializer
end
super super
end end
end end
@ -72,7 +61,8 @@ module ActiveModel
def build_serializer(object, options = {}) def build_serializer(object, options = {})
if @serializer_class && !(@serializer_class <= ArraySerializer) if @serializer_class && !(@serializer_class <= ArraySerializer)
use_array_serializer! @options[:each_serializer] = @serializer_class
@serializer_class = ArraySerializer
else else
@serializer_class ||= ArraySerializer @serializer_class ||= ArraySerializer
end end

View File

@ -11,11 +11,8 @@ module ActiveModel
def test_build_serializer_for_array_called_twice def test_build_serializer_for_array_called_twice
2.times do 2.times do
serializer = @association.build_serializer([@post]) serializer = @association.build_serializer(@post)
each_serializer = serializer.serializer_for(@post) assert_instance_of(PostSerializer, serializer)
assert_instance_of(ArraySerializer, serializer)
assert_instance_of(PostSerializer, each_serializer)
end end
end end
end end