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

Changeset 6880

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

apply [6879] to stable. Closes #7293.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1-2-stable/activerecord/CHANGELOG

    r6805 r6880  
    11*SVN* 
     2 
     3* Allow nil serialized attributes with a set class constraint. #7293 [sandofsky]  
    24 
    35* 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  
    576576      # Specifies that the attribute by the name of +attr_name+ should be serialized before saving to the database and unserialized 
    577577      # 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. 
    579579      def serialize(attr_name, class_name = Object) 
    580580        serialized_attributes[attr_name.to_s] = class_name 
     
    19611961        unserialized_object = object_from_yaml(@attributes[attr_name]) 
    19621962 
    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? 
    19641964          @attributes[attr_name] = unserialized_object 
    19651965        else 
  • branches/1-2-stable/activerecord/test/base_test.rb

    r6109 r6880  
    10871087  end 
    10881088 
     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 
    10891105  def test_serialized_attribute_with_class_constraint 
    1090     myobj = MyObject.new('value1', 'value2') 
    1091     topic = Topic.create("content" => myobj) 
     1106    settings = { "color" => "blue" } 
    10921107    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 
    10981110    assert_equal(settings, Topic.find(topic.id).content) 
     1111  ensure 
    10991112    Topic.serialize(:content) 
    11001113  end