mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Use Range#cover? for Numeric ranges (tests via endpoints) and use Range#include? for non-numeric ranges
added changelog message
This commit is contained in:
parent
5a687e930f
commit
0317b93c17
4 changed files with 10 additions and 4 deletions
|
@ -1,3 +1,7 @@
|
|||
* `validates_inclusion_of` for ranges uses the speedy Range#cover for numerical ranges, and the accurate Range#include? for non-numerical ranges.
|
||||
|
||||
*Charles Bergeron*
|
||||
|
||||
* Deprecate `Validator#setup`. This should be done manually now in the validator's constructor.
|
||||
|
||||
*Nick Sutterer*
|
||||
|
|
|
@ -31,10 +31,11 @@ module ActiveModel
|
|||
end
|
||||
|
||||
# In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the
|
||||
# range for equality, so it may be slow for large ranges. The new <tt>Range#cover?</tt>
|
||||
# uses the previous logic of comparing a value with the range endpoints.
|
||||
# range for equality, which is slower but more accurate. <tt>Range#cover?</tt> uses
|
||||
# the previous logic of comparing a value with the range endpoints, which is fast
|
||||
# but is only accurate on numeric ranges.
|
||||
def inclusion_method(enumerable)
|
||||
enumerable.is_a?(Range) ? :cover? : :include?
|
||||
(enumerable.is_a?(Range) && enumerable.first.is_a?(Numeric)) ? :cover? : :include?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -28,7 +28,7 @@ module ActiveModel
|
|||
# Configuration options:
|
||||
# * <tt>:in</tt> - An enumerable object of available items. This can be
|
||||
# supplied as a proc, lambda or symbol which returns an enumerable. If the
|
||||
# enumerable is a range the test is performed with <tt>Range#cover?</tt>,
|
||||
# enumerable is a numerical range the test is performed with <tt>Range#cover?</tt>,
|
||||
# otherwise with <tt>include?</tt>.
|
||||
# * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
|
||||
# * <tt>:message</tt> - Specifies a custom error message (default is: "is
|
||||
|
|
|
@ -14,6 +14,7 @@ class InclusionValidationTest < ActiveModel::TestCase
|
|||
Topic.validates_inclusion_of(:title, in: 'aaa'..'bbb')
|
||||
assert Topic.new("title" => "bbc", "content" => "abc").invalid?
|
||||
assert Topic.new("title" => "aa", "content" => "abc").invalid?
|
||||
assert Topic.new("title" => "aaab", "content" => "abc").invalid?
|
||||
assert Topic.new("title" => "aaa", "content" => "abc").valid?
|
||||
assert Topic.new("title" => "abc", "content" => "abc").valid?
|
||||
assert Topic.new("title" => "bbb", "content" => "abc").valid?
|
||||
|
|
Loading…
Reference in a new issue