From d14ae09820ef7d8c8880ccbb9f754049a5f8d7b3 Mon Sep 17 00:00:00 2001 From: Edgars Beigarts Date: Fri, 21 Jan 2011 11:02:59 +0200 Subject: [PATCH] Fixed :between option. --- lib/validates_timeliness/validator.rb | 3 ++- spec/validates_timeliness/validator_spec.rb | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index 3c0019d..44f5404 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -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 diff --git a/spec/validates_timeliness/validator_spec.rb b/spec/validates_timeliness/validator_spec.rb index 9a2f117..f03e203 100644 --- a/spec/validates_timeliness/validator_spec.rb +++ b/spec/validates_timeliness/validator_spec.rb @@ -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