From 20ed2b16335955a686c90a618a656e80c6b79ddf Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Fri, 11 Jul 2008 17:23:37 +1000 Subject: [PATCH] ignore time part for dates in validation --- lib/validates_timeliness/validations.rb | 4 ++++ spec/validations_spec.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/validates_timeliness/validations.rb b/lib/validates_timeliness/validations.rb index b8e9dd9..682098c 100644 --- a/lib/validates_timeliness/validations.rb +++ b/lib/validates_timeliness/validations.rb @@ -34,6 +34,10 @@ module ValidatesTimeliness if type == :time # Rails dummy time date part is defined as 2000-01-01 time_array[0..2] = 2000, 1, 1 + elsif type == :date + # throw away time part and check date + time_array[3..5] = 0, 0, 0 + Date.new(*time_array[0..2]) else # Date.new enforces days per month, unlike Time Date.new(*time_array[0..2]) diff --git a/spec/validations_spec.rb b/spec/validations_spec.rb index a4751e3..1a071a8 100644 --- a/spec/validations_spec.rb +++ b/spec/validations_spec.rb @@ -146,6 +146,12 @@ describe ValidatesTimeliness::Validations do end describe "for date type" do + it "should validate with invalid time part" do + person = BasicValidation.new + person.birth_date = "1980-01-01 25:61:61" + person.should be_valid + end + describe "with before and after restrictions" do before :all do class DateBeforeAfter < Person @@ -210,6 +216,12 @@ describe ValidatesTimeliness::Validations do end describe "for time type" do + it "should validate with invalid date part" do + person = BasicValidation.new + person.birth_time = "1980-02-30 23:59:59" + person.should be_valid + end + describe "with before and after restrictions" do before :all do class TimeBeforeAfter < Person