aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorGrey Baker <greysteil@gmail.com>2014-12-23 02:30:00 +0000
committerGrey Baker <greysteil@gmail.com>2014-12-23 02:35:12 +0000
commitd318badc269358c53d9dfb4000e8c4c21a94b578 (patch)
tree45d9f6492aea1b6416ae48f036d4cd55f3c8ea72 /activerecord
parentd9d865aa40b29b4b5aba71e9f6108eaab7206b1e (diff)
downloadrails-d318badc269358c53d9dfb4000e8c4c21a94b578.tar.gz
rails-d318badc269358c53d9dfb4000e8c4c21a94b578.tar.bz2
rails-d318badc269358c53d9dfb4000e8c4c21a94b578.zip
Don't raise on out-of-range datetimes passed by a user
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb6
-rw-r--r--activerecord/test/cases/date_time_test.rb10
3 files changed, 20 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 6494c7374e..f8ac17c2d9 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Don't raise when writing an attribute with an out-of-range datetime passed
+ by the user.
+
+ *Grey Baker*
+
* Introduce `force: :cascade` option for `create_table`. Using this option
will recreate tables even if they have dependent objects (like foreign keys).
`db/schema.rb` now uses `force: :cascade`. This makes it possible to
diff --git a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
index 87274dd4e1..777f7ab4d7 100644
--- a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
+++ b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
@@ -12,7 +12,11 @@ module ActiveRecord
if value.is_a?(Array)
value.map { |v| type_cast_from_user(v) }
elsif value.respond_to?(:in_time_zone)
- value.in_time_zone || super
+ begin
+ value.in_time_zone || super
+ rescue ArgumentError
+ nil
+ end
end
end
diff --git a/activerecord/test/cases/date_time_test.rb b/activerecord/test/cases/date_time_test.rb
index c0491bbee5..c2ec92c40d 100644
--- a/activerecord/test/cases/date_time_test.rb
+++ b/activerecord/test/cases/date_time_test.rb
@@ -3,6 +3,8 @@ require 'models/topic'
require 'models/task'
class DateTimeTest < ActiveRecord::TestCase
+ include InTimeZone
+
def test_saves_both_date_and_time
with_env_tz 'America/New_York' do
with_timezone_config default: :utc do
@@ -29,6 +31,14 @@ class DateTimeTest < ActiveRecord::TestCase
assert_nil task.ending
end
+ def test_assign_bad_date_time_with_timezone
+ in_time_zone "Pacific Time (US & Canada)" do
+ task = Task.new
+ task.starting = '2014-07-01T24:59:59GMT'
+ assert_nil task.starting
+ end
+ end
+
def test_assign_empty_date
topic = Topic.new
topic.last_read = ''