public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fixed Time/Date object serialization

Time/Date objects used to be converted to_s instead of to_uaml
which made them unserializable.
Tarmo Tänav (author)
Tue Aug 12 20:18:01 -0700 2008
jeremy (committer)
Tue Aug 12 20:29:07 -0700 2008
commit  a5aad2e81febfa1a8d9fea0faffb5a3b4535982b
tree    f1936fe0655d0dd5fd0a6da1ac1e1d5d5b7d61e2
parent  1b127fcdea28d6c6ab2f2c6370125c8817ba99d6
...
2626
2627
2628
2629
2630
 
 
 
 
 
 
 
 
 
2631
2632
2633
...
2626
2627
2628
 
 
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
0
@@ -2626,8 +2626,15 @@ module ActiveRecord #:nodoc:
0
         quoted = {}
0
         connection = self.class.connection
0
         attribute_names.each do |name|
0
-          if column = column_for_attribute(name)
0
-            quoted[name] = connection.quote(read_attribute(name), column) unless !include_primary_key && column.primary
0
+          if (column = column_for_attribute(name)) && (include_primary_key || !column.primary)
0
+            value = read_attribute(name)
0
+
0
+            # We need explicit to_yaml because quote() does not properly convert Time/Date fields to YAML.
0
+            if value && self.class.serialized_attributes.has_key?(name) && (value.acts_like?(:date) || value.acts_like?(:time))
0
+              value = value.to_yaml
0
+            end
0
+
0
+            quoted[name] = connection.quote(value, column)
0
           end
0
         end
0
         include_readonly_attributes ? quoted : remove_readonly_attributes(quoted)
...
1361
1362
1363
 
 
 
 
 
 
1364
1365
1366
...
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
0
@@ -1361,6 +1361,12 @@ class BasicsTest < ActiveRecord::TestCase
0
     assert_equal(myobj, topic.content)
0
   end
0
 
0
+  def test_serialized_time_attribute
0
+    myobj = Time.local(2008,1,1,1,0)
0
+    topic = Topic.create("content" => myobj).reload
0
+    assert_equal(myobj, topic.content)
0
+  end
0
+
0
   def test_nil_serialized_attribute_with_class_constraint
0
     myobj = MyObject.new('value1', 'value2')
0
     topic = Topic.new

Comments