Conventional Validations
Posted Friday, October 12, 2007As part of some other work I’m currently undertaking, I’ve just extracted a VERY new Rails plugin called, you guessed it, Conventional Validations.
As the README says, Conventional Validations is a plugin that attempts to apply validation based on the naming of column. Specifically, the plugin searches for validation methods such that a columnnamed foo or ending in _foo would be validated using a method named validates_foo.
For example, by defining a class method as:
def self.validates_phone(*attr_names)
validates_format_of attr_names, :with => /[0-9]+(\.[0-9]+)*/, :allow_nil => true
end
Any columns named phone or ending in _phone would be automatically validated.
I’ve only just added it (it’s only available in trunk) and the matching rules are VERY simplistic but if you have company-wide or even project-wide validation and you’re strict about your naming conventions (as I am), consider extracting them into a module and mixing them into ActiveRecord::Base and letting the plugin apply them for you.
There are already some existing plugins such as (off the top of my head) “validates_email” that would most likely just work out of the box.
Enjoy.
- Later: A Diamond in the Rough
- Earlier: FarceBook
About Simon
Husband, Father, One-time Entrepreneur.
Aka Haruki Zaemon. Aka Sampy.
In my younger years I wanted to save the world; now I'm happy solving bigger problems than I create.
If I didn't need to work I'd be teaching Aikido and spending all my free time with my amazing wife and two children in Woodend, Victoria, Australia.
Books
Beginning Algorithms
with James Ross

COMMENTS
blog comments powered by Disqus