aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorJohn Maxwell <johnmaxwell@gmail.com>2009-07-22 20:47:15 -0500
committerJoshua Peek <josh@joshpeek.com>2009-07-22 20:47:15 -0500
commitc39151a84768397d3bb025c6e8f877eac59ebbf9 (patch)
treea4e34e773ff2629abfab91925ed6cd3a2685408e /activerecord/lib
parent9d7aae710384fb5f04129c35b86c5ea5fb9d83a9 (diff)
downloadrails-c39151a84768397d3bb025c6e8f877eac59ebbf9.tar.gz
rails-c39151a84768397d3bb025c6e8f877eac59ebbf9.tar.bz2
rails-c39151a84768397d3bb025c6e8f877eac59ebbf9.zip
Patch to ActiveModel's (and ActiveRecord, by association) XML serialization: If two parameters are present in Procs supplied to to_xml's :procs option, the model being serialized will be passed as the second argument [#2373 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/serializers/xml_serializer.rb15
1 files changed, 15 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb
index 253fa03785..4e172bd2b6 100644
--- a/activerecord/lib/active_record/serializers/xml_serializer.rb
+++ b/activerecord/lib/active_record/serializers/xml_serializer.rb
@@ -71,6 +71,21 @@ module ActiveRecord #:nodoc:
# </account>
# </firm>
#
+ # Additionally, the record being serialized will be passed to a Proc's second
+ # parameter. This allows for ad hoc additions to the resultant document that
+ # incorporate the context of the record being serialized. And by leveraging the
+ # closure created by a Proc, to_xml can be used to add elements that normally fall
+ # outside of the scope of the model -- for example, generating and appending URLs
+ # associated with models.
+ #
+ # proc = Proc.new { |options, record| options[:builder].tag!('name-reverse', record.name.reverse) }
+ # firm.to_xml :procs => [ proc ]
+ #
+ # <firm>
+ # # ... normal attributes as shown above ...
+ # <name-reverse>slangis73</name-reverse>
+ # </firm>
+ #
# To include deeper levels of associations pass a hash like this:
#
# firm.to_xml :include => {:account => {}, :clients => {:include => :address}}