From 64a7440de4e5acdf6cfe60f7c040fa553f00f224 Mon Sep 17 00:00:00 2001 From: Chris Griego Date: Thu, 29 Dec 2011 21:49:58 -0600 Subject: [PATCH] Fix multiparameter extension to work with ActiveRecord 3.1 --- .../extensions/multiparameter_handler.rb | 1 + .../extensions/multiparameter_handler_spec.rb | 54 ++++++++++++++----- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/validates_timeliness/extensions/multiparameter_handler.rb b/lib/validates_timeliness/extensions/multiparameter_handler.rb index ddbe687..920aef4 100644 --- a/lib/validates_timeliness/extensions/multiparameter_handler.rb +++ b/lib/validates_timeliness/extensions/multiparameter_handler.rb @@ -39,6 +39,7 @@ module ValidatesTimeliness callstack.each do |name, values_with_empty_parameters| begin klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass + values_with_empty_parameters = values_with_empty_parameters.to_a.sort_by { |v| v.first }.map { |v| v.last } if Hash === values_with_empty_parameters values = values_with_empty_parameters.reject { |v| v.nil? } if values.empty? diff --git a/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb b/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb index c5ec057..dd14757 100644 --- a/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +++ b/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb @@ -4,26 +4,54 @@ describe ValidatesTimeliness::Extensions::MultiparameterHandler do let(:employee) { Employee.new } context "time column" do - it 'should return string value for invalid date portion' do - multiparameter_attribute(:birth_datetime, [2000, 2, 31, 12, 0, 0]) - employee.birth_datetime_before_type_cast.should == '2000-02-31 12:00:00' + context "given an array callstack as in Rails 3.0 and before" do + it 'should return string value for invalid date portion' do + multiparameter_attribute(:birth_datetime, [2000, 2, 31, 12, 0, 0]) + employee.birth_datetime_before_type_cast.should == '2000-02-31 12:00:00' + end + + it 'should return Time value for valid datetimes' do + multiparameter_attribute(:birth_datetime, [2000, 2, 28, 12, 0, 0]) + employee.birth_datetime_before_type_cast.should be_kind_of(Time) + end end - - it 'should return Time value for valid datetimes' do - multiparameter_attribute(:birth_datetime, [2000, 2, 28, 12, 0, 0]) - employee.birth_datetime_before_type_cast.should be_kind_of(Time) + + context "given a hash callstack as in Rails 3.1+" do + it 'should return string value for invalid date portion' do + multiparameter_attribute(:birth_datetime, { 1 => 2000, 2 => 2, 3 => 31, 4 => 12, 5 => 0, 6 => 0 }) + employee.birth_datetime_before_type_cast.should == '2000-02-31 12:00:00' + end + + it 'should return Time value for valid datetimes' do + multiparameter_attribute(:birth_datetime, { 1 => 2000, 2 => 2, 3 => 28, 4 => 12, 5 => 0, 6 => 0 }) + employee.birth_datetime_before_type_cast.should be_kind_of(Time) + end end end context "date column" do - it 'should return string value for invalid date' do - multiparameter_attribute(:birth_date, [2000, 2, 31]) - employee.birth_date_before_type_cast.should == '2000-02-31' + context "given an array callstack as in Rails 3.0 and before" do + it 'should return string value for invalid date' do + multiparameter_attribute(:birth_date, [2000, 2, 31]) + employee.birth_date_before_type_cast.should == '2000-02-31' + end + + it 'should return Date value for valid date' do + multiparameter_attribute(:birth_date, [2000, 2, 28]) + employee.birth_date_before_type_cast.should be_kind_of(Date) + end end - it 'should return Date value for valid date' do - multiparameter_attribute(:birth_date, [2000, 2, 28]) - employee.birth_date_before_type_cast.should be_kind_of(Date) + context "given a hash callstack as in Rails 3.1+" do + it 'should return string value for invalid date' do + multiparameter_attribute(:birth_date, { 1 => 2000, 2 => 2, 3 => 31 }) + employee.birth_date_before_type_cast.should == '2000-02-31' + end + + it 'should return Date value for valid date' do + multiparameter_attribute(:birth_date, { 1 => 2000, 2 => 2, 3 => 28 }) + employee.birth_date_before_type_cast.should be_kind_of(Date) + end end end