aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
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 /activemodel
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 'activemodel')
-rw-r--r--activemodel/lib/active_model/serializers/xml.rb6
-rw-r--r--activemodel/test/cases/serializeration/xml_serialization_test.rb12
2 files changed, 17 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb
index 76a0e54a56..8137300479 100644
--- a/activemodel/lib/active_model/serializers/xml.rb
+++ b/activemodel/lib/active_model/serializers/xml.rb
@@ -152,7 +152,11 @@ module ActiveModel
def add_procs
if procs = options.delete(:procs)
[ *procs ].each do |proc|
- proc.call(options)
+ if proc.arity > 1
+ proc.call(options, @serializable)
+ else
+ proc.call(options)
+ end
end
end
end
diff --git a/activemodel/test/cases/serializeration/xml_serialization_test.rb b/activemodel/test/cases/serializeration/xml_serialization_test.rb
index e459f6433a..0737c4f2f9 100644
--- a/activemodel/test/cases/serializeration/xml_serialization_test.rb
+++ b/activemodel/test/cases/serializeration/xml_serialization_test.rb
@@ -82,4 +82,16 @@ class XmlSerializationTest < ActiveModel::TestCase
test "should serialize yaml" do
assert_match %r{<preferences type=\"yaml\">--- \n:gem: ruby\n</preferences>}, @contact.to_xml
end
+
+ test "should call proc on object" do
+ proc = Proc.new { |options| options[:builder].tag!('nationality', 'unknown') }
+ xml = @contact.to_xml(:procs => [ proc ])
+ assert_match %r{<nationality>unknown</nationality>}, xml
+ end
+
+ test 'should supply serializable to second proc argument' do
+ proc = Proc.new { |options, record| options[:builder].tag!('name-reverse', record.name.reverse) }
+ xml = @contact.to_xml(:procs => [ proc ])
+ assert_match %r{<name-reverse>kcats noraa</name-reverse>}, xml
+ end
end