aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authoryui-knk <spiketeika@gmail.com>2016-04-15 00:54:57 +0900
committeryui-knk <spiketeika@gmail.com>2016-04-17 08:23:20 +0900
commit29e876f775a5bae0a8bf60c9ec75d89681717a9c (patch)
tree2ff3c148009e24385332c80eb0ec8f287e8a6232 /activesupport
parent91798c75bc56fa706c9ed4a8dab6020462463a10 (diff)
downloadrails-29e876f775a5bae0a8bf60c9ec75d89681717a9c.tar.gz
rails-29e876f775a5bae0a8bf60c9ec75d89681717a9c.tar.bz2
rails-29e876f775a5bae0a8bf60c9ec75d89681717a9c.zip
Raise `ArgumentError` when an invalid form is passed to `Date#to_time`
Before this commit `NoMethodError: undefined method `form_name' for Time:Class` is raised when an invalid argument is passed. It is better to raise `ArgumentError` and show list of valid arguments to developers.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/date/conversions.rb1
-rw-r--r--activesupport/test/core_ext/date_ext_test.rb4
2 files changed, 5 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb
index ed8bca77ac..9a6d7bb415 100644
--- a/activesupport/lib/active_support/core_ext/date/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/date/conversions.rb
@@ -80,6 +80,7 @@ class Date
#
# date.to_time(:utc) # => 2007-11-10 00:00:00 UTC
def to_time(form = :local)
+ raise ArgumentError, "Expected :local or :utc, got #{form.inspect}." unless [:local, :utc].include?(form)
::Time.send(form, year, month, day)
end
diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb
index 0fc3f765f5..932675a50d 100644
--- a/activesupport/test/core_ext/date_ext_test.rb
+++ b/activesupport/test/core_ext/date_ext_test.rb
@@ -49,6 +49,10 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
end
end
end
+
+ assert_raise(ArgumentError) do
+ Date.new(2005, 2, 21).to_time(:tokyo)
+ end
end
def test_compare_to_time