Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

patch to add :count=>:words to validates_length_of

#422

This is a patch to add a feature to count words instead of characters, in the ActiveRecord validates_length_of method.

for instance:

validates_length_of :essay, :minimum=200, :count=>:words, :too_short=>"Your essay must be at least %d words."

The patch includes the following tests.

def test_validates_length_of_with_word_count_is
  Topic.validates_length_of( :title, :is => 5, :count => :words )
  
  assert !Topic.create("title" => "this title has too many words").valid?
  assert !Topic.create("title" => "too little").valid?
  assert Topic.create("title" => "this title has just enough").valid?
end

def test_validates_length_of_with_word_count_within
  Topic.validates_length_of( :title, :within => 3..5, :count => :words )

  t = Topic.new( :title => "not enough")
  assert !t.valid?
  assert_equal "is too short (minimum is 3 words)", t.errors.on(:title)

  t = Topic.new( :title => "this title has too many words")
  assert !t.valid?
  assert_equal "is too long (maximum is 5 words)", t.errors.on(:title)
end

Reported by David Lowenfels · June 15th, 2008 @ 06:22 AM

State: wontfix
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Pratik
    Pratik
    • State changed from new to invalid
    • Tag set to activerecord, patch

    I don't think this is a very common requirement. I'd suggest releasing a plugin. Although, you can try emailing railscore mailing list to see if there is enough interest in the patch, in which case we can roll it in.

    Thanks!

    June 27th, 2008 @ 04:33 PM

  2. Jeremy Kemper
    Jeremy Kemper

    Seconded. I've never needed to count word length. If I did, I think I'd prefer adding a new kind of validation instead of another layer on validates_length_of.

    June 28th, 2008 @ 03:21 AM

  3. David Lowenfels
    David Lowenfels

    It seemed like a natural fit to me because the existing rails code already is splitting on '', so I just modified it to optionally split on '\W'. It's a low hanging fruit. I thought a plugin would be overkill, as the core implementation happens on one line (which occurs twice in the code):

    • value = value.split(//) if value.kind_of?(String)
    • value = value.split(/#{split_token}/) if value.kind_of?(String)

    The rest is just icing on the cake to extend the API, modify the validation error messages, and add tests.

    June 28th, 2008 @ 03:53 AM

  4. Jeremy Kemper
    Jeremy Kemper
    • State changed from invalid to wontfix

    Good point, David! We split with // so we get Unicode-aware character counts instead of byte length. This is an implementation detail that goes away in Ruby 1.9.

    Perhaps we should accept an optional block to take an object and return a count.

    validates_length_of(:essay, :minimum => 200, :too_short=>"Your essay must be at least %d words.") { |str| str.scan(/\w+/).size }
    

    June 28th, 2008 @ 05:28 AM

  5. David Lowenfels
    David Lowenfels

    @jeremy: that sounds like a good solution. Should I cook up a patch to accept a block?

    June 28th, 2008 @ 11:26 PM

  6. Jeremy Kemper
  7. David Lowenfels