aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2007-04-13 01:26:17 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2007-04-13 01:26:17 +0000
commitc2f4681ab40083880b3aabe38769a5d5d3432447 (patch)
tree511975d9db3174c96c0c13d65036e4fe18726028 /activerecord
parent596112dcf82d58fd3f10e875818e1a99c4a0575e (diff)
downloadrails-c2f4681ab40083880b3aabe38769a5d5d3432447.tar.gz
rails-c2f4681ab40083880b3aabe38769a5d5d3432447.tar.bz2
rails-c2f4681ab40083880b3aabe38769a5d5d3432447.zip
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
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/xml_serialization.rb23
-rw-r--r--activerecord/test/xml_serialization_test.rb8
3 files changed, 31 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index bc8cd0ba86..2cc7c4c304 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added yielding of Builder instance for ActiveRecord::Base#to_xml calls [DHH]
+
* Small additions and fixes for ActiveRecord documentation. Closes #7342 [jeremymcanally]
* Add helpful debugging info to the ActiveRecord::StatementInvalid exception in ActiveRecord::ConnectionAdapters::SqliteAdapter#table_structure. Closes #7925. [court3nay]
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:
# <abc>def</abc>
# </firm>
#
+ # 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
+ #
+ # <firm>
+ # # ... normal attributes as shown above ...
+ # <creator>
+ # <first_name>David</first_name>
+ # <last_name>Heinemeier Hansson</last_name>
+ # </creator>
+ # </firm>
+ #
# 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
diff --git a/activerecord/test/xml_serialization_test.rb b/activerecord/test/xml_serialization_test.rb
index 61da81fe2d..752a0021a9 100644
--- a/activerecord/test/xml_serialization_test.rb
+++ b/activerecord/test/xml_serialization_test.rb
@@ -56,6 +56,14 @@ class XmlSerializationTest < Test::Unit::TestCase
assert_no_match %r{<age}, @xml
assert_match %r{<created-at}, @xml
end
+
+ def test_should_include_yielded_additions
+ @xml = Contact.new.to_xml do |xml|
+ xml.creator "David"
+ end
+
+ assert_match %r{<creator>David</creator>}, @xml
+ end
end
class DefaultXmlSerializationTest < Test::Unit::TestCase