diff options
author | Andrew White <andrew.white@unboxedconsulting.com> | 2016-04-03 23:43:53 +0100 |
---|---|---|
committer | Andrew White <andrew.white@unboxedconsulting.com> | 2016-04-03 23:47:46 +0100 |
commit | 08073125a5bd4cae0f2e02723e7743a389d496cd (patch) | |
tree | e290932b105597ea893565b1f62420cf6b0c4dd8 | |
parent | ae2f193c0379453f73ae08a3df541d2cb0ae00e2 (diff) | |
download | rails-08073125a5bd4cae0f2e02723e7743a389d496cd.tar.gz rails-08073125a5bd4cae0f2e02723e7743a389d496cd.tar.bz2 rails-08073125a5bd4cae0f2e02723e7743a389d496cd.zip |
Call super instead of returning nil for DateTime#<=>
The native DateTime#<=> implementation can be used to compare instances
with numeric values being considered as astronomical julian day numbers
so we should call that instead of returning nil.
Fixes #24228.
-rw-r--r-- | activesupport/CHANGELOG.md | 9 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/date_time/calculations.rb | 7 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_time_ext_test.rb | 18 |
3 files changed, 29 insertions, 5 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 0baa95cdf0..5b7077a846 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,12 @@ +* Rely on the native DateTime#<=> implementation to handle non-datetime like + objects instead of returning `nil` ourselves. This restores the ability + of `DateTime` instances to be compared with a `Numeric` that represents an + astronomical julian day number. + + Fixes #24228. + + *Andrew White* + * Add `String#upcase_first` method. *Glauco Custódio*, *bogdanvlviv* 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 95617fb8c2..ac46f5ffe8 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -165,13 +165,10 @@ class DateTime # Layers additional behavior on DateTime#<=> so that Time and # ActiveSupport::TimeWithZone instances can be compared with a DateTime. def <=>(other) - if other.kind_of?(Infinity) - super - elsif other.respond_to? :to_datetime + if other.respond_to? :to_datetime super other.to_datetime rescue nil else - nil + super end end - end diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index b183a20e0d..16efeeadd5 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -354,6 +354,24 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal nil, DateTime.civil(2000) <=> "Invalid as Time" end + def test_compare_with_integer + assert_equal 1, DateTime.civil(1970, 1, 1, 12, 0, 0) <=> 2440587 + assert_equal 0, DateTime.civil(1970, 1, 1, 12, 0, 0) <=> 2440588 + assert_equal(-1, DateTime.civil(1970, 1, 1, 12, 0, 0) <=> 2440589) + end + + def test_compare_with_float + assert_equal 1, DateTime.civil(1970) <=> 2440586.5 + assert_equal 0, DateTime.civil(1970) <=> 2440587.5 + assert_equal(-1, DateTime.civil(1970) <=> 2440588.5) + end + + def test_compare_with_rational + assert_equal 1, DateTime.civil(1970) <=> Rational(4881173, 2) + assert_equal 0, DateTime.civil(1970) <=> Rational(4881175, 2) + assert_equal(-1, DateTime.civil(1970) <=> Rational(4881177, 2)) + end + def test_to_f assert_equal 946684800.0, DateTime.civil(2000).to_f assert_equal 946684800.0, DateTime.civil(1999,12,31,19,0,0,Rational(-5,24)).to_f |