diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2007-12-03 01:47:21 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2007-12-03 01:47:21 +0000 |
commit | c4cb2dd01410417b5ab027aff5d472d68d9cbdb1 (patch) | |
tree | 2d1d767c23ef2c9460322f586c52a521c2fa50e5 /activerecord | |
parent | f1047173e8d2ccc3a65ed434445bd2807df86614 (diff) | |
download | rails-c4cb2dd01410417b5ab027aff5d472d68d9cbdb1.tar.gz rails-c4cb2dd01410417b5ab027aff5d472d68d9cbdb1.tar.bz2 rails-c4cb2dd01410417b5ab027aff5d472d68d9cbdb1.zip |
Fixed that to_xml should not automatically pass :procs to associations included with :include (closes #10162) [chuyeow]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8258 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/serializers/xml_serializer.rb | 18 | ||||
-rw-r--r-- | activerecord/test/xml_serialization_test.rb | 69 |
3 files changed, 63 insertions, 29 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 5192a57159..b8acc5f148 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,10 @@ +*SVN* + +* Fixed that to_xml should not automatically pass :procs to associations included with :include #10162 [chuyeow] + * Fix documentation typo introduced in [8250]. Closes #10339 [Henrik N] + *2.0.0 [RC2]* (November 28th, 2007) * Foxy fixtures: support single-table inheritance. #10234 [tom] diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb index 693060f06e..84b1a53470 100644 --- a/activerecord/lib/active_record/serializers/xml_serializer.rb +++ b/activerecord/lib/active_record/serializers/xml_serializer.rb @@ -1,12 +1,12 @@ module ActiveRecord #:nodoc: module Serialization # Builds an XML document to represent the model. Some configuration is - # available through +options+, however more complicated cases should + # available through +options+, however more complicated cases should # override ActiveRecord's to_xml. # - # By default the generated XML document will include the processing + # By default the generated XML document will include the processing # instruction and all object's attributes. For example: - # + # # <?xml version="1.0" encoding="UTF-8"?> # <topic> # <title>The First Topic</title> @@ -42,7 +42,7 @@ module ActiveRecord #:nodoc: # <parent-id></parent-id> # <last-read type="date">2004-04-15</last-read> # </topic> - # + # # To include first level associations use :include # # firm.to_xml :include => [ :account, :clients ] @@ -189,8 +189,8 @@ module ActiveRecord #:nodoc: def add_tag(attribute) builder.tag!( - dasherize? ? attribute.name.dasherize : attribute.name, - attribute.value.to_s, + dasherize? ? attribute.name.dasherize : attribute.name, + attribute.value.to_s, attribute.decorations(!options[:skip_types]) ) end @@ -204,9 +204,9 @@ module ActiveRecord #:nodoc: else builder.tag!(tag, :type => :array) do association_name = association.to_s.singularize - records.each do |record| + records.each do |record| record.to_xml opts.merge( - :root => association_name, + :root => association_name, :type => (record.class.to_s.underscore == association_name ? nil : record.class.name) ) end @@ -231,7 +231,9 @@ module ActiveRecord #:nodoc: builder.tag!(*args) do add_attributes + procs = options.delete(:procs) add_includes { |association, records, opts| add_associations(association, records, opts) } + options[:procs] = procs add_procs yield builder if block_given? end diff --git a/activerecord/test/xml_serialization_test.rb b/activerecord/test/xml_serialization_test.rb index 011f27ad14..9b74ddd45b 100644 --- a/activerecord/test/xml_serialization_test.rb +++ b/activerecord/test/xml_serialization_test.rb @@ -11,19 +11,19 @@ class XmlSerializationTest < Test::Unit::TestCase assert_match %r{^<contact>}, @xml assert_match %r{</contact>$}, @xml end - + def test_should_serialize_default_root_with_namespace @xml = Contact.new.to_xml :namespace=>"http://xml.rubyonrails.org/contact" assert_match %r{^<contact xmlns="http://xml.rubyonrails.org/contact">}, @xml assert_match %r{</contact>$}, @xml end - + def test_should_serialize_custom_root @xml = Contact.new.to_xml :root => 'xml_contact' assert_match %r{^<xml-contact>}, @xml assert_match %r{</xml-contact>$}, @xml end - + def test_should_allow_undasherized_tags @xml = Contact.new.to_xml :root => 'xml_contact', :dasherize => false assert_match %r{^<xml_contact>}, @xml @@ -48,25 +48,25 @@ class DefaultXmlSerializationTest < Test::Unit::TestCase def test_should_serialize_string assert_match %r{<name>aaron stack</name>}, @xml end - + def test_should_serialize_integer assert_match %r{<age type="integer">25</age>}, @xml end - + def test_should_serialize_binary assert_match %r{YmluYXJ5ZGF0YQ==\n</avatar>}, @xml assert_match %r{<avatar(.*)(type="binary")}, @xml assert_match %r{<avatar(.*)(encoding="base64")}, @xml end - + def test_should_serialize_datetime assert_match %r{<created-at type=\"datetime\">2006-08-01T00:00:00Z</created-at>}, @xml end - + def test_should_serialize_boolean assert_match %r{<awesome type=\"boolean\">false</awesome>}, @xml end - + def test_should_serialize_yaml assert_match %r{<preferences type=\"yaml\">--- \n:gem: ruby\n</preferences>}, @xml end @@ -80,14 +80,14 @@ class NilXmlSerializationTest < Test::Unit::TestCase def test_should_serialize_string assert_match %r{<name nil="true"></name>}, @xml end - + def test_should_serialize_integer assert %r{<age (.*)></age>}.match(@xml) attributes = $1 assert_match %r{nil="true"}, attributes assert_match %r{type="integer"}, attributes end - + def test_should_serialize_binary assert %r{<avatar (.*)></avatar>}.match(@xml) attributes = $1 @@ -95,21 +95,21 @@ class NilXmlSerializationTest < Test::Unit::TestCase assert_match %r{encoding="base64"}, attributes assert_match %r{nil="true"}, attributes end - + def test_should_serialize_datetime assert %r{<created-at (.*)></created-at>}.match(@xml) attributes = $1 assert_match %r{nil="true"}, attributes assert_match %r{type="datetime"}, attributes end - + def test_should_serialize_boolean assert %r{<awesome (.*)></awesome>}.match(@xml) attributes = $1 assert_match %r{type="boolean"}, attributes assert_match %r{nil="true"}, attributes end - + def test_should_serialize_yaml assert %r{<preferences(.*)></preferences>}.match(@xml) attributes = $1 @@ -137,30 +137,57 @@ class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase assert_match %r{<hello-post type="Post">}, xml assert_match %r{<hello-post type="StiPost">}, xml end - + def test_methods_are_called_on_object xml = authors(:david).to_xml :methods => :label, :indent => 0 assert_match %r{<label>.*</label>}, xml end - + def test_should_not_call_methods_on_associations_that_dont_respond xml = authors(:david).to_xml :include=>:hello_posts, :methods => :label, :indent => 2 assert !authors(:david).hello_posts.first.respond_to?(:label) assert_match %r{^ <label>.*</label>}, xml assert_no_match %r{^ <label>}, xml end - + + def test_procs_are_called_on_object + proc = Proc.new { |options| options[:builder].tag!('nationality', 'Danish') } + xml = authors(:david).to_xml(:procs => [ proc ]) + assert_match %r{<nationality>Danish</nationality>}, xml + end + + def test_top_level_procs_arent_applied_to_associations + author_proc = Proc.new { |options| options[:builder].tag!('nationality', 'Danish') } + xml = authors(:david).to_xml(:procs => [ author_proc ], :include => :posts, :indent => 2) + + assert_match %r{^ <nationality>Danish</nationality>}, xml + assert_no_match %r{^ {6}<nationality>Danish</nationality>}, xml + end + + def test_procs_on_included_associations_are_called + posts_proc = Proc.new { |options| options[:builder].tag!('copyright', 'DHH') } + xml = authors(:david).to_xml( + :indent => 2, + :include => { + :posts => { :procs => [ posts_proc ] } + } + ) + + assert_no_match %r{^ <copyright>DHH</copyright>}, xml + assert_match %r{^ {6}<copyright>DHH</copyright>}, xml + end + def test_should_include_empty_has_many_as_empty_array - authors(:david).posts.delete_all + authors(:david).posts.delete_all xml = authors(:david).to_xml :include=>:posts, :indent => 2 - + assert_equal [], Hash.from_xml(xml)['author']['posts'] assert_match %r{^ <posts type="array"/>}, xml end - + def test_should_has_many_array_elements_should_include_type_when_different_from_guessed_value xml = authors(:david).to_xml :include=>:posts_with_comments, :indent => 2 - + assert Hash.from_xml(xml) assert_match %r{^ <posts-with-comments type="array">}, xml assert_match %r{^ <posts-with-comment type="Post">}, xml @@ -171,5 +198,5 @@ class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase assert types.include?('Post') assert types.include?('StiPost') end - + end
\ No newline at end of file |