Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AquaGeek/971735 to your computer and use it in GitHub Desktop.
Save AquaGeek/971735 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #6062
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 1dc3933..5b112f0 100644
--- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb
@@ -14,6 +14,18 @@ class DateTime
end
end
+ # If DateTime implements it's own + and - methods, and doesn't
+ # inherit them from Date, override the methods to make them work
+ # with Durations.
+ if instance_methods(false).include?(RUBY_VERSION >= '1.9' ? :+ : '+')
+ alias_method :plus_without_duration, :+
+ alias_method :+, :plus_with_duration
+ end
+ if instance_methods(false).include?(RUBY_VERSION >= '1.9' ? :- : '-')
+ alias_method :minus_without_duration, :-
+ alias_method :-, :minus_with_duration
+ end
+
# Tells whether the DateTime object's datetime lies in the past
def past?
self < ::DateTime.current
diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb
index 342a31c..de51b50 100644
--- a/activesupport/test/core_ext/date_ext_test.rb
+++ b/activesupport/test/core_ext/date_ext_test.rb
@@ -422,6 +422,14 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
assert_equal({ :years => 3, :months => 11, :days => 2 }, options)
end
+ def test_plus_with_duration
+ assert_equal Date.tomorrow, Date.today + 1.days
+ end
+
+ def test_minus_with_duration
+ assert_equal Date.yesterday, Date.today - 1.days
+ end
+
protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb
index 7d993d8..69173fb 100644
--- a/activesupport/test/core_ext/date_time_ext_test.rb
+++ b/activesupport/test/core_ext/date_time_ext_test.rb
@@ -382,6 +382,14 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase
assert_equal 946684800, DateTime.civil(2000).to_i
end
+ def test_plus_with_duration
+ assert_equal Date.tomorrow.to_datetime, Date.today.to_datetime + 1.days
+ end
+
+ def test_minus_with_duration
+ assert_equal Date.yesterday.to_datetime, Date.today.to_datetime - 1.days
+ end
+
protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment