diff options
author | Andrew White <andrew.white@unboxed.co> | 2019-01-20 10:19:07 +0000 |
---|---|---|
committer | Andrew White <andrew.white@unboxed.co> | 2019-01-21 09:23:12 +0000 |
commit | ccdedeb9d5e1b2b74f03baef65aa8198b456e3b0 (patch) | |
tree | aa974b0826ded5979d79ba05e06793d3c27ccad8 /activerecord/test | |
parent | 31e118c39dc50a5ffeffe2b745a26b4c56651a35 (diff) | |
download | rails-ccdedeb9d5e1b2b74f03baef65aa8198b456e3b0.tar.gz rails-ccdedeb9d5e1b2b74f03baef65aa8198b456e3b0.tar.bz2 rails-ccdedeb9d5e1b2b74f03baef65aa8198b456e3b0.zip |
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
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/type/time_test.rb | 22 |
1 files changed, 22 insertions, 0 deletions
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 |