aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2013-06-03 16:42:41 +0200
committerDavid Heinemeier Hansson <david@loudthinking.com>2013-06-03 16:47:48 +0200
commita6fe33d8a3c09f3a70ee2a79f96ed5b2c9580d4f (patch)
treeea4a9dcced4302e0808ae46190d15ae11b06b600
parentbb3d9a6f6c74dc1591639b779bf4272b272f93df (diff)
downloadrails-a6fe33d8a3c09f3a70ee2a79f96ed5b2c9580d4f.tar.gz
rails-a6fe33d8a3c09f3a70ee2a79f96ed5b2c9580d4f.tar.bz2
rails-a6fe33d8a3c09f3a70ee2a79f96ed5b2c9580d4f.zip
Allow Date to be compared with Time (like it was possible to compare Time with Date)
-rw-r--r--activesupport/lib/active_support/core_ext/date/calculations.rb11
-rw-r--r--activesupport/test/core_ext/date_ext_test.rb4
2 files changed, 15 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb
index 106a65610c..c7e8b6ae25 100644
--- a/activesupport/lib/active_support/core_ext/date/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -119,4 +119,15 @@ class Date
options.fetch(:day, day)
)
end
+
+ # Allow Dates to be compared with times. The Date will have its time set to 00:00:00 for the comparison.
+ def compare_with_coercion(other)
+ if other.is_a?(Time)
+ self.to_time <=> other
+ else
+ compare_without_coercion(other)
+ end
+ end
+ alias_method :compare_without_coercion, :<=>
+ alias_method :<=>, :compare_with_coercion
end
diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb
index b1adf6d396..aa5e4461fd 100644
--- a/activesupport/test/core_ext/date_ext_test.rb
+++ b/activesupport/test/core_ext/date_ext_test.rb
@@ -48,6 +48,10 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
end
end
+ def test_compare_to_time
+ assert Date.yesterday < Time.now
+ end
+
def test_to_datetime
assert_equal DateTime.civil(2005, 2, 21), Date.new(2005, 2, 21).to_datetime
assert_equal 0, Date.new(2005, 2, 21).to_datetime.offset # use UTC offset