diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index 5639d75..8340da6 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -62,13 +62,15 @@ module ValidatesTimeliness end def validate_restrictions(record, attr_name, value) - value = if configuration[:with_time] || configuration[:with_date] + restriction_type = type + + if configuration[:with_time] || configuration[:with_date] restriction_type = :datetime - combine_date_and_time(value, record) - else - restriction_type = type - self.class.type_cast_value(value, type, configuration[:ignore_usec]) + value = combine_date_and_time(value, record) end + + value = self.class.type_cast_value(value, restriction_type, configuration[:ignore_usec]) + return if value.nil? RESTRICTION_METHODS.each do |option, method| diff --git a/spec/validator_spec.rb b/spec/validator_spec.rb index a62bda8..b97ef2d 100644 --- a/spec/validator_spec.rb +++ b/spec/validator_spec.rb @@ -423,20 +423,31 @@ describe ValidatesTimeliness::Validator do should_have_no_error(:birth_date, :on_or_before) end + it "should should ignore usec value on combined value if :ignore_usec option is true" do + configure_validator(:type => :date, :with_time => Time.mktime(2000,1,1,12,30,0,500), :equal_to => Time.mktime(2000,1,1,12,30), :ignore_usec => true) + validate_with(:birth_date, "2000-01-01") + should_have_no_error(:birth_date, :equal_to) + end end describe "instance with :with_date option" do it "should validate time attribute as datetime combining value of :with_date against restrictions " do configure_validator(:type => :time, :with_date => '2009-01-01', :on_or_before => Time.mktime(2000,1,1,12,30)) - validate_with(:birth_date, "12:30") - should_have_error(:birth_date, :on_or_before) + validate_with(:birth_time, "12:30") + should_have_error(:birth_time, :on_or_before) end it "should skip restriction validation if :with_date value is nil" do configure_validator(:type => :time, :with_date => nil, :on_or_before => Time.mktime(2000,1,1,12,30)) - validate_with(:birth_date, "12:30") - should_have_no_error(:birth_date, :on_or_before) + validate_with(:birth_time, "12:30") + should_have_no_error(:birth_time, :on_or_before) + end + + it "should should ignore usec value on combined value if :ignore_usec option is true" do + configure_validator(:type => :time, :with_date => Date.new(2000,1,1), :on_or_before => Time.mktime(2000,1,1,12,30), :ignore_usec => true) + validate_with(:birth_time, Time.mktime(2000,1,1,12,30,0,50)) + should_have_no_error(:birth_time, :on_or_before) end end