From 03121a1289c133bd22e42c2523ab344ecac09b00 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 17 Feb 2010 19:00:08 +0100 Subject: [PATCH] Solved a bug that prevented xml serialization when ActiveRecord attributes contains more than column values --- .../active_record/serializers/xml_serializer.rb | 10 +++++++++- activerecord/test/cases/xml_serialization_test.rb | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb index b199207..310b266 100644 --- a/activerecord/lib/active_record/serializers/xml_serializer.rb +++ b/activerecord/lib/active_record/serializers/xml_serializer.rb @@ -246,7 +246,15 @@ module ActiveRecord #:nodoc: class Attribute < ActiveModel::Serializers::Xml::Serializer::Attribute #:nodoc: protected def compute_type - type = @serializable.class.serialized_attributes.has_key?(name) ? :yaml : @serializable.class.columns_hash[name].type + if @serializable.class.serialized_attributes.has_key?(name) + type = :yaml + else + if @serializable.class.columns_hash[name] + type = @serializable.class.columns_hash[name].type + else + type = Hash::XML_TYPE_NAMES[@serializable.send(name).class.name] || :string + end + end case type when :text diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb index e1ad5c1..9a784dc 100644 --- a/activerecord/test/cases/xml_serialization_test.rb +++ b/activerecord/test/cases/xml_serialization_test.rb @@ -49,6 +49,15 @@ class XmlSerializationTest < ActiveRecord::TestCase end assert_match %r{David}, @xml end + + def test_should_allow_the_serialization_of_non_column_attributes + instance = Contact.new + instance.class_eval("def _attr_(k,v); @attributes[k]=v; end") + instance._attr_("attr",1) + @xml = instance.to_xml + assert_match %r{},@xml + end + end class DefaultXmlSerializationTest < ActiveRecord::TestCase -- 1.6.6.1