mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
Fix multiparameter extension to not allow partial dates as per ActiveRecord implementation.
This commit is contained in:
parent
a6a3dff4d4
commit
f11255a7a3
@ -21,18 +21,24 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
|
|
||||||
def instantiate_time_object_with_timeliness(name, values)
|
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)
|
instantiate_time_object_without_timeliness(name, values)
|
||||||
else
|
}
|
||||||
invalid_multiparameter_date_or_time_as_string(values)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def instantiate_date_object(name, values)
|
def instantiate_date_object(name, values)
|
||||||
values = values.map { |v| v.nil? ? 1 : v }
|
validate_multiparameter_date_values(values) {
|
||||||
Date.new(*values)
|
Date.new(*values)
|
||||||
rescue ArgumentError => ex
|
}
|
||||||
invalid_multiparameter_date_or_time_as_string(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
|
end
|
||||||
|
|
||||||
def read_value_from_parameter_with_timeliness(name, values_from_param)
|
def read_value_from_parameter_with_timeliness(name, values_from_param)
|
||||||
|
|||||||
@ -3,26 +3,36 @@ require 'spec_helper'
|
|||||||
describe ValidatesTimeliness::Extensions::MultiparameterHandler do
|
describe ValidatesTimeliness::Extensions::MultiparameterHandler do
|
||||||
|
|
||||||
context "time column" 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 = 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
|
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 = 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
|
||||||
end
|
end
|
||||||
|
|
||||||
context "date column" do
|
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 = 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
|
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 = 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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user