From ce7cdb90728d2883e6eee100f3c6a845d3569d63 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Sun, 12 Aug 2012 20:26:17 +1000 Subject: [PATCH 1/2] Fix for time type columns with invalid time The string_to_dummy_time method was blindly parsing the dummy time string with Date._parse which returns a hash for the date part regardless of whether the time part is an invalid time string. --- .../lib/active_record/connection_adapters/column.rb | 8 +++++++- activerecord/test/cases/base_test.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index 1445bb3b2f938..d0237848c7337 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -178,7 +178,13 @@ def string_to_dummy_time(string) return string unless string.is_a?(String) return nil if string.blank? - 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 9fcee99222bdb..b9d480d9ce899 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -970,6 +970,18 @@ def test_attributes_on_dummy_time 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 From acf583a5e00be0fb684f73ad51cdad0237c981b2 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Wed, 5 Sep 2012 21:05:25 +1000 Subject: [PATCH 2/2] Update changelog with time column type casting fix --- activerecord/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 85880e97eae1d..68c43558fddca 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 4.0.0 (unreleased) ## +* Fix time column type casting for invalid time string values to correctly return nil. + + *Adam Meehan* + * Allow to pass Symbol or Proc into :limit option of #accepts_nested_attributes_for *Mikhail Dieterle*