The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
validates_with_block

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Francis Hwang

Posts: 130
Nickname: francis
Registered: Jul, 2004

Francis Hwang is the Director of Technology at Rhizome.org.
validates_with_block Posted: Jul 17, 2008 7:28 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Francis Hwang.
Original Post: validates_with_block
Feed Title: Francis Hwang's site: ruby
Feed URL: http://fhwang.net/syndicate/ruby.atom
Feed Description: Author & artist Francis Hwang's personal site.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Francis Hwang
Latest Posts From Francis Hwang's site: ruby

Advertisement
In one of our Rails projects at Diversion Media, our models can get pretty big with validations -- one in particular has almost 20. It ends up being pretty noisy having all those repeated words:
class User < ActiveRecord::Base
  validates_presence_of   :login, :message => 'Please enter a login.'
  validates_uniqueness_of :login, :case_sensitive => false
  validates_format_of     :login, :with => /\A\w*\Z/
  validates_length_of     :login, :within => 4..15
end
So, we wrote validates_with_block, a Rails plugin that allows you to write a more readable set of validations for one model.
class User < ActiveRecord::Base
  validates_login do |login|
    login.present   :message => 'Please enter a login.'
    login.unique    :case_sensitive => false
    login.formatted :with => /\A\w*\Z/
    login.length    :within => 4..15
  end
end
These methods just call the same old validates_* methods; they don't do anything interesting with ActiveRecord beyond that. It's just for keeping things readable, but sometimes readability takes a little extra work.

Read: validates_with_block

Topic: What's New in Edge Rails: Easy Memoization Previous Topic   Next Topic Topic: Ruby Debugging with 'Cylon' - Tutorial Screencast

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use