ignore_usec option added back

This commit is contained in:
Adam Meehan 2010-09-17 13:45:31 +10:00
parent b2d1dcdd3f
commit 8aa26a7898
3 changed files with 27 additions and 0 deletions

View File

@ -13,6 +13,11 @@ module ValidatesTimeliness
when :datetime
value.to_time
end
if options[:ignore_usec] && value.is_a?(Time)
ValidatesTimeliness::Parser.make_time(Array(value).reverse[4..9])
else
value
end
end
def dummy_time(value)

View File

@ -8,6 +8,8 @@ describe ValidatesTimeliness::Conversion do
end
describe "#type_cast_value" do
let(:options) { Hash.new }
describe "for date type" do
it "should return same value for date value" do
type_cast_value(Date.new(2010, 1, 1), :date).should == Date.new(2010, 1, 1)
@ -54,6 +56,15 @@ describe ValidatesTimeliness::Conversion do
type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :datetime).should == Time.utc(2010, 1, 1, 12, 34, 56)
end
end
describe "ignore_usec option" do
let(:options) { {:ignore_usec => true} }
it "should ignore usec on time values when evaluated" do
value = Time.utc(2010, 1, 1, 12, 34, 56, 10000)
type_cast_value(value, :datetime).should == Time.utc(2010, 1, 1, 12, 34, 56)
end
end
end
describe "#dummy_time" do

View File

@ -86,6 +86,17 @@ describe ValidatesTimeliness::Validator do
end
end
describe ":ignore_usec option" do
it "should not be valid when usec values don't match and option is false" do
Person.validates_datetime :birth_datetime, :on_or_before => Time.utc(2010,1,2,3,4,5), :ignore_usec => false
invalid!(:birth_datetime, Time.utc(2010,1,2,3,4,5,10000))
end
it "should be valid when usec values dont't match and option is true" do
Person.validates_datetime :birth_datetime, :on_or_before => Time.utc(2010,1,2,3,4,5), :ignore_usec => true
valid!(:birth_datetime, Time.utc(2010,1,2,3,4,5,10000))
end
end
describe "restriction value errors" do
let(:person) { Person.new(:birth_date => Date.today) }