diff --git a/lib/validates_timeliness/attribute_methods.rb b/lib/validates_timeliness/attribute_methods.rb index c535883..df859b6 100644 --- a/lib/validates_timeliness/attribute_methods.rb +++ b/lib/validates_timeliness/attribute_methods.rb @@ -16,8 +16,8 @@ module ValidatesTimeliness # for any subsequent differentiation. # # The wholesale replacement of the Rails time type casting is not done to - # preserve the quick conversion for timestamp columns and also any value which - # is never changed during the life of the record object. + # preserve the quickest conversion for timestamp columns and also any value + # which is never changed during the life of the record object. # # Dates are also handled but only write to cache value converted by plugin # parser. Default read method will retrieve from cache or do default @@ -35,7 +35,8 @@ module ValidatesTimeliness # Adds check for cached time attributes which have been type cast already # and value can be used from cache. This prevents the raw time value - # from being type cast using default Rails type casting. + # from being type cast using default Rails type casting when writing values + # to the database. def read_attribute(attr_name) attr_name = attr_name.to_s if !(value = @attributes[attr_name]).nil? diff --git a/spec/validations_spec.rb b/spec/validations_spec.rb index 55a78d8..d29d252 100644 --- a/spec/validations_spec.rb +++ b/spec/validations_spec.rb @@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/spec_helper' describe ValidatesTimeliness::Validations do before :all do # freezes time using time_travel plugin - Time.now = Time.utc(2008, 1, 1, 12, 0, 0) + Time.now = Time.utc(2000, 1, 1, 0, 0, 0) end after :all do @@ -34,11 +34,7 @@ describe ValidatesTimeliness::Validations do Time.zone.utc_offset.should == 10.hours end end - - it "should return Date object valid date string" do - parse_method("2000-02-01", :date).should be_kind_of(Date) - end - + it "should return nil for invalid date string" do parse_method("2000-02-30", :date).should be_nil end @@ -62,19 +58,19 @@ describe ValidatesTimeliness::Validations do end it "should have error for invalid date component for datetime column" do - @person.birth_date_and_time = "1980-02-30 01:02:03" + @person.birth_date_and_time = "2000-02-30 01:02:03" @person.should_not be_valid @person.errors.on(:birth_date_and_time).should == "is not a valid datetime" end it "should have error for invalid time component for datetime column" do - @person.birth_date_and_time = "1980-02-30 25:02:03" + @person.birth_date_and_time = "2000-02-30 25:02:03" @person.should_not be_valid @person.errors.on(:birth_date_and_time).should == "is not a valid datetime" end it "should have error for invalid date value for date column" do - @person.birth_date = "1980-02-30" + @person.birth_date = "2000-02-30" @person.should_not be_valid @person.errors.on(:birth_date).should == "is not a valid date" end @@ -86,8 +82,8 @@ describe ValidatesTimeliness::Validations do end it "should be valid with valid values" do - @person.birth_date_and_time = "1980-01-31 12:12:12" - @person.birth_date = "1980-01-31" + @person.birth_date_and_time = "2000-01-31 12:12:12" + @person.birth_date = "2000-01-31" @person.should be_valid end @@ -337,12 +333,13 @@ describe ValidatesTimeliness::Validations do describe "with mixed value and restriction types" do before :all do + class MixedBeforeAndAfter < Person validates_timeliness_of :birth_date_and_time, :before => Date.new(2008,1,2), - :after => lambda { Time.mktime(2008, 1, 1) } + :after => lambda { "2008-01-01" } validates_timeliness_of :birth_date, :type => :date, - :on_or_before => lambda { Time.mktime(2008, 1, 2) }, + :on_or_before => lambda { "2008-01-01" }, :on_or_after => :birth_date_and_time end end @@ -379,7 +376,7 @@ describe ValidatesTimeliness::Validations do end - describe "ignoring rstriction errors" do + describe "ignoring restriction errors" do before :all do class BadRestriction < Person validates_date :birth_date, :before => Proc.new { raise }