aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-11-28 12:53:48 -0800
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-11-29 13:41:38 -0200
commite8c360527d22b6299d28e5b6784c4af74537eef2 (patch)
treea0e5573da44872e3d996f58512e4c8d0a123a1e4 /activemodel
parent66e87b714b406a25af60156a1fa15d1ebb99a0bd (diff)
downloadrails-e8c360527d22b6299d28e5b6784c4af74537eef2.tar.gz
rails-e8c360527d22b6299d28e5b6784c4af74537eef2.tar.bz2
rails-e8c360527d22b6299d28e5b6784c4af74537eef2.zip
Merge pull request #8352 from steveklabnik/update_7858
Specify type of singular assication during serialization Conflicts: activemodel/CHANGELOG.md activemodel/test/cases/serializers/xml_serialization_test.rb
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/CHANGELOG.md5
-rw-r--r--activemodel/lib/active_model/serializers/xml.rb7
-rw-r--r--activemodel/test/cases/serializers/xml_serialization_test.rb12
3 files changed, 21 insertions, 3 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md
index 4882421014..d98df4cb91 100644
--- a/activemodel/CHANGELOG.md
+++ b/activemodel/CHANGELOG.md
@@ -1,3 +1,8 @@
+## Rails 3.2.10 (unreleased) ##
+
+* Specify type of singular association during serialization *Steve Klabnik*
+
+
## Rails 3.2.9 (Nov 12, 2012) ##
* Due to a change in builder, nil values and empty strings now generates
diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb
index 2dc7d00f52..a63856045f 100644
--- a/activemodel/lib/active_model/serializers/xml.rb
+++ b/activemodel/lib/active_model/serializers/xml.rb
@@ -144,7 +144,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 83db8d21f4..a93db629a9 100644
--- 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
@@ -55,6 +55,9 @@ class XmlSerializationTest < ActiveModel::TestCase
@contact.address.state = "CA"
@contact.address.zip = 11111
@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
@@ -203,4 +206,9 @@ class XmlSerializationTest < ActiveModel::TestCase
assert_match %r{<friends>}, xml
assert_match %r{<friend>}, xml
end
+
+ test "association with sti" do
+ xml = @contact.to_xml(include: :contact)
+ assert xml.include?(%(<contact type="SerializableContact">))
+ end
end