Skip to content

Instantly share code, notes, and snippets.

@franckverrot
Created November 18, 2010 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franckverrot/704770 to your computer and use it in GitHub Desktop.
Save franckverrot/704770 to your computer and use it in GitHub Desktop.
From 829913f766e69ce2f1ea600f02dbb30f02bbccb9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vi=CC=81ctor=20Marti=CC=81nez?= <knoopx@gmail.com>
Date: Thu, 30 Sep 2010 19:14:47 +0200
Subject: [PATCH 1/2] to_xml doesn't work in such case: Event.select('title as t').to_xml [#4840 state:resolved]
NilClass.type is no longer defined in Ruby 1.9 and causes ActiveRecord::Base.to_xml to fail with message: undefined method `type' for nil:NilClass
---
.../active_record/serializers/xml_serializer.rb | 10 ++++++++--
1 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb
index 15abf8b..0746908 100644
--- a/activerecord/lib/active_record/serializers/xml_serializer.rb
+++ b/activerecord/lib/active_record/serializers/xml_serializer.rb
@@ -226,8 +226,14 @@ module ActiveRecord #:nodoc:
class Attribute < ActiveModel::Serializers::Xml::Serializer::Attribute #:nodoc:
def compute_type
- type = @serializable.class.serialized_attributes.has_key?(name) ?
- super : @serializable.class.columns_hash[name].type
+ case
+ when @serializable.class.serialized_attributes.has_key?(name)
+ type = super
+ when @serializable.class.columns_hash.has_key?(name)
+ type = @serializable.class.columns_hash[name].type
+ else
+ type = NilClass
+ end
case type
when :text
--
1.7.1.1
From d02c8b6801e619175e9faf05cf49462472f72ff5 Mon Sep 17 00:00:00 2001
From: Franck Verrot <franck@verrot.fr>
Date: Thu, 18 Nov 2010 09:01:45 +0100
Subject: [PATCH 2/2] Provide test for #4840: to_xml doesn't work in such case: Event.select('title as t').to_xml
---
activerecord/test/cases/xml_serialization_test.rb | 6 ++++++
1 files changed, 6 insertions(+), 0 deletions(-)
diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb
index b11b340..a0b4a8e 100644
--- a/activerecord/test/cases/xml_serialization_test.rb
+++ b/activerecord/test/cases/xml_serialization_test.rb
@@ -241,4 +241,10 @@ class DatabaseConnectedXmlSerializationTest < ActiveRecord::TestCase
assert array.include? 'github'
end
+ def test_should_support_aliased_attributes
+ xml = Author.select("name as firstname").to_xml
+ array = Hash.from_xml(xml)['authors']
+ assert_equal array.size, array.select { |author| author.has_key? 'firstname' }.size
+ end
+
end
--
1.7.1.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment