From 5850edf104ab1da0f72a43e6717a1270134db293 Mon Sep 17 00:00:00 2001 From: mfoster Date: Sun, 31 Jan 2010 01:03:52 -0800 Subject: Made arrays of ActiveRecords, and any classes, with namespaces convert to valid xml. [#3824 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activerecord/test/cases/xml_serialization_test.rb | 12 +++++++++++- .../lib/active_support/core_ext/array/conversions.rb | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb index e1ad5c1685..2849ff11b7 100644 --- a/activerecord/test/cases/xml_serialization_test.rb +++ b/activerecord/test/cases/xml_serialization_test.rb @@ -130,10 +130,20 @@ class NilXmlSerializationTest < ActiveRecord::TestCase end class DatabaseConnectedXmlSerializationTest < ActiveRecord::TestCase - fixtures :authors, :posts + fixtures :authors, :posts, :projects + # to_xml used to mess with the hash the user provided which # caused the builder to be reused. This meant the document kept # getting appended to. + + def test_modules + projects = MyApplication::Business::Project.all + xml = projects.to_xml + root = projects.first.class.to_s.underscore.pluralize.tr('/','_').dasherize + assert_match "<#{root} type=\"array\">", xml + assert_match "", xml + end + def test_passing_hash_shouldnt_reuse_builder options = {:include=>:posts} david = authors(:david) diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index 2119322bfe..5d8e78e6e5 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -131,7 +131,7 @@ class Array require 'builder' unless defined?(Builder) options = options.dup - options[:root] ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? ActiveSupport::Inflector.pluralize(ActiveSupport::Inflector.underscore(first.class.name)) : "records" + options[:root] ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? ActiveSupport::Inflector.pluralize(ActiveSupport::Inflector.underscore(first.class.name)).tr('/', '_') : "records" options[:children] ||= options[:root].singularize options[:indent] ||= 2 options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) -- cgit v1.2.3