Merge pull request #98 from 907th/master

Range with excluded end passed to :between option should be split into :on_or_after and :before options
This commit is contained in:
Adam Meehan 2013-04-30 04:45:54 -07:00
commit 3d798697e1
3 changed files with 26 additions and 5 deletions

View File

@ -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_datetime :finish_time, :after => :start_time # Method symbol
validates_date :booked_at, :on => :create, :on_or_after => :today # See Restriction Shorthand. 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', validates_time :breakfast_time, :on_or_after => '6:00am',
:on_or_after_message => 'must be after opening time', :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 :after_message
:on_or_after_message :on_or_after_message
Note: There is no :between_message option. The between error message should be defined using the Note: There is no :between_message option. The between error message should be defined using the :on_or_after and :on_or_before
:on_or_before and :on_or_after messages. (: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. It is highly recommended you use the I18n system for error messages.

View File

@ -33,7 +33,12 @@ module ValidatesTimeliness
if range = options.delete(:between) if range = options.delete(:between)
raise ArgumentError, ":between must be a Range or an Array" unless range.is_a?(Range) || range.is_a?(Array) 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 end
@restrictions_to_check = RESTRICTIONS.keys & options.keys @restrictions_to_check = RESTRICTIONS.keys & options.keys

View File

@ -115,6 +115,19 @@ describe ValidatesTimeliness::Validator do
valid!(:birth_date, on_or_before) valid!(:birth_date, on_or_before)
end end
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 end
describe ":ignore_usec option" do describe ":ignore_usec option" do