diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-10-21 17:51:54 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-21 17:51:54 +0900 |
commit | 26a202a91b5a4754be5e3e137c390786ec977e56 (patch) | |
tree | 05acc29b494b7d8f567f722c59f1a6c5053e3b20 | |
parent | d684778a000cdcfcac53f6390c565c6a91a3391e (diff) | |
parent | 26cdd01eab5daf9ae17544ef44d3830dadeb8d23 (diff) | |
download | rails-26a202a91b5a4754be5e3e137c390786ec977e56.tar.gz rails-26a202a91b5a4754be5e3e137c390786ec977e56.tar.bz2 rails-26a202a91b5a4754be5e3e137c390786ec977e56.zip |
Merge pull request #34219 from wilddima/datetime-exception-message
Add message to exception in datetime type
-rw-r--r-- | activemodel/lib/active_model/type/date_time.rb | 6 | ||||
-rw-r--r-- | activemodel/test/cases/type/date_time_test.rb | 11 |
2 files changed, 14 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/type/date_time.rb b/activemodel/lib/active_model/type/date_time.rb index 9641bf45ee..d48598376e 100644 --- a/activemodel/lib/active_model/type/date_time.rb +++ b/activemodel/lib/active_model/type/date_time.rb @@ -39,9 +39,9 @@ module ActiveModel end def value_from_multiparameter_assignment(values_hash) - missing_parameter = (1..3).detect { |key| !values_hash.key?(key) } - if missing_parameter - raise ArgumentError, missing_parameter + missing_parameters = (1..3).select { |key| !values_hash.key?(key) } + if missing_parameters.any? + raise ArgumentError, "Provided hash #{values_hash} doesn't contain necessary keys: #{missing_parameters}" end super end diff --git a/activemodel/test/cases/type/date_time_test.rb b/activemodel/test/cases/type/date_time_test.rb index 60f62becc2..74b47d1b4d 100644 --- a/activemodel/test/cases/type/date_time_test.rb +++ b/activemodel/test/cases/type/date_time_test.rb @@ -25,6 +25,17 @@ module ActiveModel end end + def test_hash_to_time + type = Type::DateTime.new + assert_equal ::Time.utc(2018, 10, 15, 0, 0, 0), type.cast(1 => 2018, 2 => 10, 3 => 15) + end + + def test_hash_with_wrong_keys + type = Type::DateTime.new + error = assert_raises(ArgumentError) { type.cast(a: 1) } + assert_equal "Provided hash {:a=>1} doesn't contain necessary keys: [1, 2, 3]", error.message + end + private def with_timezone_config(default:) |