Easier to work with polymorphic interface

This commit is contained in:
twinturbo 2012-08-25 17:39:39 +02:00
parent 655c4fe1c2
commit 38859d1f3d
2 changed files with 17 additions and 8 deletions

View File

@ -267,7 +267,10 @@ module ActiveModel
object = associated_object
if object && polymorphic?
{ polymorphic_key => find_serializable(object).serializable_hash }
{
:type => polymorphic_key,
polymorphic_key => find_serializable(object).serializable_hash
}
elsif object
find_serializable(object).serializable_hash
end
@ -283,7 +286,10 @@ module ActiveModel
object = associated_object
if object && polymorphic?
{ polymorphic_key => object.read_attribute_for_serialization(:id) }
{
:type => polymorphic_key,
:id => object.read_attribute_for_serialization(:id)
}
elsif object
object.read_attribute_for_serialization(:id)
else

View File

@ -1025,6 +1025,7 @@ class SerializerTest < ActiveModel::TestCase
:name => 'logo.png',
:url => 'http://example.com/logo.png',
:attachable => {
:type => :email,
:email => { :subject => 'foo', :body => 'bar' }
}
}, actual)
@ -1061,7 +1062,8 @@ class SerializerTest < ActiveModel::TestCase
:name => 'logo.png',
:url => 'http://example.com/logo.png',
:attachable => {
:email => 1
:type => :email,
:id => 1
}
}, actual)
end
@ -1099,7 +1101,8 @@ class SerializerTest < ActiveModel::TestCase
:name => 'logo.png',
:url => 'http://example.com/logo.png',
:attachable => {
:email => 1
:type => :email,
:id => 1
}},
:emails => [{
:id => 1,
@ -1179,15 +1182,15 @@ class SerializerTest < ActiveModel::TestCase
:oranges => [{
:plu => "3027",
:id => 1,
:readable => { :email => 1 }
:readable => { :type => :email, :id => 1 }
}],
:attachment => {
:name => 'logo.png',
:url => 'http://example.com/logo.png',
:attachable => { :email => 1 },
:readable => { :email => 1 },
:edible => { :orange => 1 }
:attachable => { :type => :email, :id => 1 },
:readable => { :type => :email, :id => 1 },
:edible => { :type => :orange, :id => 1 }
}
}, actual)
end