aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/xml_serialization.rb12
-rw-r--r--activerecord/test/xml_serialization_test.rb19
2 files changed, 28 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/xml_serialization.rb b/activerecord/lib/active_record/xml_serialization.rb
index 42fe46bca3..7e2d5dd01e 100644
--- a/activerecord/lib/active_record/xml_serialization.rb
+++ b/activerecord/lib/active_record/xml_serialization.rb
@@ -211,7 +211,13 @@ module ActiveRecord #:nodoc:
builder.tag!(tag, :type => :array)
else
builder.tag!(tag, :type => :array) do
- records.each { |r| r.to_xml(opts.merge(:root=>r.class.to_s.underscore)) }
+ association_name = association.to_s.singularize
+ records.each do |record|
+ record.to_xml opts.merge(
+ :root => association_name,
+ :type => (record.class.to_s.underscore == association_name ? nil : record.class.name)
+ )
+ end
end
end
when :has_one, :belongs_to
@@ -247,6 +253,10 @@ module ActiveRecord #:nodoc:
if options[:namespace]
args << {:xmlns=>options[:namespace]}
end
+
+ if options[:type]
+ args << {:type=>options[:type]}
+ end
builder.tag!(*args) do
add_attributes
diff --git a/activerecord/test/xml_serialization_test.rb b/activerecord/test/xml_serialization_test.rb
index 807daddabb..f6e3d0dd9d 100644
--- a/activerecord/test/xml_serialization_test.rb
+++ b/activerecord/test/xml_serialization_test.rb
@@ -2,6 +2,7 @@ require 'abstract_unit'
require 'fixtures/post'
require 'fixtures/author'
require 'fixtures/tagging'
+require 'fixtures/comment'
class Contact < ActiveRecord::Base
# mock out self.columns so no pesky db is needed for these tests
@@ -147,8 +148,8 @@ class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase
def test_include_uses_association_name
xml = authors(:david).to_xml :include=>:hello_posts, :indent => 0
assert_match %r{<hello-posts type="array">}, xml
- assert_match %r{<post>}, xml
- assert_match %r{<sti-post>}, xml
+ assert_match %r{<hello-post type="Post">}, xml
+ assert_match %r{<hello-post type="StiPost">}, xml
end
def test_methods_are_called_on_object
@@ -171,4 +172,18 @@ class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase
assert_match %r{^ <posts type="array"/>}, xml
end
+ def test_should_has_many_array_elements_should_include_type_when_different_from_guessed_value
+ xml = authors(:david).to_xml :include=>:posts_with_comments, :indent => 2
+
+ assert Hash.from_xml(xml)
+ assert_match %r{^ <posts-with-comments type="array">}, xml
+ assert_match %r{^ <posts-with-comment type="Post">}, xml
+ assert_match %r{^ <posts-with-comment type="StiPost">}, xml
+
+ types = Hash.from_xml(xml)['author']['posts_with_comments'].collect {|t| t['type'] }
+ assert types.include?('SpecialPost')
+ assert types.include?('Post')
+ assert types.include?('StiPost')
+ end
+
end \ No newline at end of file