From d872281975b2bdcfcd06e21b55d78c8fb53ba5d1 Mon Sep 17 00:00:00 2001
From: David Heinemeier Hansson <david@loudthinking.com>
Date: Thu, 9 Mar 2006 21:12:28 +0000
Subject: Fixed to_xml across the board to use nice indention, better
 skip_attributes workings, no type on strings, and cleaned tests [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3829 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
---
 activerecord/CHANGELOG                 | 37 ++++++++++++++++++++++++++++++++++
 activerecord/lib/active_record/base.rb | 14 +++++++++++++
 activerecord/test/base_test.rb         | 28 +++++++++++++++++++++++++
 3 files changed, 79 insertions(+)

(limited to 'activerecord')

diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 760863de45..c3fdbe38c6 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,42 @@
 *SVN*
 
+* Added Base#to_xml that'll turn the current record into a XML representation [DHH]. Example:
+
+    topic.to_xml
+  
+  ...returns:
+  
+    <?xml version="1.0" encoding="UTF-8"?>
+    <topic>
+      <title>The First Topic</title>
+      <author-name>David</author-name>
+      <id type="integer">1</id>
+      <approved type="boolean">false</approved>
+      <replies-count type="integer">0</replies-count>
+      <bonus-time type="datetime">2000-01-01 08:28:00</bonus-time>
+      <written-on type="datetime">2003-07-16 09:28:00</written-on>
+      <content>Have a nice day</content>
+      <author-email-address>david@loudthinking.com</author-email-address>
+      <parent-id></parent-id>
+      <last-read type="date">2004-04-15</last-read>
+    </topic>
+  
+  ...and you can configure with:
+  
+    topic.to_xml(:skip_instruct => true, :skip_attributes => [ :id, bonus_time, :written_on, replies_count ])
+  
+  ...that'll return:
+  
+    <topic>
+      <title>The First Topic</title>
+      <author-name>David</author-name>
+      <approved type="boolean">false</approved>
+      <content>Have a nice day</content>
+      <author-email-address>david@loudthinking.com</author-email-address>
+      <parent-id></parent-id>
+      <last-read type="date">2004-04-15</last-read>
+    </topic>
+
 * Allow :counter_cache to take a column name for custom counter cache columns [Jamis Buck]
 
 * Documentation fixes for :dependent [robby@planetargon.com]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 1203068aa8..61f7e94d89 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1504,6 +1504,20 @@ module ActiveRecord #:nodoc:
         @readonly = true
       end
 
+      # Turns this record into XML 
+      def to_xml(options = {})
+        options[:skip_attributes] = Array(options[:skip_attributes])
+        options[:skip_attributes] << :type
+        options[:skip_attributes].collect! { |attribute| attribute.to_s }
+
+        attributes_to_be_xmled = attributes
+        options[:skip_attributes].each { |attribute_name| attributes_to_be_xmled.delete(attribute_name) }
+
+        options[:root] ||= self.class.to_s.underscore
+
+        attributes_to_be_xmled.to_xml(options)
+      end
+
     private
       def create_or_update
         if new_record? then create else update end
diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb
index fe74bfa7e3..d82bde433d 100755
--- a/activerecord/test/base_test.rb
+++ b/activerecord/test/base_test.rb
@@ -1154,6 +1154,34 @@ class BasicsTest < Test::Unit::TestCase
     assert_no_queries { assert true }
   end
 
+  def test_to_xml
+    xml = Topic.find(:first).to_xml(:indent => 0, :skip_instruct => true)
+    assert_equal "<topic>", xml.first(7)
+    assert xml.include?(%(<title>The First Topic</title>))
+    assert xml.include?(%(<author-name>David</author-name>))
+    assert xml.include?(%(<id type="integer">1</id>))
+    assert xml.include?(%(<approved type="boolean">false</approved>))
+    assert xml.include?(%(<replies-count type="integer">0</replies-count>))
+    assert xml.include?(%(<bonus-time type="datetime">2000-01-01 08:28:00</bonus-time>))
+    assert xml.include?(%(<written-on type="datetime">2003-07-16 09:28:00</written-on>))
+    assert xml.include?(%(<content>Have a nice day</content>))
+    assert xml.include?(%(<author-email-address>david@loudthinking.com</author-email-address>))
+    assert xml.include?(%(<parent-id></parent-id>))
+    assert xml.include?(%(<last-read type="date">2004-04-15</last-read>))
+  end
+  
+  def test_to_xml_skipping_attributes
+    xml = Topic.find(:first).to_xml(:indent => 0, :skip_instruct => true, :skip_attributes => :title)
+    breakpoint
+    assert_equal "<topic>", xml.first(7)
+    assert !xml.include?(%(<title>The First Topic</title>))
+    assert xml.include?(%(<author-name>David</author-name>))    
+
+    xml = Topic.find(:first).to_xml(:indent => 0, :skip_instruct => true, :skip_attributes => [ :title, :author_name ])
+    assert !xml.include?(%(<title>The First Topic</title>))
+    assert !xml.include?(%(<author-name>David</author-name>))    
+  end
+
   # FIXME: this test ought to run, but it needs to run sandboxed so that it
   # doesn't b0rk the current test environment by undefing everything.
   #
-- 
cgit v1.2.3