From c2f4681ab40083880b3aabe38769a5d5d3432447 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 13 Apr 2007 01:26:17 +0000 Subject: Added yielding of Builder instance for ActiveRecord::Base#to_xml calls [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6519 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_record/xml_serialization.rb | 23 ++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/xml_serialization.rb') diff --git a/activerecord/lib/active_record/xml_serialization.rb b/activerecord/lib/active_record/xml_serialization.rb index ff7720d786..e256135205 100644 --- a/activerecord/lib/active_record/xml_serialization.rb +++ b/activerecord/lib/active_record/xml_serialization.rb @@ -90,6 +90,23 @@ module ActiveRecord #:nodoc: # def # # + # Alternatively, you can also just yield the builder object as part of the to_xml call: + # + # firm.to_xml do |xml| + # xml.creator do + # xml.first_name "David" + # xml.last_name "Heinemeier Hansson" + # end + # end + # + # + # # ... normal attributes as shown above ... + # + # David + # Heinemeier Hansson + # + # + # # You may override the to_xml method in your ActiveRecord::Base # subclasses if you need to. The general form of doing this is # @@ -103,8 +120,9 @@ module ActiveRecord #:nodoc: # end # end # end - def to_xml(options = {}) - XmlSerializer.new(self, options).to_s + def to_xml(options = {}, &block) + serializer = XmlSerializer.new(self, options) + block_given? ? serializer.to_s(&block) : serializer.to_s end end @@ -231,6 +249,7 @@ module ActiveRecord #:nodoc: add_attributes add_includes add_procs + yield builder if block_given? end end -- cgit v1.2.3