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

Ticket #10058 (closed defect: fixed)

Opened 2 years ago

Last modified 1 year ago

[PATCH] tz_time incorrectly converts attrs that haven't been accessed

Reported by: toolmantim Assigned to: gbuesing@gmail.com
Priority: normal Milestone: 2.x
Component: Plugins Version: edge
Severity: major Keywords: verified
Cc: technoweenie

Description

tz_time's fix_timezone reinterprets a record's tz_time attributes as local time even when they haven't been set by the user. If you simply load a record and then save it the attribute will be reinterpreted as local. You normally don't stumble across this because if you call the attr_reader the problem doesn't occur.

For example:

class Promotion < ActiveRecord::Base
  tz_time_attributes :valid_from
end

TzTime.zone = TZInfo::Timezone.new("Australia/Sydney")

Promotion.find(1).attributes["valid_from"]
#=> Sun Nov 11 06:00:00 UTC 2007

Promotion.find(1).save!
Promotion.find(1).attributes["valid_from"]
#=> Sat Nov 10 19:00:00 UTC 2007

# notice it's 11 hours less... but we didn't change it!

# if we simply access the attribute before saving everything's ok
p = Promotion.find(1)
p.valid_from # here we simply access the variable before saving
p.save!

Promotion.find(1).attributes["valid_from"]
#=> Sat Nov 10 19:00:00 UTC 2007

# nothings changed. It's behaved well.

# Watching it misbehave again:

Promotion.find(1).save!
Promotion.find(1).attributes["valid_from"]
#=> Sat Nov 10 08:00:00 UTC 2007

Promotion.find(1).save!
Promotion.find(1).attributes["valid_from"]
#=> Fri Nov 09 21:00:00 UTC 2007

Promotion.find(1).save!
Promotion.find(1).attributes["valid_from"]
#=> Fri Nov 09 10:00:00 UTC 2007

# and so on, and so forth...

This problem pops its head up in an action such as this:

class PromotionsController < ApplicationController
  def publish
    promotion = Promotion.find(params[:id])
    promotion.update_attributes!(:published, true) # will reinterpret any tz_time_attributes
    redirect_to promotion
  end
end

Attached is a failing test case, as well as patch which fixes things by using an attr_writer rather than a before_validation callback.

Attachments

tz_time_fix_timezone_failing_test_case.diff (0.6 kB) - added by toolmantim on 11/02/07 06:39:05.
Failing test case against trunk of plugin
tz_time_fix.diff (1.8 kB) - added by toolmantim on 11/02/07 06:39:38.
Fix with test against plugin trunk

Change History

11/02/07 06:39:05 changed by toolmantim

  • attachment tz_time_fix_timezone_failing_test_case.diff added.

Failing test case against trunk of plugin

11/02/07 06:39:38 changed by toolmantim

  • attachment tz_time_fix.diff added.

Fix with test against plugin trunk

(follow-up: ↓ 2 ) 11/19/07 06:10:08 changed by nzkoz

I don't quite follow why you're using instance_variable_set in the tests?

(in reply to: ↑ 1 ) 11/19/07 09:14:04 changed by toolmantim

  • cc set to technoweenie.

Replying to nzkoz:

I don't quite follow why you're using instance_variable_set in the tests?

Because the bug shows up when an attribute is loaded from the DB, as opposed to being set via the accessor. It should probably have a comment saying as much.

My main concern is not so much the test, but the actual approach: moving from the before_validation callback to defining a writer method.

Maybe Rick, being the original author, has a comment on the change or reason for the original approach?

11/21/07 01:52:03 changed by nzkoz

Rick?

11/22/07 15:14:13 changed by jw

+1

(follow-up: ↓ 6 ) 12/21/07 19:04:11 changed by wadewinningham

+1 for me. I did not run into this on edge rails prior to the release of 2.0. Running on 2.0.2 it's giving me a pain. This patch gets data into my database correctly but I'm still running into issues with it converting created_at fields in email notifications generated via script/runner for some reason.

(in reply to: ↑ 5 ) 12/21/07 19:22:00 changed by wadewinningham

Replying to wadewinningham:

+1 for me. I did not run into this on edge rails prior to the release of 2.0. Running on 2.0.2 it's giving me a pain. This patch gets data into my database correctly but I'm still running into issues with it converting created_at fields in email notifications generated via script/runner for some reason.

The script/runner issue was because TzTime.zone was nil since it wasn't getting set via application.rb. By setting it, the script/runner issue was resolved. Regardless I still had the issue which this plugin fixed.

01/08/08 00:04:08 changed by dylanegan

+1 Great patch that I will make use of.

01/08/08 00:08:02 changed by toolmantim

  • keywords set to verified.

01/30/08 21:49:32 changed by zackchandler

+1

looks like ActiveSupport is gettine some time zone lovin lately - I really hope this problem is fixed soon!

01/30/08 22:06:43 changed by toolmantim

though i think this plugin will be reduced to simply the AR-magic component once the TZ additions are complete

01/30/08 22:41:01 changed by nzkoz

We intend to merge AR TZ stuff into core too, so this plugin won't be used after 2.1

however if this patch can be updated to apply cleanly, I'm happy to apply it :)

02/07/08 17:25:32 changed by rick

  • status changed from new to closed.
  • resolution set to fixed.

(In [8815]) remove fix_timezone before_validation hook and convert times as they are set. Closes #10058 [toolmantim]

03/11/08 22:15:15 changed by lindvall

  • status changed from closed to reopened.
  • resolution deleted.

This patch didn't end up being quite right. See #11132 for the fix.

03/13/08 02:17:56 changed by david

  • owner changed from minam to rick.
  • status changed from reopened to new.

03/27/08 17:56:24 changed by david

  • owner changed from rick to gbuesing@gmail.com.

03/27/08 18:13:47 changed by technoweenie

Oh, I think [9058] should've closed this. I don't know why trac wasn't updated.

03/27/08 18:38:44 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

Fixed by [9058]