Centralizing validation for multiple AR models

I have several ActiveRecord models that contain email addresses and I’d
like to be able to DRY up the validation for them so I only have to put
the appropriate regex in one place. Currently each model has its own
“validates_format_of” with the same regex provided for the appropriate
email field.

Some options:

  1. One option is to use composed_of - would this work for me? I’d need
    to compose each email field as an EmailAddress model object in my
    various models.

  2. Alternatively, there seems to be an opportunity here for declaring
    validations that span models - either by naming them, or pointing to a
    method somewhere, etc.
    Then I could just say “validates_format_of by using this code that I’m
    referencing” in my model objects.

Thoughts?

Wes

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs