I’ve never taken the time to package/release/announce this plugin of
mine,
have I? Forgive me. The time has come.
I introduce to you Semantic Attributes!
From the README:
The method-chained validation routine built into ActiveRecord must die!
It’s
time for an object-oriented approach to attribute validations. The
Semantic
Attributes plugin provides this approach by letting you attach
predicates to
your attributes with a tasty DSL. These predicates package up some
really
sweet behavior, where validations are really only the beginning. I’ve
also
discovered that it can be really useful to use these predicates to
convert
between human and machine formats: for example, with the phone number
predicate you can let your users enter phone numbers with whatever
formatting they want, always save the values to the database as numeric
strings, and then present the values back to the user with standard
formatting.
I’ve also found other nifty uses for object-oriented predicates that
package
up validation. For example, it becomes easy to run a quick validation
check
on a field with a sample value and report true/false. This is exactly
what
the expected_error_for(:field, value)> method does, and it lets you
build a
validation routine that listens to form data as it’s being typed and
report
problems without duplicating your validation code client-side. In a
similar
vein, the _valid? attribute suffix lets you do single-attribute
validation
on a record anytime you want.
For more, see:
*
http://codelevy.com/articles/2008/05/16/semantic-attributes-a-rails-validation-and-attribute-formatting-plugin
and of course:
-Lance
p.s. this is completely compatible with ActiveRecord validations … you
can
use 'em both if you want
–
rails blog: http://codelevy.com
co-founder: http://uservoice.com