Conventional Validations
Listen to this article
As 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 column
named 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.
Comments
Hmm, this is rather clever. I think you might run into some problem relying on just suffix patterns. My personal preference has always been to use any common denominator as a prefix rather than a suffix, such as "phone_office" and "phone_mobile" or "name_first" and "name_last". But either way, this is another step forward in the fight of convention over configuration, and I like it! Thanks.
Posted by: Ted
|
October 11, 2007 10:46 PM
Yup. I agree with the fact that some ppl might like to use prefixes. I prefer suffixes :D I guess it's personal preference more than anything.
Posted by: Simon Harris
|
October 11, 2007 11:49 PM