fixed up AR 2.0.x timezone support

This commit is contained in:
Adam Meehan 2008-05-03 08:44:41 +10:00
parent 9eddf9c130
commit 3cf6e64747
2 changed files with 6 additions and 4 deletions

View File

@ -26,7 +26,6 @@ module ValidatesTimeliness
evaluate_attribute_method attr_name, method_body evaluate_attribute_method attr_name, method_body
end end
# TODO rails 2.0 time casting better with timezone
def define_write_method_for_time_zone_conversion(attr_name) def define_write_method_for_time_zone_conversion(attr_name)
method_body = <<-EOV method_body = <<-EOV
def #{attr_name}=(time) def #{attr_name}=(time)
@ -34,7 +33,7 @@ module ValidatesTimeliness
time = time.in_time_zone time = time.in_time_zone
elsif time && time.acts_like?(:time) elsif time && time.acts_like?(:time)
# Rails 2.0.x compatibility # Rails 2.0.x compatibility
time = @@default_timezone == :utc ? time.to_time : time.to_time time = @@default_timezone == :utc ? time.utc : time.localtime
end end
write_attribute(:#{attr_name}, time) write_attribute(:#{attr_name}, time)
end end

View File

@ -33,14 +33,17 @@ describe ValidatesTimeliness::AttributeMethods do
end end
if ActiveRecord::VERSION::STRING < '2.1' if ActiveRecord::VERSION::STRING < '2.1'
it "should return stored time string as Time with correct timezone" do it "should return stored time string as Time with correct timezone" do
@person.birth_date_and_time = "1980-12-25 01:02:03" @person.birth_date_and_time = "1980-12-25 01:02:03"
@person.birth_date_and_time.zone == Time.new.zone @person.birth_date_and_time.zone == 'EST'
ActiveRecord::Base.default_timezone = :utc
@person.birth_date_and_time.zone == 'UTC'
end end
end end
unless ActiveRecord::VERSION::STRING < '2.1' unless ActiveRecord::VERSION::STRING < '2.1'
it "should return stored time string as Time with correct timezone" do it "should return stored time string as Time with correct timezone" do
Time.zone = TimeZone['Sydney'] # no I'm not from Sydney but there is no Melbourne timezone!
@person.birth_date_and_time = "1980-12-25 01:02:03" @person.birth_date_and_time = "1980-12-25 01:02:03"
@person.birth_date_and_time.zone == Time.zone @person.birth_date_and_time.zone == Time.zone
end end