Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6879

Show
Ignore:
Timestamp:
05/28/07 15:51:53 (1 year ago)
Author:
rick
Message:

Allow nil serialized attributes with a set class constraint. #7293 [sandofsky]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r6859 r6879  
    11*SVN* 
     2 
     3* Allow nil serialized attributes with a set class constraint.  #7293 [sandofsky] 
    24 
    35* Oracle: support binary fixtures.  #7987 [Michael Schoen] 
  • trunk/activerecord/lib/active_record/base.rb

    r6852 r6879  
    644644      # Specifies that the attribute by the name of +attr_name+ should be serialized before saving to the database and unserialized 
    645645      # 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. 
    647647      def serialize(attr_name, class_name = Object) 
    648648        serialized_attributes[attr_name.to_s] = class_name 
     
    21112111        unserialized_object = object_from_yaml(@attributes[attr_name]) 
    21122112 
    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? 
    21142114          @attributes[attr_name] = unserialized_object 
    21152115        else 
  • trunk/activerecord/test/base_test.rb

    r6847 r6879  
    11551155  end 
    11561156 
     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 
    11571173  def test_serialized_attribute_with_class_constraint 
    1158     myobj = MyObject.new('value1', 'value2') 
    1159     topic = Topic.create("content" => myobj) 
     1174    settings = { "color" => "blue" } 
    11601175    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 
    11661178    assert_equal(settings, Topic.find(topic.id).content) 
     1179  ensure 
    11671180    Topic.serialize(:content) 
    11681181  end