leah
6/24/2007 11:05:00 PM
Alrighty, I have a solution...
Instead of embedding all my validation code in custom_validations,
I've moved the code into it's own module and I call it from
custom_validations like this
lib/custom_validations.rb
require 'lib/utils/authentication'
module ActiveRecord
module Validations
module ClassMethods
def validates_as_email(*attr_names)
configuration = { :message => "Invalid email address" }
configuration.update(attr_names.pop) if attr_names.last.is_a?
(Hash)
validates_each(attr_names, configuration) do |record,
attr_name, value|
record.errors.add(attr_name, configuration[:message])
unless !value.nil? and Utils::Authentication.valid_email?( value )
end
end
end
end
end
lib/utils/authenticaion.rb
module Utils
module Authentication
def valid_email?( value )
... a whole lot of regexp code ...
end
end
end
Now I can unit test my custom validation code.
Sweet!