From dc0fdc03407f6349552df97415854c9991438a9f Mon Sep 17 00:00:00 2001 From: Alexey Chernenkov Date: Tue, 23 Apr 2013 15:36:47 +0600 Subject: [PATCH] Range with excluded end passed to :between option should be split into :on_or_after and :before options --- README.rdoc | 9 ++++++--- lib/validates_timeliness/validator.rb | 7 ++++++- spec/validates_timeliness/validator_spec.rb | 15 ++++++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.rdoc b/README.rdoc index 15393ede..82e1932d 100644 --- a/README.rdoc +++ b/README.rdoc @@ -55,7 +55,10 @@ NOTE: You may wish to enable the plugin parser and the extensions to start. Plea validates_datetime :finish_time, :after => :start_time # Method symbol validates_date :booked_at, :on => :create, :on_or_after => :today # See Restriction Shorthand. - validates_time :booked_at, :between => ['9.00am', '5:00pm'] + + validates_time :booked_at, :between => ['9:00am', '5:00pm'] # On or after 9:00AM and on or before 5:00PM + validates_time :booked_at, :between => '9:00am'..'5:00pm' # The same as previous example + validates_time :booked_at, :between => '9:00am'...'5:00pm' # On or after 9:00AM and strictly before 5:00PM validates_time :breakfast_time, :on_or_after => '6:00am', :on_or_after_message => 'must be after opening time', @@ -173,8 +176,8 @@ You can also use validation options for custom error messages. The following opt :after_message :on_or_after_message -Note: There is no :between_message option. The between error message should be defined using the -:on_or_before and :on_or_after messages. +Note: There is no :between_message option. The between error message should be defined using the :on_or_after and :on_or_before +(:before in case when :between argument is a Range with excluded high value, see Examples) messages. It is highly recommended you use the I18n system for error messages. diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index b52e9797..484125e1 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -32,7 +32,12 @@ def initialize(options) 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 + options[:on_or_after] = range.first + if range.is_a?(Range) && range.exclude_end? + options[:before] = range.last + else + options[:on_or_before] = range.last + end end @restrictions_to_check = RESTRICTIONS.keys & options.keys diff --git a/spec/validates_timeliness/validator_spec.rb b/spec/validates_timeliness/validator_spec.rb index 94d90bc3..6d03ba1b 100644 --- a/spec/validates_timeliness/validator_spec.rb +++ b/spec/validates_timeliness/validator_spec.rb @@ -115,6 +115,19 @@ valid!(:birth_date, on_or_before) end end + + describe "range with excluded end value" do + it 'should be split option into :on_or_after and :before values' do + on_or_after, before = Date.new(2010,1,1), Date.new(2010,1,3) + Person.validates_date :birth_date, :between => on_or_after...before + Person.validators.first.options[:on_or_after].should == on_or_after + Person.validators.first.options[:before].should == before + invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01") + invalid!(:birth_date, before, "must be before 2010-01-03") + valid!(:birth_date, on_or_after) + valid!(:birth_date, before - 1) + end + end end describe ":ignore_usec option" do @@ -224,7 +237,7 @@ class PersonWithFormatOption Person.validates_date :birth_date, :invalid_date_message => 'custom invalid message' invalid!(:birth_date, 'asdf', 'custom invalid message') end - + it 'should be used for invalid restriction' do Person.validates_date :birth_date, :before => Time.now, :before_message => 'custom before message' invalid!(:birth_date, Time.now, 'custom before message')