Skip to content

Commit

Permalink
Add :tokenizer option to validates_length_of. [#507 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
dfl authored and lifo committed Jul 4, 2008
1 parent a78750f commit dd89462
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,10 @@
*2.1.1 (next release)*

* Add :tokenizer option to validates_length_of to specify how to split up the attribute string. #507. [David Lowenfels] Example :

# Ensure essay contains at least 100 words.
validates_length_of :essay, :minimum => 100, :too_short => "Your essay must be at least %d words."), :tokenizer => lambda {|str| str.scan(/\w+/) }

* Always treat integer :limit as byte length. #420 [Tarmo Tänav]

* Partial updates don't update lock_version if nothing changed. #426 [Daniel Morrison]
Expand Down
16 changes: 10 additions & 6 deletions activerecord/lib/active_record/validations.rb
Expand Up @@ -480,8 +480,9 @@ def validates_presence_of(*attr_names)
# validates_length_of :fax, :in => 7..32, :allow_nil => true
# validates_length_of :phone, :in => 7..32, :allow_blank => true
# validates_length_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name"
# validates_length_of :fav_bra_size, :minimum=>1, :too_short=>"please enter at least %d character"
# validates_length_of :smurf_leader, :is=>4, :message=>"papa is spelled with %d characters... don't play me."
# validates_length_of :fav_bra_size, :minimum => 1, :too_short => "please enter at least %d character"
# validates_length_of :smurf_leader, :is => 4, :message => "papa is spelled with %d characters... don't play me."
# validates_length_of :essay, :minimum => 100, :too_short => "Your essay must be at least %d words."), :tokenizer => lambda {|str| str.scan(/\w+/) }
# end
#
# Configuration options:
Expand All @@ -492,7 +493,6 @@ def validates_presence_of(*attr_names)
# * <tt>:in</tt> - A synonym(or alias) for <tt>:within</tt>.
# * <tt>:allow_nil</tt> - Attribute may be +nil+; skip validation.
# * <tt>:allow_blank</tt> - Attribute may be blank; skip validation.
#
# * <tt>:too_long</tt> - The error message if the attribute goes over the maximum (default is: "is too long (maximum is %d characters)").
# * <tt>:too_short</tt> - The error message if the attribute goes under the minimum (default is: "is too short (min is %d characters)").
# * <tt>:wrong_length</tt> - The error message if using the <tt>:is</tt> method and the attribute is the wrong size (default is: "is the wrong length (should be %d characters)").
Expand All @@ -504,12 +504,16 @@ def validates_presence_of(*attr_names)
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false value.
# * <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.
def validates_length_of(*attrs)
# Merge given options with defaults.
options = {
:too_long => ActiveRecord::Errors.default_error_messages[:too_long],
:too_short => ActiveRecord::Errors.default_error_messages[:too_short],
:wrong_length => ActiveRecord::Errors.default_error_messages[:wrong_length]
:wrong_length => ActiveRecord::Errors.default_error_messages[:wrong_length],
:tokenizer => lambda {|value| value.split(//)}
}.merge(DEFAULT_VALIDATION_OPTIONS)
options.update(attrs.extract_options!.symbolize_keys)

Expand All @@ -536,7 +540,7 @@ def validates_length_of(*attrs)
too_long = options[:too_long] % option_value.end

validates_each(attrs, options) do |record, attr, value|
value = value.split(//) if value.kind_of?(String)
value = options[:tokenizer].call(value) if value.kind_of?(String)
if value.nil? or value.size < option_value.begin
record.errors.add(attr, too_short)
elsif value.size > option_value.end
Expand All @@ -553,7 +557,7 @@ def validates_length_of(*attrs)
message = (options[:message] || options[message_options[option]]) % option_value

validates_each(attrs, options) do |record, attr, value|
value = value.split(//) if value.kind_of?(String)
value = options[:tokenizer].call(value) if value.kind_of?(String)
record.errors.add(attr, message) unless !value.nil? and value.size.method(validity_checks[option])[option_value]
end
end
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/validations_test.rb
Expand Up @@ -1059,6 +1059,18 @@ def test_validates_length_of_using_is_utf8
end
end

def test_validates_length_of_with_block
Topic.validates_length_of :content, :minimum => 5, :too_short=>"Your essay must be at least %d words.",
:tokenizer => lambda {|str| str.scan(/\w+/) }
t = Topic.create!(:content => "this content should be long enough")
assert t.valid?

t.content = "not long enough"
assert !t.valid?
assert t.errors.on(:content)
assert_equal "Your essay must be at least 5 words.", t.errors[:content]
end

def test_validates_size_of_association_utf8
with_kcode('UTF8') do
assert_nothing_raised { Topic.validates_size_of :replies, :minimum => 1 }
Expand Down

0 comments on commit dd89462

Please sign in to comment.