aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/serializers
diff options
context:
space:
mode:
authorSteve Klabnik <steve@steveklabnik.com>2012-10-05 15:07:39 -0600
committerSteve Klabnik <steve@steveklabnik.com>2012-11-28 11:48:32 -0800
commit9504b44cf60c1d69519cef32465f46a73719bc17 (patch)
treef3b5f0ae2c80cd9cf6e390a112f34d2f11e893fe /activemodel/test/cases/serializers
parent3e965e2144d515499c3c94de7973cf0bd84649e2 (diff)
downloadrails-9504b44cf60c1d69519cef32465f46a73719bc17.tar.gz
rails-9504b44cf60c1d69519cef32465f46a73719bc17.tar.bz2
rails-9504b44cf60c1d69519cef32465f46a73719bc17.zip
Specify type of singular association during serialization
When serialising a class, specify the type of any singular associations, if necessary. Rails already correctly specifies the :type of any enumerable association (e.g. a has_many association), but made no attempt to do so for non-enumerables (e.g. a has_one association). We must specify the :type of any STI association. A has_one association to a class which uses single-table inheritance is an example of this type of association. Fixes #7471
Diffstat (limited to 'activemodel/test/cases/serializers')
-rwxr-xr-xactivemodel/test/cases/serializers/xml_serialization_test.rb12
1 files changed, 10 insertions, 2 deletions
diff --git a/activemodel/test/cases/serializers/xml_serialization_test.rb b/activemodel/test/cases/serializers/xml_serialization_test.rb
index 90ddf8ff0c..99a9c1fe33 100755
--- a/activemodel/test/cases/serializers/xml_serialization_test.rb
+++ b/activemodel/test/cases/serializers/xml_serialization_test.rb
@@ -6,12 +6,12 @@ require 'ostruct'
class Contact
include ActiveModel::Serializers::Xml
- attr_accessor :address, :friends
+ attr_accessor :address, :friends, :contact
remove_method :attributes if method_defined?(:attributes)
def attributes
- instance_values.except("address", "friends")
+ instance_values.except("address", "friends", "contact")
end
end
@@ -56,6 +56,9 @@ class XmlSerializationTest < ActiveModel::TestCase
@contact.address.zip = 11111
@contact.address.apt_number = 35
@contact.friends = [Contact.new, Contact.new]
+ @related_contact = SerializableContact.new
+ @related_contact.name = "related"
+ @contact.contact = @related_contact
end
test "should serialize default root" do
@@ -256,4 +259,9 @@ class XmlSerializationTest < ActiveModel::TestCase
assert_match %r{<address>}, xml
assert_match %r{<apt-number type="integer">}, xml
end
+
+ test "association with sti" do
+ xml = @contact.to_xml(include: :contact)
+ assert xml.include?(%(<contact type="SerializableContact">))
+ end
end