aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2011-12-06 14:13:44 +0100
committerJosé Valim <jose.valim@gmail.com>2011-12-06 14:13:44 +0100
commitf0f0e59c104ebd79ce2a48d5678d6a51a5acd42b (patch)
tree34e460add27a2bb132be5724d84d56eb8ac2416a /activemodel/lib
parentd722eb3b049b18da602a36b6dfd1c69945b9fb0d (diff)
downloadrails-f0f0e59c104ebd79ce2a48d5678d6a51a5acd42b.tar.gz
rails-f0f0e59c104ebd79ce2a48d5678d6a51a5acd42b.tar.bz2
rails-f0f0e59c104ebd79ce2a48d5678d6a51a5acd42b.zip
Ensure length validator also works on 1.8.7.
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model/validations/length.rb23
1 files changed, 17 insertions, 6 deletions
diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb
index 95a8ca49e9..a38de27b3c 100644
--- a/activemodel/lib/active_model/validations/length.rb
+++ b/activemodel/lib/active_model/validations/length.rb
@@ -1,3 +1,5 @@
+require "active_support/core_ext/string/encoding"
+
module ActiveModel
# == Active Model Length Validator
@@ -35,14 +37,11 @@ module ActiveModel
end
def validate_each(record, attribute, value)
- value = options[:tokenizer].call(value) if value.kind_of?(String) && options[:tokenizer].present?
+ value = tokenize(value)
+ value_length = value.respond_to?(:length) ? value.length : value.to_s.length
CHECKS.each do |key, validity_check|
next unless check_value = options[key]
-
- value ||= [] if key == :maximum
-
- value_length = value.respond_to?(:length) ? value.length : value.to_s.length
next if value_length.send(validity_check, check_value)
errors_options = options.except(*RESERVED_OPTIONS)
@@ -54,6 +53,18 @@ module ActiveModel
record.errors.add(attribute, MESSAGES[key], errors_options)
end
end
+
+ private
+
+ def tokenize(value)
+ if value.kind_of?(String)
+ if options[:tokenizer]
+ options[:tokenizer].call(value)
+ elsif !value.encoding_aware?
+ value.mb_chars
+ end
+ end || value
+ end
end
module HelperMethods
@@ -95,7 +106,7 @@ module ActiveModel
# * <tt>:tokenizer</tt> - Specifies how to split up the attribute string. (e.g. <tt>:tokenizer => lambda {|str| str.scan(/\w+/)}</tt> to
# count words as in above example.)
# Defaults to <tt>lambda{ |value| value.split(//) }</tt> which counts individual characters.
- # * <tt>:strict</tt> - Specifies whether validation should be strict.
+ # * <tt>:strict</tt> - Specifies whether validation should be strict.
# See <tt>ActiveModel::Validation#validates!</tt> for more information
def validates_length_of(*attr_names)
validates_with LengthValidator, _merge_attributes(attr_names)