Permalink
Browse files

Fixed Time/Date object serialization

Time/Date objects used to be converted to_s instead of to_uaml
which made them unserializable.
  • Loading branch information...
1 parent decc973 commit dc5997ff4223d1cfa93ef4a8f9184cc5bba27522 @tarmo tarmo committed with jeremy Aug 13, 2008
Showing with 15 additions and 2 deletions.
  1. +9 −2 activerecord/lib/active_record/base.rb
  2. +6 −0 activerecord/test/cases/base_test.rb
@@ -2582,8 +2582,15 @@ def attributes_with_quotes(include_primary_key = true, include_readonly_attribut
quoted = {}
connection = self.class.connection
attribute_names.each do |name|
- if column = column_for_attribute(name)
- quoted[name] = connection.quote(read_attribute(name), column) unless !include_primary_key && column.primary
+ if (column = column_for_attribute(name)) && (include_primary_key || !column.primary)
+ value = read_attribute(name)
+
+ # We need explicit to_yaml because quote() does not properly convert Time/Date fields to YAML.
+ if value && self.class.serialized_attributes.has_key?(name) && (value.acts_like?(:date) || value.acts_like?(:time))
+ value = value.to_yaml
+ end
+
+ quoted[name] = connection.quote(value, column)
end
end
include_readonly_attributes ? quoted : remove_readonly_attributes(quoted)
@@ -1353,6 +1353,12 @@ def test_serialized_attribute
assert_equal(myobj, topic.content)
end
+ def test_serialized_time_attribute
+ myobj = Time.local(2008,1,1,1,0)
+ topic = Topic.create("content" => myobj).reload
+ assert_equal(myobj, topic.content)
+ end
+
def test_nil_serialized_attribute_with_class_constraint
myobj = MyObject.new('value1', 'value2')
topic = Topic.new

0 comments on commit dc5997f

Please sign in to comment.