Fix multiparameter extension to work with ActiveRecord 3.1

This commit is contained in:
Chris Griego 2011-12-29 21:49:58 -06:00 committed by Adam Meehan
parent 2702ec3266
commit 64a7440de4
2 changed files with 42 additions and 13 deletions

View File

@ -39,6 +39,7 @@ module ValidatesTimeliness
callstack.each do |name, values_with_empty_parameters| callstack.each do |name, values_with_empty_parameters|
begin begin
klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass 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? } values = values_with_empty_parameters.reject { |v| v.nil? }
if values.empty? if values.empty?

View File

@ -4,6 +4,7 @@ describe ValidatesTimeliness::Extensions::MultiparameterHandler do
let(:employee) { Employee.new } let(:employee) { Employee.new }
context "time column" do context "time column" do
context "given an array callstack as in Rails 3.0 and before" do
it 'should return string value for invalid date portion' do it 'should return string value for invalid date portion' do
multiparameter_attribute(:birth_datetime, [2000, 2, 31, 12, 0, 0]) 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 == '2000-02-31 12:00:00'
@ -15,7 +16,21 @@ describe ValidatesTimeliness::Extensions::MultiparameterHandler do
end end
end end
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 context "date column" do
context "given an array callstack as in Rails 3.0 and before" do
it 'should return string value for invalid date' do it 'should return string value for invalid date' do
multiparameter_attribute(:birth_date, [2000, 2, 31]) multiparameter_attribute(:birth_date, [2000, 2, 31])
employee.birth_date_before_type_cast.should == '2000-02-31' employee.birth_date_before_type_cast.should == '2000-02-31'
@ -27,6 +42,19 @@ describe ValidatesTimeliness::Extensions::MultiparameterHandler do
end end
end end
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
def multiparameter_attribute(name, values) def multiparameter_attribute(name, values)
employee.send(:execute_callstack_for_multiparameter_attributes, name.to_s => values) employee.send(:execute_callstack_for_multiparameter_attributes, name.to_s => values)
end end