Permalink
Browse files

apply [6879] to stable. Closes #7293.

git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-stable@6880 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent fe3fefe commit d5950cdd57d1a5c81587b429d176d6436db6845c @technoweenie technoweenie committed May 28, 2007
Showing with 21 additions and 6 deletions.
  1. +2 −0 activerecord/CHANGELOG
  2. +2 −2 activerecord/lib/active_record/base.rb
  3. +17 −4 activerecord/test/base_test.rb
View
@@ -1,5 +1,7 @@
*SVN*
+* Allow nil serialized attributes with a set class constraint. #7293 [sandofsky]
+
* belongs_to assignment creates a new proxy rather than modifying its target in-place. #8412 [mmangino@elevatedrails.com]
* Fix column type detection while loading fixtures. Closes #7987 [roderickvd]
@@ -575,7 +575,7 @@ def accessible_attributes # :nodoc:
# Specifies that the attribute by the name of +attr_name+ should be serialized before saving to the database and unserialized
# after loading from the database. The serialization is done through YAML. If +class_name+ is specified, the serialized
- # object must be of that class on retrieval or +SerializationTypeMismatch+ will be raised.
+ # object must be of that class on retrieval, or nil. Otherwise, +SerializationTypeMismatch+ will be raised.
def serialize(attr_name, class_name = Object)
serialized_attributes[attr_name.to_s] = class_name
end
@@ -1960,7 +1960,7 @@ def unserializable_attribute?(attr_name, column)
def unserialize_attribute(attr_name)
unserialized_object = object_from_yaml(@attributes[attr_name])
- if unserialized_object.is_a?(self.class.serialized_attributes[attr_name])
+ if unserialized_object.is_a?(self.class.serialized_attributes[attr_name]) || unserialized_object.nil?
@attributes[attr_name] = unserialized_object
else
raise SerializationTypeMismatch,
@@ -1086,16 +1086,29 @@ def test_serialized_attribute
assert_equal(myobj, topic.content)
end
- def test_serialized_attribute_with_class_constraint
+ def test_nil_serialized_attribute_with_class_constraint
myobj = MyObject.new('value1', 'value2')
- topic = Topic.create("content" => myobj)
- Topic.serialize(:content, Hash)
+ topic = Topic.new
+ assert_nil topic.content
+ end
+ def test_should_raise_exception_on_serialized_attribute_with_type_mismatch
+ myobj = MyObject.new('value1', 'value2')
+ topic = Topic.new(:content => myobj)
+ assert topic.save
+ Topic.serialize(:content, Hash)
assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).content }
+ ensure
+ Topic.serialize(:content)
+ end
+ def test_serialized_attribute_with_class_constraint
settings = { "color" => "blue" }
- Topic.find(topic.id).update_attribute("content", settings)
+ Topic.serialize(:content, Hash)
+ topic = Topic.new(:content => settings)
+ assert topic.save
assert_equal(settings, Topic.find(topic.id).content)
+ ensure
Topic.serialize(:content)
end

0 comments on commit d5950cd

Please sign in to comment.