Changeset 6879
- Timestamp:
- 05/28/07 15:51:53 (1 year ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record/base.rb (modified) (2 diffs)
- trunk/activerecord/test/base_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r6859 r6879 1 1 *SVN* 2 3 * Allow nil serialized attributes with a set class constraint. #7293 [sandofsky] 2 4 3 5 * Oracle: support binary fixtures. #7987 [Michael Schoen] trunk/activerecord/lib/active_record/base.rb
r6852 r6879 644 644 # Specifies that the attribute by the name of +attr_name+ should be serialized before saving to the database and unserialized 645 645 # after loading from the database. The serialization is done through YAML. If +class_name+ is specified, the serialized 646 # object must be of that class on retrieval or+SerializationTypeMismatch+ will be raised.646 # object must be of that class on retrieval, or nil. Otherwise, +SerializationTypeMismatch+ will be raised. 647 647 def serialize(attr_name, class_name = Object) 648 648 serialized_attributes[attr_name.to_s] = class_name … … 2111 2111 unserialized_object = object_from_yaml(@attributes[attr_name]) 2112 2112 2113 if unserialized_object.is_a?(self.class.serialized_attributes[attr_name]) 2113 if unserialized_object.is_a?(self.class.serialized_attributes[attr_name]) || unserialized_object.nil? 2114 2114 @attributes[attr_name] = unserialized_object 2115 2115 else trunk/activerecord/test/base_test.rb
r6847 r6879 1155 1155 end 1156 1156 1157 def test_nil_serialized_attribute_with_class_constraint 1158 myobj = MyObject.new('value1', 'value2') 1159 topic = Topic.new 1160 assert_nil topic.content 1161 end 1162 1163 def test_should_raise_exception_on_serialized_attribute_with_type_mismatch 1164 myobj = MyObject.new('value1', 'value2') 1165 topic = Topic.new(:content => myobj) 1166 assert topic.save 1167 Topic.serialize(:content, Hash) 1168 assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).content } 1169 ensure 1170 Topic.serialize(:content) 1171 end 1172 1157 1173 def test_serialized_attribute_with_class_constraint 1158 myobj = MyObject.new('value1', 'value2') 1159 topic = Topic.create("content" => myobj) 1174 settings = { "color" => "blue" } 1160 1175 Topic.serialize(:content, Hash) 1161 1162 assert_raise(ActiveRecord::SerializationTypeMismatch) { Topic.find(topic.id).content } 1163 1164 settings = { "color" => "blue" } 1165 Topic.find(topic.id).update_attribute("content", settings) 1176 topic = Topic.new(:content => settings) 1177 assert topic.save 1166 1178 assert_equal(settings, Topic.find(topic.id).content) 1179 ensure 1167 1180 Topic.serialize(:content) 1168 1181 end