From fb9e7eb0990a03716f8c926dc8a4bdf0ae3b6003 Mon Sep 17 00:00:00 2001 From: Joshua Nichols Date: Sun, 9 Aug 2009 22:16:30 -0400 Subject: [PATCH] Backported XML serialization behavior from master for dealing with root nodes that have modules. ie, - the root node is dasherized, such that MyApplication::Business::Project becomes - association children nodes have type attributes, such that MyApplication::Business::Developer becomes --- .../active_record/serializers/xml_serializer.rb | 8 ++++++-- activerecord/test/cases/xml_serialization_test.rb | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb index fa75874..9bf1193 100644 --- a/activerecord/lib/active_record/serializers/xml_serializer.rb +++ b/activerecord/lib/active_record/serializers/xml_serializer.rb @@ -178,7 +178,7 @@ module ActiveRecord #:nodoc: end def root - root = (options[:root] || @record.class.to_s.underscore).to_s + root = (options[:root] || @record.class.model_name.singular).to_s reformat_name(root) end @@ -320,7 +320,11 @@ module ActiveRecord #:nodoc: protected def compute_type - type = @record.class.serialized_attributes.has_key?(name) ? :yaml : @record.class.columns_hash[name].type + type = if @record.class.serialized_attributes.has_key?(name) + :yaml + else + @record.class.columns_hash[name].try(:type) + end case type when :text diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb index b499976..f958b7c 100644 --- a/activerecord/test/cases/xml_serialization_test.rb +++ b/activerecord/test/cases/xml_serialization_test.rb @@ -4,6 +4,7 @@ require 'models/post' require 'models/author' require 'models/tagging' require 'models/comment' +require 'models/company_in_module' class XmlSerializationTest < ActiveRecord::TestCase def test_should_serialize_default_root @@ -129,6 +130,25 @@ class NilXmlSerializationTest < ActiveRecord::TestCase end end +class DatabaseConnectedXmlModuleSerializationTest < ActiveRecord::TestCase + + fixtures :projects, :developers, :developers_projects + + def test_module + project = MyApplication::Business::Project.find :first + xml = project.to_xml + assert_match %r{}, xml + assert_match %r{}, xml + end + + def test_module_with_include + project = MyApplication::Business::Project.find :first + xml = project.to_xml :include => :developers + assert_match %r{}, xml + assert_match %r{}, xml + end +end + class DatabaseConnectedXmlSerializationTest < ActiveRecord::TestCase fixtures :authors, :posts # to_xml used to mess with the hash the user provided which -- 1.6.4