aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnnie-Claude Côté <hello@annie.codes>2018-05-16 15:50:04 -0400
committerAnnie-Claude Côté <hello@annie.codes>2018-05-16 17:01:07 -0400
commit6ff593ef87f0a63e41022cbb3542ff8319e5a630 (patch)
treea79e81651b1450d89d996856724f1723a562d1f2
parent35bf8e90b18954fb1ebf2b4a03a123e04634b064 (diff)
downloadrails-6ff593ef87f0a63e41022cbb3542ff8319e5a630.tar.gz
rails-6ff593ef87f0a63e41022cbb3542ff8319e5a630.tar.bz2
rails-6ff593ef87f0a63e41022cbb3542ff8319e5a630.zip
Fix user_input_in_time_zone to coerce non valid string into nil
Before it was coercing an invalid string into "2000-01-01 00:00:00".
-rw-r--r--activemodel/lib/active_model/type/time.rb2
-rw-r--r--activemodel/test/cases/type/time_test.rb15
-rw-r--r--activerecord/test/cases/attribute_methods_test.rb10
3 files changed, 27 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/type/time.rb b/activemodel/lib/active_model/type/time.rb
index c094ee0013..b3056b1333 100644
--- a/activemodel/lib/active_model/type/time.rb
+++ b/activemodel/lib/active_model/type/time.rb
@@ -18,6 +18,8 @@ module ActiveModel
case value
when ::String
value = "2000-01-01 #{value}"
+ time_hash = ::Date._parse(value)
+ return if time_hash[:hour].nil?
when ::Time
value = value.change(year: 2000, day: 1, month: 1)
end
diff --git a/activemodel/test/cases/type/time_test.rb b/activemodel/test/cases/type/time_test.rb
index f7102d1e97..3fbae1a169 100644
--- a/activemodel/test/cases/type/time_test.rb
+++ b/activemodel/test/cases/type/time_test.rb
@@ -17,6 +17,21 @@ module ActiveModel
assert_equal ::Time.utc(2000, 1, 1, 16, 45, 54), type.cast("2015-06-13T19:45:54+03:00")
assert_equal ::Time.utc(1999, 12, 31, 21, 7, 8), type.cast("06:07:08+09:00")
end
+
+ def test_user_input_in_time_zone
+ ::Time.use_zone("Pacific Time (US & Canada)") do
+ type = Type::Time.new
+ assert_nil type.user_input_in_time_zone(nil)
+ assert_nil type.user_input_in_time_zone("")
+ assert_nil type.user_input_in_time_zone("ABC")
+
+ offset = ::Time.zone.formatted_offset
+ time_string = "2015-02-09T19:45:54#{offset}"
+
+ assert_equal 19, type.user_input_in_time_zone(time_string).hour
+ assert_equal offset, type.user_input_in_time_zone(time_string).formatted_offset
+ end
+ end
end
end
end
diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb
index 4a7427158a..434d32846c 100644
--- a/activerecord/test/cases/attribute_methods_test.rb
+++ b/activerecord/test/cases/attribute_methods_test.rb
@@ -736,6 +736,16 @@ class AttributeMethodsTest < ActiveRecord::TestCase
end
end
+ test "setting invalid string to a zone-aware time attribute" do
+ in_time_zone "Pacific Time (US & Canada)" do
+ record = @target.new
+ time_string = "ABC"
+
+ record.bonus_time = time_string
+ assert_nil record.bonus_time
+ end
+ end
+
test "removing time zone-aware types" do
with_time_zone_aware_types(:datetime) do
in_time_zone "Pacific Time (US & Canada)" do