fix ignore_usec for with_date and with_time options

This commit is contained in:
Adam Meehan 2009-09-11 13:16:03 +10:00
parent 0e382e15f2
commit df3283e5a1
2 changed files with 22 additions and 9 deletions

View File

@ -62,13 +62,15 @@ module ValidatesTimeliness
end end
def validate_restrictions(record, attr_name, value) 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 restriction_type = :datetime
combine_date_and_time(value, record) value = combine_date_and_time(value, record)
else
restriction_type = type
self.class.type_cast_value(value, type, configuration[:ignore_usec])
end end
value = self.class.type_cast_value(value, restriction_type, configuration[:ignore_usec])
return if value.nil? return if value.nil?
RESTRICTION_METHODS.each do |option, method| RESTRICTION_METHODS.each do |option, method|

View File

@ -423,20 +423,31 @@ describe ValidatesTimeliness::Validator do
should_have_no_error(:birth_date, :on_or_before) should_have_no_error(:birth_date, :on_or_before)
end 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 end
describe "instance with :with_date option" do describe "instance with :with_date option" do
it "should validate time attribute as datetime combining value of :with_date against restrictions " 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)) 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") validate_with(:birth_time, "12:30")
should_have_error(:birth_date, :on_or_before) should_have_error(:birth_time, :on_or_before)
end end
it "should skip restriction validation if :with_date value is nil" do 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)) configure_validator(:type => :time, :with_date => nil, :on_or_before => Time.mktime(2000,1,1,12,30))
validate_with(:birth_date, "12:30") validate_with(:birth_time, "12:30")
should_have_no_error(:birth_date, :on_or_before) 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
end end