From 0776881fd4bb0ae61a90c588b2406f0c8711d40e Mon Sep 17 00:00:00 2001 From: James Sanders Date: Tue, 18 Aug 2009 16:34:18 -0600 Subject: [PATCH 1/2] Add skip_nils to xml serialize --- activemodel/lib/active_model/serializers/xml.rb | 6 +++--- .../serializeration/xml_serialization_test.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb index 86149f1..de76f58 100644 --- a/activemodel/lib/active_model/serializers/xml.rb +++ b/activemodel/lib/active_model/serializers/xml.rb @@ -30,7 +30,7 @@ module ActiveModel ![ :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 @@ -41,7 +41,7 @@ module ActiveModel decorations[:type] = type end - if value.nil? + if include_nils && value.nil? decorations[:nil] = true end @@ -173,7 +173,7 @@ module ActiveModel builder.tag!( reformat_name(attribute.name), attribute.value.to_s, - attribute.decorations(!options[:skip_types]) + attribute.decorations(!options[:skip_types], !options[:skip_nils]) ) end end diff --git a/activemodel/test/cases/serializeration/xml_serialization_test.rb b/activemodel/test/cases/serializeration/xml_serialization_test.rb index 428e5a6..d051141 100644 --- a/activemodel/test/cases/serializeration/xml_serialization_test.rb +++ b/activemodel/test/cases/serializeration/xml_serialization_test.rb @@ -105,4 +105,16 @@ class XmlSerializationTest < ActiveModel::TestCase xml = @contact.to_xml(:procs => [ proc ]) assert_match %r{kcats noraa}, xml end + + test "should serialize nil" do + @contact.name = nil + @xml = @contact.to_xml + assert_match %r{}, @xml + end + + test "should allow skipped nils" do + @contact.name = nil + @xml = @contact.to_xml :skip_nils => true + assert_match %r{}, @xml + end end -- 1.6.3.3 From fa1185af55d2ad803fefd1908e69f10b7bd448e7 Mon Sep 17 00:00:00 2001 From: James Sanders Date: Wed, 19 Aug 2009 11:11:40 -0600 Subject: [PATCH 2/2] Add tests, update documentation in xml_serializer --- .../serializeration/xml_serialization_test.rb | 8 ++++- .../active_record/serializers/xml_serializer.rb | 5 ++- activerecord/test/cases/xml_serialization_test.rb | 32 +++++++++++++++++-- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/activemodel/test/cases/serializeration/xml_serialization_test.rb b/activemodel/test/cases/serializeration/xml_serialization_test.rb index d051141..1d75a84 100644 --- a/activemodel/test/cases/serializeration/xml_serialization_test.rb +++ b/activemodel/test/cases/serializeration/xml_serialization_test.rb @@ -109,12 +109,16 @@ class XmlSerializationTest < ActiveModel::TestCase test "should serialize nil" do @contact.name = nil @xml = @contact.to_xml - assert_match %r{}, @xml + assert %r{}.match(@xml) + attributes = $1 + assert_match %r{nil="true"}, attributes end test "should allow skipped nils" do @contact.name = nil @xml = @contact.to_xml :skip_nils => true - assert_match %r{}, @xml + assert %r{}.match(@xml) + attributes = $1 + assert_no_match %r{nil="true"}, attributes end end diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb index b199207..ba87213 100644 --- a/activerecord/lib/active_record/serializers/xml_serializer.rb +++ b/activerecord/lib/active_record/serializers/xml_serializer.rb @@ -22,17 +22,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: # diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb index e1ad5c1..1eb3cd7 100644 --- a/activerecord/test/cases/xml_serialization_test.rb +++ b/activerecord/test/cases/xml_serialization_test.rb @@ -85,11 +85,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 @@ -97,6 +100,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 @@ -105,6 +113,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 @@ -112,13 +126,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 @@ -126,6 +145,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