Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8884

Show
Ignore:
Timestamp:
02/16/08 23:15:59 (5 months ago)
Author:
gbuesing
Message:

TimeWithZone #+ and #- behave consistently with numeric arguments regardless of whether wrapped time is a Time or DateTime; consistenty answers false to #acts_like?(:date)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r8878 r8884  
    11*SVN* 
     2 
     3* TimeWithZone #+ and #- behave consistently with numeric arguments regardless of whether wrapped time is a Time or DateTime; consistenty answers false to #acts_like?(:date) [Geoff Buesing] 
    24 
    35* Add String#squish and String#squish! to remove consecutive chunks of whitespace.  #11123 [jordi, Henrik N] 
  • trunk/activesupport/lib/active_support/time_with_zone.rb

    r8854 r8884  
    130130    end 
    131131     
    132     # Need to override #- to intercept situation where a Time or Time With Zone object is passed in 
     132    # If wrapped #time is a DateTime, use DateTime#since instead of #+ 
     133    # Otherwise, just pass on to #method_missing 
     134    def +(other) 
     135      time.acts_like?(:date) ? method_missing(:since, other) : method_missing(:+, other) 
     136    end 
     137     
     138    # If a time-like object is passed in, compare it with #utc 
     139    # Else if wrapped #time is a DateTime, use DateTime#ago instead of #- 
    133140    # Otherwise, just pass on to method missing 
    134141    def -(other) 
    135       other.acts_like?(:time) ? utc - other : method_missing(:-, other) 
     142      if other.acts_like?(:time) 
     143        utc - other 
     144      else 
     145        time.acts_like?(:date) ? method_missing(:ago, other) : method_missing(:-, other) 
     146      end 
    136147    end 
    137148     
     
    177188    # Ensure proxy class responds to all methods that underlying time instance responds to 
    178189    def respond_to?(sym) 
     190      # consistently respond false to acts_like?(:date), regardless of whether #time is a Time or DateTime 
     191      return false if sym.to_s == 'acts_like_date?' 
    179192      super || time.respond_to?(sym) 
    180193    end 
  • trunk/activesupport/test/core_ext/time_with_zone_test.rb

    r8854 r8884  
    122122    end 
    123123       
    124     def test_plus 
     124    def test_plus_with_integer 
    125125      assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time 
     126    end 
     127       
     128    def test_plus_with_integer_when_self_wraps_datetime 
     129      datetime = DateTime.civil(2000, 1, 1, 0) 
     130      twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone) 
     131      assert_equal DateTime.civil(1999, 12, 31, 19, 0 ,5), (twz + 5).time 
    126132    end 
    127133       
     
    130136    end 
    131137       
    132     def test_minus 
     138    def test_minus_with_integer 
    133139      assert_equal Time.utc(1999, 12, 31, 18, 59 ,55), (@twz - 5).time 
     140    end 
     141     
     142    def test_minus_with_integer_when_self_wraps_datetime 
     143      datetime = DateTime.civil(2000, 1, 1, 0) 
     144      twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone) 
     145      assert_equal DateTime.civil(1999, 12, 31, 18, 59 ,55), (twz - 5).time 
    134146    end 
    135147       
     
    175187    def test_acts_like_time 
    176188      assert @twz.acts_like?(:time) 
     189      assert ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone).acts_like?(:time) 
     190    end 
     191     
     192    def test_acts_like_date 
     193      assert_equal false, @twz.acts_like?(:date) 
     194      assert_equal false, ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone).acts_like?(:date) 
    177195    end 
    178196