Fixes max value detection in inclusion and exclusion validators

This commit is contained in:
Alexey Plutalov 2013-03-30 22:03:38 +04:00 committed by Melissa Xie
parent 8aa860dc2d
commit d06ec02d8d
5 changed files with 48 additions and 2 deletions

View File

@ -1,5 +1,7 @@
# HEAD
* Fixes maximum value detection for the `ensure_inclusion` and `ensure_exclusion`
matchers.
* Add `:odd` and `:even` options to the `validate_numericality_of` matcher.
* Add `:touch` option to the association matcher.
* Ruby 2.0.0 is now officially supported.

View File

@ -27,7 +27,7 @@ module Shoulda # :nodoc:
def in_range(range)
@range = range
@minimum = range.first
@maximum = range.last
@maximum = range.max
self
end

View File

@ -37,7 +37,7 @@ module Shoulda # :nodoc:
def in_range(range)
@range = range
@minimum = range.first
@maximum = range.last
@maximum = range.max
self
end

View File

@ -18,6 +18,18 @@ describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
end
end
context 'an attribute which must be excluded from a range with excluded end' do
it 'accepts ensuring the correct range' do
validating_exclusion(:in => 2...5).
should ensure_exclusion_of(:attr).in_range(2...5)
end
it 'rejects ensuring excluded value' do
validating_exclusion(:in => 2...5).
should_not ensure_exclusion_of(:attr).in_range(2...4)
end
end
context 'an attribute with a custom validation message' do
it 'accepts ensuring the correct range' do
validating_exclusion(:in => 2..4, :message => 'not good').
@ -35,6 +47,15 @@ describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
model.should ensure_exclusion_of(:attr).in_range(2..5).
with_message(/should be out of this range/)
model = custom_validation do
if attr >= 2 && attr <= 4
errors.add(:attr, 'should be out of this range')
end
end
model.should ensure_exclusion_of(:attr).in_range(2...5).
with_message(/should be out of this range/)
end
end

View File

@ -56,6 +56,18 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
end
end
context 'an attribute which must be included in a range with excluded end' do
it 'accepts ensuring the correct range' do
validating_inclusion(:in => 2...5).
should ensure_inclusion_of(:attr).in_range(2...5)
end
it 'rejects ensuring a lower maximum value' do
validating_inclusion(:in => 2...5).
should_not ensure_inclusion_of(:attr).in_range(2...4)
end
end
context 'an attribute with a custom ranged value validation' do
it 'accepts ensuring the correct range' do
validating_inclusion(:in => 2..4, :message => 'not good').
@ -75,6 +87,17 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
model.should ensure_inclusion_of(:attr).in_range(2..5).
with_low_message(/low/).with_high_message(/high/)
model = custom_validation do
if attr < 2
errors.add(:attr, 'too low')
elsif attr > 4
errors.add(:attr, 'too high')
end
end
model.should ensure_inclusion_of(:attr).in_range(2...5).
with_low_message(/low/).with_high_message(/high/)
end
end