From e7ebd656b31244c3da5abf45e100c8e976c79d50 Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Sat, 16 Feb 2008 23:15:59 +0000 Subject: TimeWithZone #+ and #- behave consistently with numeric arguments regardless of whether wrapped time is a Time or DateTime; consistenty answers false to #acts_like?(:date) git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8884 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/lib/active_support/time_with_zone.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 15cd95772c..377eee949f 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -129,10 +129,21 @@ module ActiveSupport utc == other end - # Need to override #- to intercept situation where a Time or Time With Zone object is passed in + # If wrapped #time is a DateTime, use DateTime#since instead of #+ + # Otherwise, just pass on to #method_missing + def +(other) + time.acts_like?(:date) ? method_missing(:since, other) : method_missing(:+, other) + end + + # If a time-like object is passed in, compare it with #utc + # Else if wrapped #time is a DateTime, use DateTime#ago instead of #- # Otherwise, just pass on to method missing def -(other) - other.acts_like?(:time) ? utc - other : method_missing(:-, other) + if other.acts_like?(:time) + utc - other + else + time.acts_like?(:date) ? method_missing(:ago, other) : method_missing(:-, other) + end end def to_a @@ -176,6 +187,8 @@ module ActiveSupport # Ensure proxy class responds to all methods that underlying time instance responds to def respond_to?(sym) + # consistently respond false to acts_like?(:date), regardless of whether #time is a Time or DateTime + return false if sym.to_s == 'acts_like_date?' super || time.respond_to?(sym) end -- cgit v1.2.3