refactored to convert and cache user input value on write to distinguish from values in read raw from database which allows correct timezone conversion

added override for read_attribute to check for failed time type cast which should save nil value
This commit is contained in:
Adam Meehan
2008-06-30 15:38:33 +10:00
parent ec12d60224
commit ed23c4a9a4
2 changed files with 129 additions and 111 deletions

View File

@@ -1,5 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
# TODO test dirty
describe ValidatesTimeliness::AttributeMethods do
include ValidatesTimeliness::AttributeMethods
@@ -7,26 +8,6 @@ describe ValidatesTimeliness::AttributeMethods do
@person = Person.new
end
describe "read_attribute" do
it "should return time object from time string" do
@attributes = {}
self.stub!(:column_for_attribute).and_return( mock('Column', :klass => Time) )
self.stub!(:unserializable_attribute?).and_return(false)
@attributes['birth_date_and_time'] = "1980-01-01 00:00:00"
read_attribute(:birth_date_and_time).should be_kind_of(Time)
end
it "should return nil from invalid time string" do
@attributes = {}
self.stub!(:column_for_attribute).and_return( mock('Column', :klass => Time) )
self.stub!(:unserializable_attribute?).and_return(false)
@attributes['birth_date_and_time'] = "1980-02-30 00:00:00"
read_attribute(:birth_date_and_time).should be_nil
end
end
describe "strict_time_type_cast" do
it "should return time object for valid time string" do
strict_time_type_cast("2000-01-01 12:13:14").should be_kind_of(Time)
@@ -46,9 +27,9 @@ describe ValidatesTimeliness::AttributeMethods do
if RAILS_VER >= '2.1'
it "should convert time string into current timezone" do
Time.zone = 'Melbourne'
time = strict_time_type_cast("2000-01-01 12:13:14")
Time.zone.utc_offset.should == 0
time.zone.should == 'UTC'
Time.zone.utc_offset.should == 10.hours
end
end
end
@@ -69,14 +50,20 @@ describe ValidatesTimeliness::AttributeMethods do
@person.birth_date_and_time.should be_kind_of(Time)
end
# This fails running as plugin under vendor using Rails 2.1RC
# due to write_attribute_with_dirty ignoring the write method for time zone
# method. But invalid dates do return nil when running app.
it "should return nil when time is invalid" do
@person.birth_date_and_time = "2000-02-30 01:02:03"
@person.birth_date_and_time.should be_nil
end
it "should not save invalid date value to database" do
time_string = "2000-02-30 09:00:00"
@person = Person.new
@person.birth_date_and_time = time_string
@person.save
@person.reload
@person.birth_date_and_time_before_type_cast.should be_nil
end
unless RAILS_VER < '2.1'
it "should return stored time string as Time with correct timezone" do
Time.zone = 'Melbourne'
@@ -85,20 +72,27 @@ describe ValidatesTimeliness::AttributeMethods do
@person.birth_date_and_time.utc_offset.should == 10.hours
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S').should == time_string
end
it "should return time object from database in correct timezone" do
Time.zone = 'Melbourne'
time_string = "2000-06-01 09:00:00"
@person = Person.new
@person.birth_date_and_time = time_string
@person.save
@person.reload
@person.birth_date_and_time.to_s(:db).should == time_string
end
end
describe "time attribute persistance" do
unless RAILS_VER < '2.1'
it "should return time object from database in correct timezone" do
Time.zone = 'Melbourne'
time_string = "1980-06-01 09:00:00"
@person = Person.new
@person.birth_date_and_time = time_string
@person.save
@person.reload
@person.birth_date_and_time.to_s.should == time_string
end
end
it "should return same time object on repeat reads" do
Time.zone = 'Melbourne' unless RAILS_VER < '2.1'
time_string = "2000-06-01 09:00:00"
@person = Person.new
@person.birth_date_and_time = time_string
@person.save
@person.reload
time = @person.birth_date_and_time
@person.birth_date_and_time.should == time
end
end