From 8cad1b880e1e794f5b301d37c524fd02e2bc721b Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Wed, 30 Jul 2008 12:22:06 +1000 Subject: [PATCH] remove setting attribute to nil in validations when invalid to preserve before_type_cast value --- lib/validates_timeliness/validations.rb | 4 +--- spec/validations_spec.rb | 10 +++++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/validates_timeliness/validations.rb b/lib/validates_timeliness/validations.rb index 0de9af4..7b4b902 100644 --- a/lib/validates_timeliness/validations.rb +++ b/lib/validates_timeliness/validations.rb @@ -72,14 +72,12 @@ module ValidatesTimeliness column = record.column_for_attribute(attr_name) begin unless time = parse_date_time(raw_value, configuration[:type]) - record.send("#{attr_name}=", nil) record.errors.add(attr_name, configuration["invalid_#{configuration[:type]}_message".to_sym]) next end validate_timeliness_restrictions(record, attr_name, time, configuration) - rescue Exception => e - record.send("#{attr_name}=", nil) + rescue Exception => e record.errors.add(attr_name, configuration["invalid_#{configuration[:type]}_message".to_sym]) end end diff --git a/spec/validations_spec.rb b/spec/validations_spec.rb index 2a79e52..6d2145a 100644 --- a/spec/validations_spec.rb +++ b/spec/validations_spec.rb @@ -83,16 +83,20 @@ describe ValidatesTimeliness::Validations do person.errors.on(:birth_time).should == "is not a valid time" end + it "should have same value for before_type_cast after failed validation" do + person.birth_date_and_time = "2000-01-01 25:02:03" + person.should_not be_valid + person.birth_date_and_time_before_type_cast.should == "2000-01-01 25:02:03" + end + it "should be valid with valid values" do person.birth_date_and_time = "2000-01-01 12:12:12" person.birth_date = "2000-01-31" person.should be_valid end - it "should be valid with values before out of Time range" do + it "should be valid with value out of range for Time class" do person.birth_date_and_time = "1890-01-01 12:12:12" - person.birth_date = "1890-01-31" - person.birth_time = "23:59:59" person.should be_valid end