Changed multiparameter implementation to be more like AR 3.1 but backwards compatible

Specs improved to not make direct call to multiparameter method.
This commit is contained in:
Adam Meehan
2012-02-02 09:00:05 +11:00
parent 64a7440de4
commit 43f49076fb
2 changed files with 40 additions and 63 deletions

View File

@@ -1,61 +1,34 @@
require 'spec_helper'
describe ValidatesTimeliness::Extensions::MultiparameterHandler do
let(:employee) { Employee.new }
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
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
it 'should return 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'
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
it 'should return 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)
end
end
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
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
it 'should return 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'
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
it 'should return 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)
end
end
def multiparameter_attribute(name, values)
employee.send(:execute_callstack_for_multiparameter_attributes, name.to_s => values)
def record_with_multiparameter_attribute(name, values)
hash = {}
values.each_with_index {|value, index| hash["#{name}(#{index+1}i)"] = value.to_s }
Employee.new(hash)
end
end