Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
tarmo authored and jeremy committed Aug 13, 2008
1 parent decc973 commit dc5997f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 9 additions & 2 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -2582,8 +2582,15 @@ def attributes_with_quotes(include_primary_key = true, include_readonly_attribut
quoted = {} quoted = {}
connection = self.class.connection connection = self.class.connection
attribute_names.each do |name| attribute_names.each do |name|
if column = column_for_attribute(name) if (column = column_for_attribute(name)) && (include_primary_key || !column.primary)
quoted[name] = connection.quote(read_attribute(name), column) unless !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
end end
include_readonly_attributes ? quoted : remove_readonly_attributes(quoted) include_readonly_attributes ? quoted : remove_readonly_attributes(quoted)
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -1353,6 +1353,12 @@ def test_serialized_attribute
assert_equal(myobj, topic.content) assert_equal(myobj, topic.content)
end 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 def test_nil_serialized_attribute_with_class_constraint
myobj = MyObject.new('value1', 'value2') myobj = MyObject.new('value1', 'value2')
topic = Topic.new topic = Topic.new
Expand Down

0 comments on commit dc5997f

Please sign in to comment.