diff options
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/serializers/xml.rb | 8 | ||||
-rw-r--r-- | activemodel/test/cases/serializeration/xml_serialization_test.rb | 23 | ||||
-rw-r--r-- | activemodel/test/models/contact.rb | 4 |
3 files changed, 33 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb index 76a0e54a56..4508a39347 100644 --- a/activemodel/lib/active_model/serializers/xml.rb +++ b/activemodel/lib/active_model/serializers/xml.rb @@ -90,7 +90,7 @@ module ActiveModel end def root - root = (options[:root] || @serializable.class.to_s.underscore).to_s + root = (options[:root] || @serializable.class.model_name.singular).to_s reformat_name(root) end @@ -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..428e5a6bd1 100644 --- a/activemodel/test/cases/serializeration/xml_serialization_test.rb +++ b/activemodel/test/cases/serializeration/xml_serialization_test.rb @@ -9,6 +9,11 @@ class Contact end end +module Admin + class Contact < ::Contact + end +end + class XmlSerializationTest < ActiveModel::TestCase def setup @contact = Contact.new @@ -25,6 +30,12 @@ class XmlSerializationTest < ActiveModel::TestCase assert_match %r{</contact>$}, @xml end + test "should serialize namespaced root" do + @xml = Admin::Contact.new(@contact.attributes).to_xml + assert_match %r{^<admin-contact>}, @xml + assert_match %r{</admin-contact>$}, @xml + end + test "should serialize default root with namespace" do @xml = @contact.to_xml :namespace => "http://xml.rubyonrails.org/contact" assert_match %r{^<contact xmlns="http://xml.rubyonrails.org/contact">}, @xml @@ -82,4 +93,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 diff --git a/activemodel/test/models/contact.rb b/activemodel/test/models/contact.rb index 7d69c91996..f9fb0af027 100644 --- a/activemodel/test/models/contact.rb +++ b/activemodel/test/models/contact.rb @@ -1,3 +1,7 @@ class Contact attr_accessor :name, :age, :created_at, :awesome, :preferences + + def initialize(options = {}) + options.each { |name, value| send("#{name}=", value) } + end end |