aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2013-06-25 21:46:50 -0300
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2013-06-25 21:48:41 -0300
commit61346d1b42527d072c0b4bd97f3b5ce3f6be214c (patch)
tree5e3ed3a00752aa4e8a0c5b6cc3d04c078d01ba69 /activemodel/lib
parenta1fedfa839d0cd8b40c61b1a00bd0ac848a838af (diff)
parent0317b93c17a46d7663a8c36edc26ad0ba3d75f85 (diff)
downloadrails-61346d1b42527d072c0b4bd97f3b5ce3f6be214c.tar.gz
rails-61346d1b42527d072c0b4bd97f3b5ce3f6be214c.tar.bz2
rails-61346d1b42527d072c0b4bd97f3b5ce3f6be214c.zip
Merge pull request #10774 from chuckbergeron/validates-inclusion-of-accuracy-for-non-numeric-ranges
Greater accuracy for validates_inclusion_of on non-numeric ranges Closes #10774, fixes #10593
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model/validations/clusivity.rb7
-rw-r--r--activemodel/lib/active_model/validations/inclusion.rb2
2 files changed, 5 insertions, 4 deletions
diff --git a/activemodel/lib/active_model/validations/clusivity.rb b/activemodel/lib/active_model/validations/clusivity.rb
index 49df98d6c1..c5aacb010a 100644
--- a/activemodel/lib/active_model/validations/clusivity.rb
+++ b/activemodel/lib/active_model/validations/clusivity.rb
@@ -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
diff --git a/activemodel/lib/active_model/validations/inclusion.rb b/activemodel/lib/active_model/validations/inclusion.rb
index 1cfd86efee..24337614c5 100644
--- a/activemodel/lib/active_model/validations/inclusion.rb
+++ b/activemodel/lib/active_model/validations/inclusion.rb
@@ -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