diff options
author | George Claghorn <george.claghorn@gmail.com> | 2015-01-06 05:30:04 -0500 |
---|---|---|
committer | George Claghorn <george.claghorn@gmail.com> | 2015-01-06 05:43:41 -0500 |
commit | 4c53f58a0fc89dd8bf0ea9abd40b7a198dd13b99 (patch) | |
tree | 9878b6428d0c6f7087050d3025d83785cd54282f | |
parent | b6d0d0d106b5cd59f8da3f893b29a05bc85ac14e (diff) | |
download | rails-4c53f58a0fc89dd8bf0ea9abd40b7a198dd13b99.tar.gz rails-4c53f58a0fc89dd8bf0ea9abd40b7a198dd13b99.tar.bz2 rails-4c53f58a0fc89dd8bf0ea9abd40b7a198dd13b99.zip |
Add #prev_day and #next_day as counterparts to #yesterday and #tomorrow for Date, Time, and DateTime
-rw-r--r-- | activesupport/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/date_and_time/calculations.rb | 22 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_and_time_behavior.rb | 10 |
3 files changed, 31 insertions, 6 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index f301cbbd87..2756f7e0e2 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,8 @@ +* Add `#prev_day` and `#next_day` counterparts to `#yesterday` and + `#tomorrow` for `Date`, `Time`, and `DateTime`. + + *George Claghorn* + * Add `same_time` option to `#next_week` and `#prev_week` for `Date`, `Time`, and `DateTime`. diff --git a/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb index 5f16794926..e4bfd9f7c5 100644 --- a/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb @@ -13,12 +13,22 @@ module DateAndTime # Returns a new date/time representing yesterday. def yesterday - advance(:days => -1) + advance(days: -1) + end + + # Returns a new date/time representing the previous day. + def prev_day + advance(days: -1) end # Returns a new date/time representing tomorrow. def tomorrow - advance(:days => 1) + advance(days: 1) + end + + # Returns a new date/time representing the next day. + def next_day + advance(days: 1) end # Returns true if the date/time is today. @@ -125,10 +135,10 @@ module DateAndTime # Returns a new date/time representing the next weekday. def next_weekday - if tomorrow.on_weekend? + if next_day.on_weekend? next_week(:monday, same_time: true) else - tomorrow + next_day end end @@ -159,10 +169,10 @@ module DateAndTime # Returns a new date/time representing the previous weekday. def prev_weekday - if yesterday.on_weekend? + if prev_day.on_weekend? copy_time_to(beginning_of_week(:friday)) else - yesterday + prev_day end end alias_method :last_weekday, :prev_weekday diff --git a/activesupport/test/core_ext/date_and_time_behavior.rb b/activesupport/test/core_ext/date_and_time_behavior.rb index 6b6e1a2a4c..784547bdf8 100644 --- a/activesupport/test/core_ext/date_and_time_behavior.rb +++ b/activesupport/test/core_ext/date_and_time_behavior.rb @@ -6,11 +6,21 @@ module DateAndTimeBehavior assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,3,2,10,10,10).yesterday.yesterday end + def test_prev_day + assert_equal date_time_init(2005,2,21,10,10,10), date_time_init(2005,2,22,10,10,10).prev_day + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,3,2,10,10,10).prev_day.prev_day + end + def test_tomorrow assert_equal date_time_init(2005,2,23,10,10,10), date_time_init(2005,2,22,10,10,10).tomorrow assert_equal date_time_init(2005,3,2,10,10,10), date_time_init(2005,2,28,10,10,10).tomorrow.tomorrow end + def test_next_day + assert_equal date_time_init(2005,2,23,10,10,10), date_time_init(2005,2,22,10,10,10).next_day + assert_equal date_time_init(2005,3,2,10,10,10), date_time_init(2005,2,28,10,10,10).next_day.next_day + end + def test_days_ago assert_equal date_time_init(2005,6,4,10,10,10), date_time_init(2005,6,5,10,10,10).days_ago(1) assert_equal date_time_init(2005,5,31,10,10,10), date_time_init(2005,6,5,10,10,10).days_ago(5) |