Skip to content

Commit

Permalink
Range with excluded end passed to :between option should be split int…
Browse files Browse the repository at this point in the history
…o :on_or_after and :before options
  • Loading branch information
Alexey Chernenkov committed Apr 30, 2013
1 parent df9677f commit dc0fdc0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
9 changes: 6 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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.

Expand Down
7 changes: 6 additions & 1 deletion lib/validates_timeliness/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion spec/validates_timeliness/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit dc0fdc0

Please sign in to comment.