From a39b919b51ad2dc7b2a27a7eeea4ffe5cbe10fc4 Mon Sep 17 00:00:00 2001 From: Bruce Krysiak Date: Thu, 25 Dec 2008 00:52:54 -0800 Subject: [PATCH] AR.to_xml now will really :skip_types on included associations --- .../active_record/serializers/xml_serializer.rb | 10 +++++----- activerecord/test/cases/xml_serialization_test.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb index 4749823..10665f4 100644 --- a/activerecord/lib/active_record/serializers/xml_serializer.rb +++ b/activerecord/lib/active_record/serializers/xml_serializer.rb @@ -232,14 +232,14 @@ module ActiveRecord #:nodoc: if records.is_a?(Enumerable) tag = reformat_name(association.to_s) if records.empty? - builder.tag!(tag, :type => :array) + builder.tag!(tag, options[:skip_types] ? {} : {:type => "array"}) else - builder.tag!(tag, :type => :array) do + builder.tag!(tag, options[:skip_types] ? {} : {:type => "array"}) do association_name = association.to_s.singularize records.each do |record| - record.to_xml opts.merge( - :root => association_name, - :type => (record.class.to_s.underscore == association_name ? nil : record.class.name) + record.to_xml opts.merge(:root => association_name).merge( + options[:skip_types] ? {} : + {:type => (record.class.to_s.underscore == association_name ? nil : record.class.name)} ) end end diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb index 39c6ea8..587ca52 100644 --- a/activerecord/test/cases/xml_serialization_test.rb +++ b/activerecord/test/cases/xml_serialization_test.rb @@ -37,6 +37,11 @@ class XmlSerializationTest < ActiveRecord::TestCase assert_match %r{$}, @xml assert_match %r{ 25).to_xml :skip_types => true + assert %r{25}.match(@xml) + end def test_should_include_yielded_additions @xml = Contact.new.to_xml do |xml| @@ -145,6 +150,13 @@ class DatabaseConnectedXmlSerializationTest < ActiveRecord::TestCase assert_match %r{}, xml end + def test_included_associations_should_skip_types + xml = authors(:david).to_xml :include=>:hello_posts, :indent => 0, :skip_types => true + assert_match %r{}, xml + assert_match %r{}, xml + assert_match %r{}, xml + end + def test_methods_are_called_on_object xml = authors(:david).to_xml :methods => :label, :indent => 0 assert_match %r{}, xml -- 1.6.0.4 From 09bddf5530c33adcd3d9cac1227032bc4f19077d Mon Sep 17 00:00:00 2001 From: Bruce Krysiak Date: Thu, 25 Dec 2008 00:58:12 -0800 Subject: [PATCH] fixing whitespace --- .../active_record/serializers/xml_serializer.rb | 2 +- activerecord/test/cases/xml_serialization_test.rb | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb index 10665f4..bb12442 100644 --- a/activerecord/lib/active_record/serializers/xml_serializer.rb +++ b/activerecord/lib/active_record/serializers/xml_serializer.rb @@ -239,7 +239,7 @@ module ActiveRecord #:nodoc: records.each do |record| record.to_xml opts.merge(:root => association_name).merge( options[:skip_types] ? {} : - {:type => (record.class.to_s.underscore == association_name ? nil : record.class.name)} + {:type => (record.class.to_s.underscore == association_name ? nil : record.class.name)} ) end end diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb index 587ca52..eb5efdc 100644 --- a/activerecord/test/cases/xml_serialization_test.rb +++ b/activerecord/test/cases/xml_serialization_test.rb @@ -39,15 +39,14 @@ class XmlSerializationTest < ActiveRecord::TestCase end def test_should_allow_skipped_types - @xml = Contact.new(:age => 25).to_xml :skip_types => true - assert %r{25}.match(@xml) + @xml = Contact.new(:age => 25).to_xml :skip_types => true + assert %r{25}.match(@xml) end def test_should_include_yielded_additions @xml = Contact.new.to_xml do |xml| xml.creator "David" end - assert_match %r{David}, @xml end end -- 1.6.0.4