Changeset 6880
- Timestamp:
- 05/28/07 15:54:39 (1 year ago)
- Files:
-
- branches/1-2-stable/activerecord/CHANGELOG (modified) (1 diff)
- branches/1-2-stable/activerecord/lib/active_record/base.rb (modified) (2 diffs)
- branches/1-2-stable/activerecord/test/base_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1-2-stable/activerecord/CHANGELOG
r6805 r6880 1 1 *SVN* 2 3 * Allow nil serialized attributes with a set class constraint. #7293 [sandofsky] 2 4 3 5 * belongs_to assignment creates a new proxy rather than modifying its target in-place. #8412 [mmangino@elevatedrails.com] branches/1-2-stable/activerecord/lib/active_record/base.rb
r6571 r6880 576 576 # Specifies that the attribute by the name of +attr_name+ should be serialized before saving to the database and unserialized 577 577 # after loading from the database. The serialization is done through YAML. If +class_name+ is specified, the serialized 578 # object must be of that class on retrieval or+SerializationTypeMismatch+ will be raised.578 # object must be of that class on retrieval, or nil. Otherwise, +SerializationTypeMismatch+ will be raised. 579 579 def serialize(attr_name, class_name = Object) 580 580 serialized_attributes[attr_name.to_s] = class_name … … 1961 1961 unserialized_object = object_from_yaml(@attributes[attr_name]) 1962 1962 1963 if unserialized_object.is_a?(self.class.serialized_attributes[attr_name]) 1963 if unserialized_object.is_a?(self.class.serialized_attributes[attr_name]) || unserialized_object.nil? 1964 1964 @attributes[attr_name] = unserialized_object 1965 1965 else branches/1-2-stable/activerecord/test/base_test.rb
r6109 r6880 1087 1087 end 1088 1088 1089 def test_nil_serialized_attribute_with_class_constraint 1090 myobj = MyObject.new('value1', 'value2') 1091 topic = Topic.new 1092 assert_nil topic.content 1093 end 1094 1095 def test_should_raise_exception_on_serialized_attribute_with_type_mismatch 1096 myobj = MyObject.new('value1', 'value2') 1097 topic = Topic.new(:content => myobj) 1098 assert topic.save 1099 Topic.serialize(:content, Hash) 1100 assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).content } 1101 ensure 1102 Topic.serialize(:content) 1103 end 1104 1089 1105 def test_serialized_attribute_with_class_constraint 1090 myobj = MyObject.new('value1', 'value2') 1091 topic = Topic.create("content" => myobj) 1106 settings = { "color" => "blue" } 1092 1107 Topic.serialize(:content, Hash) 1093 1094 assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).content } 1095 1096 settings = { "color" => "blue" } 1097 Topic.find(topic.id).update_attribute("content", settings) 1108 topic = Topic.new(:content => settings) 1109 assert topic.save 1098 1110 assert_equal(settings, Topic.find(topic.id).content) 1111 ensure 1099 1112 Topic.serialize(:content) 1100 1113 end