diff options
author | Andrew White <pixeltrix@users.noreply.github.com> | 2019-01-21 20:26:45 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-21 20:26:45 +0000 |
commit | 4f62e757caf608d30d46ee745f4f666fb8eba2a7 (patch) | |
tree | fe09316da3912e930f9cf512c5d8b35bc91a075a | |
parent | feb97dfabd027a73a41ca8bd2ab7c3196ab76d3d (diff) | |
parent | ccdedeb9d5e1b2b74f03baef65aa8198b456e3b0 (diff) | |
download | rails-4f62e757caf608d30d46ee745f4f666fb8eba2a7.tar.gz rails-4f62e757caf608d30d46ee745f4f666fb8eba2a7.tar.bz2 rails-4f62e757caf608d30d46ee745f4f666fb8eba2a7.zip |
Merge pull request #34990 from rails/fix-time-multiparameter-casting
Fix year value when casting a multiparameter time hash
-rw-r--r-- | activemodel/CHANGELOG.md | 28 | ||||
-rw-r--r-- | activemodel/lib/active_model/type/time.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/type/time_test.rb | 1 | ||||
-rw-r--r-- | activerecord/test/cases/type/time_test.rb | 22 |
4 files changed, 52 insertions, 1 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 571c050f2f..912b307683 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,31 @@ +* Fix year value when casting a multiparameter time hash + + When assigning a hash to a time attribute that's missing a year component + (e.g. a `time_select` with `:ignore_date` set to `true`) then the year + defaults to 1970 instead of the expected 2000. This results in the attribute + changing as a result of the save. + + Before: + ``` + event = Event.new(start_time: { 4 => 20, 5 => 30 }) + event.start_time # => 1970-01-01 20:30:00 UTC + event.save + event.reload + event.start_time # => 2000-01-01 20:30:00 UTC + ``` + + After: + ``` + event = Event.new(start_time: { 4 => 20, 5 => 30 }) + event.start_time # => 2000-01-01 20:30:00 UTC + event.save + event.reload + event.start_time # => 2000-01-01 20:30:00 UTC + ``` + + *Andrew White* + + ## Rails 6.0.0.beta1 (January 18, 2019) ## * Add `ActiveModel::Errors#of_kind?`. diff --git a/activemodel/lib/active_model/type/time.rb b/activemodel/lib/active_model/type/time.rb index b3056b1333..8fba01b86a 100644 --- a/activemodel/lib/active_model/type/time.rb +++ b/activemodel/lib/active_model/type/time.rb @@ -5,7 +5,7 @@ module ActiveModel class Time < Value # :nodoc: include Helpers::TimeValue include Helpers::AcceptsMultiparameterTime.new( - defaults: { 1 => 1970, 2 => 1, 3 => 1, 4 => 0, 5 => 0 } + defaults: { 1 => 2000, 2 => 1, 3 => 1, 4 => 0, 5 => 0 } ) def type diff --git a/activemodel/test/cases/type/time_test.rb b/activemodel/test/cases/type/time_test.rb index 3fbae1a169..5c6271241d 100644 --- a/activemodel/test/cases/type/time_test.rb +++ b/activemodel/test/cases/type/time_test.rb @@ -16,6 +16,7 @@ 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") + assert_equal ::Time.utc(2000, 1, 1, 16, 45, 54), type.cast(4 => 16, 5 => 45, 6 => 54) end def test_user_input_in_time_zone diff --git a/activerecord/test/cases/type/time_test.rb b/activerecord/test/cases/type/time_test.rb new file mode 100644 index 0000000000..1a2c47479f --- /dev/null +++ b/activerecord/test/cases/type/time_test.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require "cases/helper" +require "models/topic" + +module ActiveRecord + module Type + class TimeTest < ActiveRecord::TestCase + def test_default_year_is_correct + expected_time = ::Time.utc(2000, 1, 1, 10, 30, 0) + topic = Topic.new(bonus_time: { 4 => 10, 5 => 30 }) + + assert_equal expected_time, topic.bonus_time + + topic.save! + topic.reload + + assert_equal expected_time, topic.bonus_time + end + end + end +end |