diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2013-01-04 13:51:11 -0800 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2013-01-04 13:51:11 -0800 |
commit | 5b750c1891871d5dd6dae8b8099d91553ae1b024 (patch) | |
tree | 69380e2ad06fcb285f05ac5743af8b6f2d32422e /activesupport | |
parent | fba6a6a2d9137fb22cc8e6ca4d82a05dd9ba2cce (diff) | |
parent | ebd27d5714e20e6301a52989ae3c9e73f55ce29d (diff) | |
download | rails-5b750c1891871d5dd6dae8b8099d91553ae1b024.tar.gz rails-5b750c1891871d5dd6dae8b8099d91553ae1b024.tar.bz2 rails-5b750c1891871d5dd6dae8b8099d91553ae1b024.zip |
Merge pull request #8722 from kwstannard/string_to_date_change
Better error message for String#to_date
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 8 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/conversions.rb | 6 | ||||
-rw-r--r-- | activesupport/test/core_ext/string_ext_test.rb | 1 |
3 files changed, 10 insertions, 5 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index bee93ebfbc..0d2897d79d 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,5 +1,13 @@ ## Rails 4.0.0 (unreleased) ## +* Change String#to_date to use Date.parse. This gives more consistant error + messages and allows the use of partial dates. + + "gibberish".to_date => Argument Error: invalid date + "3rd Feb".to_date => Sun, 03 Feb 2013 + + *Kelly Stannard* + * It's now possible to compare Date, DateTime, Time and TimeWithZone with Infinity This allows to create date/time ranges with one infinite bound. Example: diff --git a/activesupport/lib/active_support/core_ext/string/conversions.rb b/activesupport/lib/active_support/core_ext/string/conversions.rb index 9d3b81cf38..c795df124b 100644 --- a/activesupport/lib/active_support/core_ext/string/conversions.rb +++ b/activesupport/lib/active_support/core_ext/string/conversions.rb @@ -32,11 +32,7 @@ class String # "2012-12-13".to_date #=> Thu, 13 Dec 2012 # "12/13/2012".to_date #=> ArgumentError: invalid date def to_date - unless blank? - date_values = ::Date._parse(self, false).values_at(:year, :mon, :mday) - - ::Date.new(*date_values) - end + ::Date.parse(self, false) unless blank? end # Converts a string to a DateTime value. diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index fa8839bcb3..e0ddeab548 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -307,6 +307,7 @@ class StringConversionsTest < ActiveSupport::TestCase def test_string_to_date assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date assert_nil "".to_date + assert_equal Date.new(Date.today.year, 2, 3), "Feb 3rd".to_date end end |