From f11255a7a33d08e0611f10f99f9a5940f6c72e89 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Mon, 26 Mar 2012 20:22:09 +1100 Subject: [PATCH] Fix multiparameter extension to not allow partial dates as per ActiveRecord implementation. --- .../extensions/multiparameter_handler.rb | 22 ++++++++++------ .../extensions/multiparameter_handler_spec.rb | 26 +++++++++++++------ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/lib/validates_timeliness/extensions/multiparameter_handler.rb b/lib/validates_timeliness/extensions/multiparameter_handler.rb index b438a8e..3da494d 100644 --- a/lib/validates_timeliness/extensions/multiparameter_handler.rb +++ b/lib/validates_timeliness/extensions/multiparameter_handler.rb @@ -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) diff --git a/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb b/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb index e034e8e..9c5ac99 100644 --- a/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +++ b/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb @@ -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