aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVipul A M <vipulnsward@gmail.com>2016-02-15 18:16:51 +0530
committerVipul A M <vipulnsward@gmail.com>2016-02-15 18:16:51 +0530
commit02a9e03c352e7c10e379368f6b41b0e2eaede07c (patch)
tree2d0f9f4a4eb8d6718c80958d5a6fd85d7b71e222
parentf800e00f9a7150f81dc2d27c26e6d1164df776f3 (diff)
downloadrails-02a9e03c352e7c10e379368f6b41b0e2eaede07c.tar.gz
rails-02a9e03c352e7c10e379368f6b41b0e2eaede07c.tar.bz2
rails-02a9e03c352e7c10e379368f6b41b0e2eaede07c.zip
Add `#on_weekday?` method to `Date`, `Time`, and `DateTime`.
-rw-r--r--activesupport/CHANGELOG.md8
-rw-r--r--activesupport/lib/active_support/core_ext/date_and_time/calculations.rb5
-rw-r--r--activesupport/test/core_ext/date_and_time_behavior.rb10
-rw-r--r--guides/source/5_0_release_notes.md2
4 files changed, 24 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 3d4cc8fae6..f4c324803c 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,8 +1,16 @@
+* Add `#on_weekday?` method to `Date`, `Time`, and `DateTime`.
+
+ `#on_weekday?` returns `true` if the receiving date/time does not fall on a Saturday
+ or Sunday.
+
+ *Vipul A M*
+
* Add `Array#second_to_last` and `Array#third_to_last` methods.
*Brian Christian*
* Fix regression in `Hash#dig` for HashWithIndifferentAccess.
+
*Jon Moss*
## Rails 5.0.0.beta2 (February 01, 2016) ##
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 e079af594d..2de0d19a7e 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
@@ -51,6 +51,11 @@ module DateAndTime
WEEKEND_DAYS.include?(wday)
end
+ # Returns true if the date/time does not fall on a Saturday or Sunday.
+ def on_weekday?
+ !WEEKEND_DAYS.include?(wday)
+ end
+
# Returns a new date/time the specified number of days ago.
def days_ago(days)
advance(:days => -days)
diff --git a/activesupport/test/core_ext/date_and_time_behavior.rb b/activesupport/test/core_ext/date_and_time_behavior.rb
index 784547bdf8..54df87def8 100644
--- a/activesupport/test/core_ext/date_and_time_behavior.rb
+++ b/activesupport/test/core_ext/date_and_time_behavior.rb
@@ -301,6 +301,16 @@ module DateAndTimeBehavior
assert_not date_time_init(2015,1,5,15,15,10).on_weekend?
end
+ def test_on_weekday_on_sunday
+ assert_not date_time_init(2015,1,4,0,0,0).on_weekday?
+ assert_not date_time_init(2015,1,4,15,15,10).on_weekday?
+ end
+
+ def test_on_weekday_on_monday
+ assert date_time_init(2015,1,5,0,0,0).on_weekday?
+ assert date_time_init(2015,1,5,15,15,10).on_weekday?
+ end
+
def with_bw_default(bw = :monday)
old_bw = Date.beginning_of_week
Date.beginning_of_week = bw
diff --git a/guides/source/5_0_release_notes.md b/guides/source/5_0_release_notes.md
index 4e8252f85b..5ae78eba04 100644
--- a/guides/source/5_0_release_notes.md
+++ b/guides/source/5_0_release_notes.md
@@ -705,7 +705,7 @@ Please refer to the [Changelog][active-support] for detailed changes.
* Changed the default test order from `:sorted` to `:random`.
([commit](https://github.com/rails/rails/commit/5f777e4b5ee2e3e8e6fd0e2a208ec2a4d25a960d))
-* Added `#on_weekend?`, `#next_weekday`, `#prev_weekday` methods to `Date`,
+* Added `#on_weekend?`, `#on_weekday?`, `#next_weekday`, `#prev_weekday` methods to `Date`,
`Time`, and `DateTime`.
([Pull Request](https://github.com/rails/rails/pull/18335))