Fixed :between option.

This commit is contained in:
Edgars Beigarts 2011-01-21 11:02:59 +02:00
parent 01d1dc7c5e
commit d14ae09820
2 changed files with 12 additions and 3 deletions

View File

@ -27,12 +27,13 @@ module ValidatesTimeliness
def initialize(options)
@type = options.delete(:type) || :datetime
@allow_nil, @allow_blank = options.delete(:allow_nil), options.delete(:allow_blank)
@restrictions_to_check = RESTRICTIONS.keys & options.keys
if range = options.delete(:between)
raise ArgumentError, ":between must be a Range or an Array" unless range.is_a?(Range) || range.is_a?(Array)
options[:on_or_after], options[:on_or_before] = range.first, range.last
end
@restrictions_to_check = RESTRICTIONS.keys & options.keys
super
end

View File

@ -70,18 +70,26 @@ describe ValidatesTimeliness::Validator do
describe "array value" do
it 'should be split option into :on_or_after and :on_or_before values' do
on_or_after, on_or_before = Date.new(2010,1,1), Date.new(2010,1,2)
Person.validates_time :birth_date, :between => [on_or_after, on_or_before]
Person.validates_date :birth_date, :between => [on_or_after, on_or_before]
Person.validators.first.options[:on_or_after].should == on_or_after
Person.validators.first.options[:on_or_before].should == on_or_before
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
invalid!(:birth_date, on_or_before + 1, "must be on or before 2010-01-02")
valid!(:birth_date, on_or_after)
valid!(:birth_date, on_or_before)
end
end
describe "range value" do
it 'should be split option into :on_or_after and :on_or_before values' do
on_or_after, on_or_before = Date.new(2010,1,1), Date.new(2010,1,2)
Person.validates_time :birth_date, :between => on_or_after..on_or_before
Person.validates_date :birth_date, :between => on_or_after..on_or_before
Person.validators.first.options[:on_or_after].should == on_or_after
Person.validators.first.options[:on_or_before].should == on_or_before
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
invalid!(:birth_date, on_or_before + 1, "must be on or before 2010-01-02")
valid!(:birth_date, on_or_after)
valid!(:birth_date, on_or_before)
end
end
end