From ac03ad1f78d88f40924225089a4b4bfebc8c74d8 Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Wed, 23 Jan 2008 22:22:36 +0000 Subject: Adding Time and DateTime #compare_with_coercion, which layers behavior on #<=> so that any combination of Time, DateTime and ActiveSupport::TimeWithZone instances can be chronologically compared git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8711 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_support/core_ext/date_time/calculations.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activesupport/lib/active_support/core_ext/date_time') 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 d93c1148c4..5c351c21c6 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -7,6 +7,11 @@ module ActiveSupport #:nodoc: module Calculations def self.included(base) #:nodoc: base.extend ClassMethods + + base.class_eval do + alias_method :compare_without_coercion, :<=> + alias_method :<=>, :compare_with_coercion + end end module ClassMethods @@ -91,6 +96,13 @@ module ActiveSupport #:nodoc: def utc_offset (offset * 86400).to_i end + + # Layers additional behavior on DateTime#<=> so that Time and ActiveSupport::TimeWithZone instances can be compared with a DateTime + def compare_with_coercion(other) + other = other.comparable_time if other.respond_to?(:comparable_time) + other = other.to_datetime unless other.acts_like?(:date) + compare_without_coercion(other) + end end end end -- cgit v1.2.3