Fix multiparameter extension to not allow partial dates as per ActiveRecord implementation.

This commit is contained in:
Adam Meehan 2012-03-26 20:22:09 +11:00
parent a6a3dff4d4
commit f11255a7a3
2 changed files with 32 additions and 16 deletions

View File

@ -21,18 +21,24 @@ module ValidatesTimeliness
end
def instantiate_time_object_with_timeliness(name, values)
if Date.valid_civil?(*values[0..2])
validate_multiparameter_date_values(values) {
instantiate_time_object_without_timeliness(name, values)
else
invalid_multiparameter_date_or_time_as_string(values)
end
}
end
def instantiate_date_object(name, values)
values = values.map { |v| v.nil? ? 1 : v }
Date.new(*values)
rescue ArgumentError => ex
invalid_multiparameter_date_or_time_as_string(values)
validate_multiparameter_date_values(values) {
Date.new(*values)
}
end
# Yield if date values are valid
def validate_multiparameter_date_values(values)
if values[0..2].all?{ |v| v.present? } && Date.valid_civil?(*values[0..2])
yield
else
invalid_multiparameter_date_or_time_as_string(values)
end
end
def read_value_from_parameter_with_timeliness(name, values_from_param)

View File

@ -3,26 +3,36 @@ require 'spec_helper'
describe ValidatesTimeliness::Extensions::MultiparameterHandler do
context "time column" do
it 'should return string value for invalid date portion' do
it 'should assign a string value for invalid date portion' do
employee = record_with_multiparameter_attribute(:birth_datetime, [2000, 2, 31, 12, 0, 0])
employee.birth_datetime_before_type_cast.should == '2000-02-31 12:00:00'
employee.birth_datetime_before_type_cast.should eq '2000-02-31 12:00:00'
end
it 'should return Time value for valid datetimes' do
it 'should assign a Time value for valid datetimes' do
employee = record_with_multiparameter_attribute(:birth_datetime, [2000, 2, 28, 12, 0, 0])
employee.birth_datetime_before_type_cast.should be_kind_of(Time)
employee.birth_datetime_before_type_cast.should eq Time.local(2000, 2, 28, 12, 0, 0)
end
it 'should assign a string value for incomplete time' do
employee = record_with_multiparameter_attribute(:birth_datetime, [2000, nil, nil])
employee.birth_datetime_before_type_cast.should eq '2000-00-00'
end
end
context "date column" do
it 'should return string value for invalid date' do
it 'should assign a string value for invalid date' do
employee = record_with_multiparameter_attribute(:birth_date, [2000, 2, 31])
employee.birth_date_before_type_cast.should == '2000-02-31'
employee.birth_date_before_type_cast.should eq '2000-02-31'
end
it 'should return Date value for valid date' do
it 'should assign a Date value for valid date' do
employee = record_with_multiparameter_attribute(:birth_date, [2000, 2, 28])
employee.birth_date_before_type_cast.should be_kind_of(Date)
employee.birth_date_before_type_cast.should eq Date.new(2000, 2, 28)
end
it 'should assign a string value for incomplete date' do
employee = record_with_multiparameter_attribute(:birth_date, [2000, nil, nil])
employee.birth_date_before_type_cast.should eq '2000-00-00'
end
end