From d872281975b2bdcfcd06e21b55d78c8fb53ba5d1 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson 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: + + + + The First Topic + David + 1 + false + 0 + 2000-01-01 08:28:00 + 2003-07-16 09:28:00 + Have a nice day + david@loudthinking.com + + 2004-04-15 + + + ...and you can configure with: + + topic.to_xml(:skip_instruct => true, :skip_attributes => [ :id, bonus_time, :written_on, replies_count ]) + + ...that'll return: + + + The First Topic + David + false + Have a nice day + david@loudthinking.com + + 2004-04-15 + + * 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 "", xml.first(7) + assert xml.include?(%(The First Topic)) + assert xml.include?(%(David)) + assert xml.include?(%(1)) + assert xml.include?(%(false)) + assert xml.include?(%(0)) + assert xml.include?(%(2000-01-01 08:28:00)) + assert xml.include?(%(2003-07-16 09:28:00)) + assert xml.include?(%(Have a nice day)) + assert xml.include?(%(david@loudthinking.com)) + assert xml.include?(%()) + assert xml.include?(%(2004-04-15)) + end + + def test_to_xml_skipping_attributes + xml = Topic.find(:first).to_xml(:indent => 0, :skip_instruct => true, :skip_attributes => :title) + breakpoint + assert_equal "", xml.first(7) + assert !xml.include?(%(The First Topic)) + assert xml.include?(%(David)) + + xml = Topic.find(:first).to_xml(:indent => 0, :skip_instruct => true, :skip_attributes => [ :title, :author_name ]) + assert !xml.include?(%(The First Topic)) + assert !xml.include?(%(David)) + 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