mirror of
https://github.com/github/rails.git
synced 2026-01-31 01:08:19 -05:00
Allow nil serialized attributes with a set class constraint. #7293 [sandofsky]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6879 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Allow nil serialized attributes with a set class constraint. #7293 [sandofsky]
|
||||
|
||||
* Oracle: support binary fixtures. #7987 [Michael Schoen]
|
||||
|
||||
* Fixtures: pull fixture insertion into the database adapters. #7987 [Michael Schoen]
|
||||
|
||||
@@ -643,7 +643,7 @@ module ActiveRecord #: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
|
||||
@@ -2110,7 +2110,7 @@ module ActiveRecord #:nodoc:
|
||||
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,
|
||||
|
||||
@@ -1154,16 +1154,29 @@ class BasicsTest < Test::Unit::TestCase
|
||||
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 = 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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user