diff options
author | José Valim <jose.valim@gmail.com> | 2011-05-10 09:41:28 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-05-10 09:41:28 -0700 |
commit | ceaf73a503814d000167503888a62c1a393730d4 (patch) | |
tree | 28fed0ac20ba91f31345e911c73e7247d16efe4e | |
parent | 4de977117f5d7123aa97ab3837cc96ec19c68d33 (diff) | |
parent | 2cef2d411a20b89b71fb76876c33b331f94d0cc5 (diff) | |
download | rails-ceaf73a503814d000167503888a62c1a393730d4.tar.gz rails-ceaf73a503814d000167503888a62c1a393730d4.tar.bz2 rails-ceaf73a503814d000167503888a62c1a393730d4.zip |
Merge pull request #492 from jaw6/master
Serialized methods that return nil should not be considered YAML
4 files changed, 10 insertions, 4 deletions
diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb index d4295e6afe..19639b1363 100644 --- a/activemodel/lib/active_model/serializers/xml.rb +++ b/activemodel/lib/active_model/serializers/xml.rb @@ -33,6 +33,7 @@ module ActiveModel protected def compute_type + return if value.nil? type = ActiveSupport::XmlMini::TYPE_NAMES[value.class.name] type ||= :string if value.respond_to?(:to_str) type ||= :yaml diff --git a/activemodel/test/cases/serializers/xml_serialization_test.rb b/activemodel/test/cases/serializers/xml_serialization_test.rb index b6a2f88667..8f5c196850 100644 --- a/activemodel/test/cases/serializers/xml_serialization_test.rb +++ b/activemodel/test/cases/serializers/xml_serialization_test.rb @@ -92,6 +92,10 @@ class XmlSerializationTest < ActiveModel::TestCase test "should serialize string" do assert_match %r{<name>aaron stack</name>}, @contact.to_xml end + + test "should serialize nil" do + assert_match %r{<pseudonyms nil=\"true\"></pseudonyms>}, @contact.to_xml(:methods => :pseudonyms) + end test "should serialize integer" do assert_match %r{<age type="integer">25</age>}, @contact.to_xml diff --git a/activemodel/test/models/contact.rb b/activemodel/test/models/contact.rb index f4f3078473..7bfc542afb 100644 --- a/activemodel/test/models/contact.rb +++ b/activemodel/test/models/contact.rb @@ -16,6 +16,10 @@ class Contact options.each { |name, value| send("#{name}=", value) } end + def pseudonyms + nil + end + def persisted? id end diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb index a6074b23e7..756c8a32eb 100644 --- a/activerecord/test/cases/xml_serialization_test.rb +++ b/activerecord/test/cases/xml_serialization_test.rb @@ -143,10 +143,7 @@ class NilXmlSerializationTest < ActiveRecord::TestCase end def test_should_serialize_yaml - assert %r{<preferences(.*)></preferences>}.match(@xml) - attributes = $1 - assert_match %r{type="yaml"}, attributes - assert_match %r{nil="true"}, attributes + assert_match %r{<preferences nil=\"true\"></preferences>}, @xml end end |