aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorwilddima <dtopornin@gmail.com>2018-10-21 10:05:48 +0200
committerwilddima <dtopornin@gmail.com>2018-10-21 10:05:48 +0200
commit26cdd01eab5daf9ae17544ef44d3830dadeb8d23 (patch)
tree97fede0dc8e733609c45509559a47cf786fe7db1 /activemodel
parent86a12b6f2932ba1a3b5f0417688ffe34ea6bab25 (diff)
downloadrails-26cdd01eab5daf9ae17544ef44d3830dadeb8d23.tar.gz
rails-26cdd01eab5daf9ae17544ef44d3830dadeb8d23.tar.bz2
rails-26cdd01eab5daf9ae17544ef44d3830dadeb8d23.zip
Add new exception message to datetime from hash cast
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/type/date_time.rb6
-rw-r--r--activemodel/test/cases/type/date_time_test.rb11
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:)