From 7a10201d3acf7cc3a267f4d215214cf5dedca0a4 Mon Sep 17 00:00:00 2001 From: James Sanders Date: Wed, 19 Aug 2009 10:28:12 -0600 Subject: [PATCH] Add skip_nils to xml serializer --- .../active_record/serializers/xml_serializer.rb | 11 ++++--- activerecord/test/cases/xml_serialization_test.rb | 32 +++++++++++++++++-- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb index 9bf1193..1526caf 100644 --- a/activerecord/lib/active_record/serializers/xml_serializer.rb +++ b/activerecord/lib/active_record/serializers/xml_serializer.rb @@ -18,17 +18,18 @@ module ActiveRecord #:nodoc: # 2003-07-16T09:28:00+1200 # Have a nice day # david@loudthinking.com - # + # # 2004-04-15 # # # This behavior can be controlled with :only, :except, - # :skip_instruct, :skip_types, :dasherize and :camelize . + # :skip_instruct, :skip_types, :skip_nils, :dasherize and :camelize . # The :only and :except options are the same as for the # +attributes+ method. The default is to dasherize all column names, but you # can disable this setting :dasherize to +false+. Setting :camelize # to +true+ will camelize all column names - this also overrides :dasherize. # To not have the column type included in the XML output set :skip_types to +true+. + # To not list nil="true" for nil attributes set :skip_nils to +true+. # # For instance: # @@ -224,7 +225,7 @@ module ActiveRecord #:nodoc: builder.tag!( reformat_name(attribute.name), attribute.value.to_s, - attribute.decorations(!options[:skip_types]) + attribute.decorations(!options[:skip_types], !options[:skip_nils]) ) end @@ -300,7 +301,7 @@ module ActiveRecord #:nodoc: ![ :binary, :date, :datetime, :boolean, :float, :integer ].include?(type) end - def decorations(include_types = true) + def decorations(include_types = true, include_nils = true) decorations = {} if type == :binary @@ -311,7 +312,7 @@ module ActiveRecord #:nodoc: decorations[:type] = type end - if value.nil? + if include_nils && value.nil? decorations[:nil] = true end diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb index d8d952b..cc9b220 100644 --- a/activerecord/test/cases/xml_serialization_test.rb +++ b/activerecord/test/cases/xml_serialization_test.rb @@ -86,11 +86,14 @@ end class NilXmlSerializationTest < ActiveRecord::TestCase def setup - @xml = Contact.new.to_xml(:root => 'xml_contact') + contact = Contact.new + @xml = contact.to_xml(:root => 'xml_contact') + @xml_without_nils = contact.to_xml(:root => 'xml_contact', :skip_nils => true) end def test_should_serialize_string - assert_match %r{}, @xml + assert_match %r{}, @xml + assert_match %r{}, @xml_without_nils end def test_should_serialize_integer @@ -98,6 +101,11 @@ class NilXmlSerializationTest < ActiveRecord::TestCase attributes = $1 assert_match %r{nil="true"}, attributes assert_match %r{type="integer"}, attributes + + assert %r{}.match(@xml_without_nils) + attributes = $1 + assert_no_match %r{nil="true"}, attributes + assert_match %r{type="integer"}, attributes end def test_should_serialize_binary @@ -106,6 +114,12 @@ class NilXmlSerializationTest < ActiveRecord::TestCase assert_match %r{type="binary"}, attributes assert_match %r{encoding="base64"}, attributes assert_match %r{nil="true"}, attributes + + assert %r{}.match(@xml_without_nils) + attributes = $1 + assert_match %r{type="binary"}, attributes + assert_match %r{encoding="base64"}, attributes + assert_no_match %r{nil="true"}, attributes end def test_should_serialize_datetime @@ -113,13 +127,18 @@ class NilXmlSerializationTest < ActiveRecord::TestCase attributes = $1 assert_match %r{nil="true"}, attributes assert_match %r{type="datetime"}, attributes + + assert %r{}.match(@xml_without_nils) + attributes = $1 + assert_no_match %r{nil="true"}, attributes + assert_match %r{type="datetime"}, attributes end def test_should_serialize_boolean - assert %r{}.match(@xml) + assert %r{}.match(@xml_without_nils) attributes = $1 assert_match %r{type="boolean"}, attributes - assert_match %r{nil="true"}, attributes + assert_no_match %r{nil="true"}, attributes end def test_should_serialize_yaml @@ -127,6 +146,11 @@ class NilXmlSerializationTest < ActiveRecord::TestCase attributes = $1 assert_match %r{type="yaml"}, attributes assert_match %r{nil="true"}, attributes + + assert %r{}.match(@xml_without_nils) + attributes = $1 + assert_match %r{type="yaml"}, attributes + assert_no_match %r{nil="true"}, attributes end end -- 1.6.3.3