Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for time type columns with invalid time value #7337

Merged
merged 2 commits into from Sep 5, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ## ## 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 * Allow to pass Symbol or Proc into :limit option of #accepts_nested_attributes_for


*Mikhail Dieterle* *Mikhail Dieterle*
Expand Down
8 changes: 7 additions & 1 deletion activerecord/lib/active_record/connection_adapters/column.rb
Expand Up @@ -178,7 +178,13 @@ def string_to_dummy_time(string)
return string unless string.is_a?(String) return string unless string.is_a?(String)
return nil if string.blank? 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 end


# convert something to a boolean # convert something to a boolean
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -970,6 +970,18 @@ def test_attributes_on_dummy_time
assert_equal Time.local(2000, 1, 1, 5, 42, 0), topic.bonus_time assert_equal Time.local(2000, 1, 1, 5, 42, 0), topic.bonus_time
end 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 def test_boolean
b_nil = Boolean.create({ "value" => nil }) b_nil = Boolean.create({ "value" => nil })
nil_id = b_nil.id nil_id = b_nil.id
Expand Down