aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-04-01 06:52:38 +0900
committerGitHub <noreply@github.com>2018-04-01 06:52:38 +0900
commit87d9228d9d2225296f68795ef685f617a844f4df (patch)
tree2bdf3da757a5030b1c9f91c0c836ea4222501f07
parent11b884e53455ae0eb95835b88e20c369b7c708dd (diff)
parent4e68b159d5696ca58c015a790ef3ceacb90b27c1 (diff)
downloadrails-87d9228d9d2225296f68795ef685f617a844f4df.tar.gz
rails-87d9228d9d2225296f68795ef685f617a844f4df.tar.bz2
rails-87d9228d9d2225296f68795ef685f617a844f4df.zip
Merge pull request #32398 from bogdanvlviv/32185-dry-in-before-after-mehtods-implementation
Move implementation of `before?` and `after?` to `DateAndTime::Calculations`
-rw-r--r--activesupport/lib/active_support/core_ext/date/calculations.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/date_and_time/calculations.rb10
-rw-r--r--activesupport/lib/active_support/core_ext/date_time/calculations.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb2
-rw-r--r--activesupport/test/core_ext/date_and_time_behavior.rb12
-rw-r--r--activesupport/test/core_ext/date_ext_test.rb12
-rw-r--r--activesupport/test/core_ext/date_time_ext_test.rb12
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb12
8 files changed, 22 insertions, 42 deletions
diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb
index e7be5c1376..1cd7acb05d 100644
--- a/activesupport/lib/active_support/core_ext/date/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -142,6 +142,4 @@ class Date
end
alias_method :compare_without_coercion, :<=>
alias_method :<=>, :compare_with_coercion
- alias_method :before?, :<
- alias_method :after?, :>
end
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 f6cb1a384c..de13f00e60 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
@@ -60,6 +60,16 @@ module DateAndTime
!WEEKEND_DAYS.include?(wday)
end
+ # Returns true if the date/time before <tt>date_or_time</tt>.
+ def before?(date_or_time)
+ self < date_or_time
+ end
+
+ # Returns true if the date/time after <tt>date_or_time</tt>.
+ def after?(date_or_time)
+ self > date_or_time
+ end
+
# Returns a new date/time the specified number of days ago.
def days_ago(days)
advance(days: -days)
diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb
index eace125883..e61b23f842 100644
--- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb
@@ -208,6 +208,4 @@ class DateTime
super
end
end
- alias_method :before?, :<
- alias_method :after?, :>
end
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index 0507f9e652..120768dec5 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -302,8 +302,6 @@ class Time
end
alias_method :compare_without_coercion, :<=>
alias_method :<=>, :compare_with_coercion
- alias_method :before?, :<
- alias_method :after?, :>
# Layers additional behavior on Time#eql? so that ActiveSupport::TimeWithZone instances
# can be eql? to an equivalent Time
diff --git a/activesupport/test/core_ext/date_and_time_behavior.rb b/activesupport/test/core_ext/date_and_time_behavior.rb
index 1422f135a8..b77ea22701 100644
--- a/activesupport/test/core_ext/date_and_time_behavior.rb
+++ b/activesupport/test/core_ext/date_and_time_behavior.rb
@@ -385,6 +385,18 @@ module DateAndTimeBehavior
assert_predicate date_time_init(2015, 1, 5, 15, 15, 10), :on_weekday?
end
+ def test_before
+ assert_equal false, date_time_init(2017, 3, 6, 12, 0, 0).before?(date_time_init(2017, 3, 5, 12, 0, 0))
+ assert_equal false, date_time_init(2017, 3, 6, 12, 0, 0).before?(date_time_init(2017, 3, 6, 12, 0, 0))
+ assert_equal true, date_time_init(2017, 3, 6, 12, 0, 0).before?(date_time_init(2017, 3, 7, 12, 0, 0))
+ end
+
+ def test_after
+ assert_equal true, date_time_init(2017, 3, 6, 12, 0, 0).after?(date_time_init(2017, 3, 5, 12, 0, 0))
+ assert_equal false, date_time_init(2017, 3, 6, 12, 0, 0).after?(date_time_init(2017, 3, 6, 12, 0, 0))
+ assert_equal false, date_time_init(2017, 3, 6, 12, 0, 0).after?(date_time_init(2017, 3, 7, 12, 0, 0))
+ end
+
def with_bw_default(bw = :monday)
old_bw = Date.beginning_of_week
Date.beginning_of_week = bw
diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb
index 8d3d575e0f..b8652884ce 100644
--- a/activesupport/test/core_ext/date_ext_test.rb
+++ b/activesupport/test/core_ext/date_ext_test.rb
@@ -360,18 +360,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
end
end
- def test_before
- assert_equal false, Date.new(2017, 3, 6).before?(Date.new(2017, 3, 5))
- assert_equal false, Date.new(2017, 3, 6).before?(Date.new(2017, 3, 6))
- assert_equal true, Date.new(2017, 3, 6).before?(Date.new(2017, 3, 7))
- end
-
- def test_after
- assert_equal true, Date.new(2017, 3, 6).after?(Date.new(2017, 3, 5))
- assert_equal false, Date.new(2017, 3, 6).after?(Date.new(2017, 3, 6))
- assert_equal false, Date.new(2017, 3, 6).after?(Date.new(2017, 3, 7))
- end
-
def test_current_returns_date_today_when_zone_not_set
with_env_tz "US/Central" do
Time.stub(:now, Time.local(1999, 12, 31, 23)) do
diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb
index a6124197ae..894fb80cba 100644
--- a/activesupport/test/core_ext/date_time_ext_test.rb
+++ b/activesupport/test/core_ext/date_time_ext_test.rb
@@ -285,18 +285,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase
end
end
- def test_before
- assert_equal false, DateTime.civil(2017, 3, 6, 12, 0, 0).before?(DateTime.civil(2017, 3, 6, 11, 59, 59))
- assert_equal false, DateTime.civil(2017, 3, 6, 12, 0, 0).before?(DateTime.civil(2017, 3, 6, 12, 0, 0))
- assert_equal true, DateTime.civil(2017, 3, 6, 12, 0, 0).before?(DateTime.civil(2017, 3, 6, 12, 00, 1))
- end
-
- def test_after
- assert_equal true, DateTime.civil(2017, 3, 6, 12, 0, 0).after?(DateTime.civil(2017, 3, 6, 11, 59, 59))
- assert_equal false, DateTime.civil(2017, 3, 6, 12, 0, 0).after?(DateTime.civil(2017, 3, 6, 12, 0, 0))
- assert_equal false, DateTime.civil(2017, 3, 6, 12, 0, 0).after?(DateTime.civil(2017, 3, 6, 12, 00, 1))
- end
-
def test_current_returns_date_today_when_zone_is_not_set
with_env_tz "US/Eastern" do
Time.stub(:now, Time.local(1999, 12, 31, 23, 59, 59)) do
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index 0d16e51087..e1cb22fda8 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -736,18 +736,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
end
end
- def test_before
- assert_equal false, Time.utc(2017, 3, 6, 12, 0, 0).before?(Time.utc(2017, 3, 6, 11, 59, 59))
- assert_equal false, Time.utc(2017, 3, 6, 12, 0, 0).before?(Time.utc(2017, 3, 6, 12, 0, 0))
- assert_equal true, Time.utc(2017, 3, 6, 12, 0, 0).before?(Time.utc(2017, 3, 6, 12, 00, 1))
- end
-
- def test_after
- assert_equal true, Time.utc(2017, 3, 6, 12, 0, 0).after?(Time.utc(2017, 3, 6, 11, 59, 59))
- assert_equal false, Time.utc(2017, 3, 6, 12, 0, 0).after?(Time.utc(2017, 3, 6, 12, 0, 0))
- assert_equal false, Time.utc(2017, 3, 6, 12, 0, 0).after?(Time.utc(2017, 3, 6, 12, 00, 1))
- end
-
def test_acts_like_time
assert_predicate Time.new, :acts_like_time?
end