From 9504b44cf60c1d69519cef32465f46a73719bc17 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Fri, 5 Oct 2012 15:07:39 -0600 Subject: 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 --- activemodel/CHANGELOG.md | 2 ++ activemodel/lib/active_model/serializers/xml.rb | 7 ++++++- activemodel/test/cases/serializers/xml_serialization_test.rb | 12 ++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) (limited to 'activemodel') diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 5135e47db1..dc39917984 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,5 +1,7 @@ ## Rails 4.0.0 (unreleased) ## +* Specify type of singular association during serialization *Steve Klabnik* + * Fixed length validator to correctly handle nil values. Fixes #7180. *Michal Zima* diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb index fb6093cce5..4a17a63e20 100755 --- a/activemodel/lib/active_model/serializers/xml.rb +++ b/activemodel/lib/active_model/serializers/xml.rb @@ -149,7 +149,12 @@ module ActiveModel end else merged_options[:root] = association.to_s - records.to_xml(merged_options) + + unless records.class.to_s.underscore == association.to_s + merged_options[:type] = records.class.name + end + + records.to_xml merged_options end end 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{
}, xml assert_match %r{}, xml end + + test "association with sti" do + xml = @contact.to_xml(include: :contact) + assert xml.include?(%()) + end end -- cgit v1.2.3