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

root/plugins/tztime/README

Revision 6521, 2.1 kB (checked in by rick, 1 year ago)

add #tz_time_attributes class method for ActiveRecord models to automatically change Times to TzTimes

Line 
1 TzTime
2 ======
3
4 [Boring Introduction]
5
6 TzTime is a wrapper for the Time class. It associates a time instance with a time zone, and keeps the two together. It quacks very much like a Time instance, and even provides the same extension methods that Rails adds to time objects.
7
8 By combining the time and the time zone in a single time-like class, you can simplify much of the time-zone gymnastics that you were previously forced to do.
9
10 [Exciting Introduction]
11
12 TzTime is subversive. It even _sounds_ subversive, like sharp incisors snicking together in the dark. It sneaks into your app from the inside and stuffs time zone support into the cracks. It's like a little ruby-colored rat, poking around in the under-basement of your code, but instead of chewing away at the infrastructure with its sharp little teeth (and believe me, they _are_ sharp), it builds fluffy (and oh, so comfortable!) little time-zone flavored nests wherever it can.
13
14 This look familiar?
15
16   class TasksController < ApplicationController
17     def create
18       task = account.tasks.build(params[:task])
19       task.alert_at = current_user.time_zone.local_to_utc(task.alert_at)
20       task.save!
21     end
22   end
23
24 Oh, that awful bloating sensation! Because the time zone is not globally accessible, you have to jump through hoop and ungainly hoop in your controllers...or pass the unfortunate time zone to method after method.
25
26 No more! Let the Rodent of Unusually Fine TZ Acumen aid you:
27
28   class ApplicationController < ActionController:Base
29     around_filter :set_timezone
30
31     private
32
33       def set_timezone
34         TzTime.zone = current_user.time_zone
35         yield
36         TzTime.reset!
37       end
38   end
39
40   class Task < ActiveRecord::Base
41     tz_time_attributes :alert_at
42   end
43
44   class TasksController < ApplicationController
45     def create
46       task = account.tasks.create(params[:task])
47     end
48   end
49
50 Let the rat cart the time-zone around for you! Refactor those nasty time zone conversions into your model, where they belong. Soar through the heady winds of your application's stratosphere, comfortable in the knowledge that you have helpful little rats scurrying about in the dark.
51
52 Or maybe they're gnomes.
Note: See TracBrowser for help on using the browser.