Despite close reading of Matz's Ruby book and Agile Web Dev. w. Rails,
I can't figure out how to tackle the challenge of sharing validation
code across models with attributes in common.
In each of my models I'd like to validate the shared attributes with a
custom validator that includes Rails ActiveRecord::Validations class
methods and some of my own validations. My problem is 1) I don't know
how to pass attribute references to my special method and 2) (mundane)
I can't successfully call any ActiveRecord::Validations class methods
from within my custom method.
A bit of garbage here, I know, but any thoughts? Is this kind of
problem handled by use of certain gems?
# example of a custom validator
def email_must_be_from_valid_provider
errors.add :email, "must be from a valid provider" if
email !~ /@[gmail.com](http://gmail.com)$/
end
end
and in your User model, include them like this:
class User < ActiveRecord::Base
include MyEmailValidations
end
I see I need to explore the specifics of mixing in (which maybe Matz's
book underserves) -- in this case it was the self.include(base)
expression that was key.
Finally, where attributes refer logically to the same kind of data,
but have different names, I have aliased them to allow the broadest
use of my version of your validation module.