Move polymorphic type into the serializable_hash

This commit is contained in:
Adman65 2011-12-12 14:33:18 +01:00
parent 9fa716e452
commit 638614ffde
2 changed files with 6 additions and 4 deletions

View File

@ -100,8 +100,12 @@ module ActiveModel
return unless object return unless object
if polymorphic? if polymorphic?
polymorphic_type = object.class.to_s.split('::').last
serializer_class = "#{object.class.to_s}Serializer".constantize serializer_class = "#{object.class.to_s}Serializer".constantize
serializer_class.new(object, scope).serializable_hash
serializer_class.new(object, scope).serializable_hash.merge({
"#{name}_type".to_sym => polymorphic_type
})
else else
serializer.new(object, scope).serializable_hash serializer.new(object, scope).serializable_hash
end end
@ -294,8 +298,6 @@ module ActiveModel
hash[association.key] = association.serialize(associated_object, scope) hash[association.key] = association.serialize(associated_object, scope)
if association.polymorphic? && associated_object if association.polymorphic? && associated_object
polymorphic_type = associated_object.class.to_s.split('::').last
hash["#{association.name}_type".to_sym] = polymorphic_type
end end
end end

View File

@ -703,8 +703,8 @@ class SerializerTest < ActiveModel::TestCase
serializer = polymorphic_serializer.new(blog, user) serializer = polymorphic_serializer.new(blog, user)
assert_equal({ assert_equal({
:writer_type => 'PolymorphicUser',
:writer => { :writer => {
:writer_type => 'PolymorphicUser',
:first_name => "Jose", :first_name => "Jose",
:last_name => "Valim" :last_name => "Valim"
} }