From 5054e266ac07da7a2a15173d3d2919948ae1eb6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 5 Sep 2012 06:26:53 -0700 Subject: Merge pull request #7337 from adzap/string_to_dummy_time Fix for time type columns with invalid time value Conflicts: activerecord/CHANGELOG.md --- activerecord/CHANGELOG.md | 4 ++++ activerecord/lib/active_record/connection_adapters/column.rb | 8 +++++++- activerecord/test/cases/base_test.rb | 12 ++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index c836886c08..1f04696d23 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 3.2.9 (unreleased) +* Fix time column type casting for invalid time string values to correctly return nil. + + *Adam Meehan* + * Fix `becomes` when using a configured `inheritance_column`. *Yves Senn* diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index 393a3b623a..ef8ee6cab0 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -150,7 +150,13 @@ module ActiveRecord return string unless string.is_a?(String) return nil if string.empty? - string_to_time "2000-01-01 #{string}" + dummy_time_string = "2000-01-01 #{string}" + + fast_string_to_time(dummy_time_string) || begin + time_hash = Date._parse(dummy_time_string) + return nil if time_hash[:hour].nil? + new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction)) + end end # convert something to a boolean diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index fc33438569..073e856e5e 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -919,6 +919,18 @@ class BasicsTest < ActiveRecord::TestCase assert_equal Time.local(2000, 1, 1, 5, 42, 0), topic.bonus_time end + def test_attributes_on_dummy_time_with_invalid_time + # Oracle, and Sybase do not have a TIME datatype. + return true if current_adapter?(:OracleAdapter, :SybaseAdapter) + + attributes = { + "bonus_time" => "not a time" + } + topic = Topic.find(1) + topic.attributes = attributes + assert_nil topic.bonus_time + end + def test_boolean b_nil = Boolean.create({ "value" => nil }) nil_id = b_nil.id -- cgit v1.2.3