Initial import

This commit is contained in:
2008-03-02 16:04:34 -03:00
commit 5e4951fa47
798 changed files with 59730 additions and 0 deletions

52
vendor/plugins/tztime/README vendored Normal file
View File

@@ -0,0 +1,52 @@
TzTime
======
[Boring Introduction]
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.
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.
[Exciting Introduction]
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.
This look familiar?
class TasksController < ApplicationController
def create
task = account.tasks.build(params[:task])
task.alert_at = current_user.time_zone.local_to_utc(task.alert_at)
task.save!
end
end
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.
No more! Let the Rodent of Unusually Fine TZ Acumen aid you:
class ApplicationController < ActionController:Base
around_filter :set_timezone
private
def set_timezone
TzTime.zone = current_user.time_zone
yield
TzTime.reset!
end
end
class Task < ActiveRecord::Base
tz_time_attributes :alert_at
end
class TasksController < ApplicationController
def create
task = account.tasks.create(params[:task])
end
end
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.
Or maybe they're gnomes.

1
vendor/plugins/tztime/init.rb vendored Normal file
View File

@@ -0,0 +1 @@
ActiveRecord::Base.extend TzTimeHelpers::ActiveRecordMethods

166
vendor/plugins/tztime/lib/tz_time.rb vendored Normal file
View File

@@ -0,0 +1,166 @@
# ---------------------------------------------------------------------------
# Copyright (c) 2007, 37signals
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ---------------------------------------------------------------------------
# A wrapper class that quacks like a Time instance, but which remembers its
# local time zone. Most operations work like normal, but some will automatically
# convert the object to utc, like #to_s(:db).
class TzTime
include Comparable
# Like the Time class, the TzTime class is its own factory. You will almost
# never create a TzTime instance via +new+. Instead, you will use something
# like TzTime.now (etc.)
class <<self
# Set and access the "local" time zone. There should be a before_filter
# somewhere in your app that ensures this is set correctly.
attr_accessor :zone
# Clears the current zone setting. This should be called from an after_filter
# to make sure time zones don't leak across requests.
def reset!
self.zone = nil
end
# Return a TzTime instance that represents the current time.
def now
new(zone.utc_to_local(Time.now.utc), zone)
end
# Return a TzTime instance for the given arguments, where the arguments
# are interpreted to be time components in the "local" time zone.
def local(*args)
new(Time.utc(*args), zone)
end
# Return a TzTime instance for the given arguments, where the arguments
# are interpreted to be time components in UTC.
def utc(*args)
new(zone.utc_to_local(Time.utc(*args)), zone)
end
# Given a time instance, return the corresponding TzTime instance. The time
# is interpreted as being in the "local" time zone, regardless of what the
# time's actual time zone value is.
def at(time)
new(time, zone)
end
# Make sure the TzTime class itself responds like the Time class.
def method_missing(sym, *args, &block)
result = Time.send(sym, *args, &block)
result = new(result, zone) if result.is_a?(Time)
result
end
end
attr_reader :time, :zone
alias_method :to_time, :time
# Create a new TzTime instance that wraps the given Time instance. The time
# is considered to be in the time zone indicated by the +zone+ parameter.
def initialize(time, zone)
@time = time.to_time
@zone = zone
end
# Wraps the Time#change method, but returns a TzTime instance for the
# changed time.
def change(options) #:nodoc:
TzTime.new(time.change(options), @zone)
end
# Adds some number of seconds to the time, and returns it wrapped in a new
# TzTime instnace.
def +(seconds)
TzTime.new(time + seconds, @zone)
end
# Subtracts some number of seconds from the time, and returns it wrapped in
# a new TzTime instnace.
def -(seconds)
TzTime.new(time - seconds, @zone)
end
# Returns a Time value, representing the wrapped time in UTC.
def utc
@utc ||= zone.local_to_utc(@time)
end
# Compares this TzTime with a time instance.
def <=>(value)
time.to_time <=> value.to_time
end
# This TzTime object always represents the local time in the associated
# timezone. Thus, #utc? should always return false, unless the zone is the
# UTC zone.
def utc?
zone.name == "UTC"
end
# Returns the underlying TZInfo::TimeZonePeriod instance for the wrapped
# time.
def period(dst=true)
t = time
begin
@period ||= zone.tzinfo.period_for_local(t, dst)
rescue TZInfo::PeriodNotFound
t -= 1.hour
retry
end
end
# Returns true if the current time is adjusted for daylight savings.
def dst?
period.dst?
end
# Returns a string repersentation of the time. For the specific case where
# +mode+ is <tt>:db</tt> or <tt>:rfc822</tt>, this will return the UTC
# representation of the time. All other conversions will use the local time.
def to_s(mode = :normal)
case mode
when :db, :rfc822 then utc.to_s(mode)
else time.to_s(mode)
end
end
# Return a reasonable representation of this TzTime object for inspection.
def inspect
"#{time.strftime("%Y-%m-%d %H:%M:%S")} #{period.abbreviation}"
end
# Because of the method_missing proxy, we want to make sure we report this
# object as responding to a method as long as the method is defined directly
# on TzTime, or if the underlying Time instance responds to the method.
def respond_to?(sym) #:nodoc:
super || @time.respond_to?(sym)
end
# Proxy anything else through to the underlying Time instance.
def method_missing(sym, *args, &block) #:nodoc:
result = @time.send(sym, *args, &block)
result = TzTime.new(result, zone) if result.is_a? Time
result
end
end

View File

@@ -0,0 +1,35 @@
module TzTimeHelpers
module ActiveRecordMethods
# Adds the given list of attributes to a class inheritable array #tz_time_attributes.
# All the attributes will have their timezones fixed in a before_save callback, and
# will have a getter method created that converts UTC times from the database into a local
# TzTime. The getter method is important because TzTime values are saved to the database
# as UTC. The getter lets you access the local time without changing your application.
def tz_time_attributes(*attributes)
class_inheritable_array :tz_time_attributes, :instance_writer => false
self.tz_time_attributes = attributes
class_eval do
attributes.each do |attribute|
define_method attribute do
time = read_attribute(attribute)
if (time.acts_like?(:time) || time.acts_like?(:date)) && time.utc?
write_attribute(attribute, TzTime.at(Time.at(TzTime.zone.utc_to_local(time))))
else
time
end
end
end
protected
def fix_timezone
tz_time_attributes.each do |attribute|
time = read_attribute(attribute)
fixed = (time.acts_like?(:time) || time.acts_like?(:date)) ? TzTime.at(time) : nil
write_attribute(attribute, fixed)
end
end
end
before_validation :fix_timezone
end
end
end

View File

@@ -0,0 +1,40 @@
require File.join(File.dirname(__FILE__), '../../../../config/environment')
require 'rubygems'
require 'test/unit'
class MockRecord
attr_writer :due_on
def self.before_validation(*args) nil end
protected
def read_attribute(attribute)
instance_variable_get("@#{attribute}")
end
def write_attribute(attribute, value)
instance_variable_set("@#{attribute}", value)
end
end
MockRecord.extend TzTimeHelpers::ActiveRecordMethods
MockRecord.tz_time_attributes :due_on
module TzTimeHelpers
class ActiveRecordMethodsTest < Test::Unit::TestCase
def setup
TzTime.zone = TimeZone["Central Time (US & Canada)"]
@record = MockRecord.new
end
def test_should_access_utc_time_as_local_with_getter_method
@record.due_on = Time.utc(2006, 1, 1)
assert_equal @record.due_on, TzTime.local(2005, 12, 31, 18)
end
def test_should_fix_timezones
@record.due_on = Time.utc(2006, 1, 1)
@record.send :fix_timezone
assert_equal @record.due_on, TzTime.local(2006, 1, 1)
end
end
end