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

Ticket #10058: tz_time_fix.diff

File tz_time_fix.diff, 1.8 kB (added by toolmantim, 10 months ago)

Fix with test against plugin trunk

  • test/active_record_methods_test.rb

    old new  
    2727    end 
    2828     
    2929    def test_should_access_utc_time_as_local_with_getter_method 
    30       @record.due_on = Time.utc(2006, 1, 1
     30      @record.instance_variable_set(:@due_on, Time.utc(2006, 1, 1)
    3131      assert_equal @record.due_on, TzTime.local(2005, 12, 31, 18) 
    3232    end 
    3333     
    3434    def test_should_fix_timezones 
    3535      @record.due_on = Time.utc(2006, 1, 1) 
    36       @record.send :fix_timezone 
    3736      assert_equal @record.due_on, TzTime.local(2006, 1, 1) 
    3837    end 
     38     
     39    def test_should_only_fix_timezones_that_have_been_written 
     40      @record.instance_variable_set(:@due_on, Time.utc(2006, 1, 1)) 
     41      assert_equal TzTime.utc(2006, 1, 1), @record.due_on 
     42    end 
    3943  end 
    4044end 
  • lib/tz_time_helpers/active_record_methods.rb

    old new  
    1818              time 
    1919            end 
    2020          end 
    21         end 
    22          
    23         protected 
    24           def fix_timezone 
    25             tz_time_attributes.each do |attribute| 
    26               time  = read_attribute(attribute) 
    27               fixed = (time.acts_like?(:time) || time.acts_like?(:date)) ? TzTime.at(time) : nil 
    28               write_attribute(attribute, fixed) 
    29             end 
     21          define_method "#{attribute}=" do |local_time| 
     22            fixed = (local_time.acts_like?(:time) || local_time.acts_like?(:date)) ? TzTime.at(local_time) : nil 
     23            write_attribute(attribute, fixed) 
    3024          end 
     25        end 
    3126      end 
    32       before_validation :fix_timezone 
    3327    end 
    3428  end 
    3529end