-
-
Save millisami/1228793 to your computer and use it in GitHub Desktop.
Rails 3 Email Validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mail' | |
class EmailValidator < ActiveModel::EachValidator | |
attr_reader :record, :attribute, :value, :email, :tree | |
def validate_each(record, attribute, value) | |
@record, @attribute, @value = record, attribute, value | |
@email = Mail::Address.new(value) | |
@tree = email.__send__(:tree) | |
add_error unless valid? | |
rescue Mail::Field::ParseError | |
add_error | |
end | |
private | |
def valid? | |
!!(domain_and_address_present? && domain_has_more_than_one_atom?) | |
end | |
def domain_and_address_present? | |
email.domain && email.address == value | |
end | |
def domain_has_more_than_one_atom? | |
tree.domain.dot_atom_text.elements.length > 1 | |
end | |
def add_error | |
if message = options[:message] | |
record.errors[attribute] << message | |
else | |
record.errors.add(attribute, :invalid) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe Author do | |
def author(attributes = {}) | |
@author ||= Author.make(attributes) # assuming you're using Machinist | |
end | |
describe '#email' do | |
%w[[email protected] [email protected] [email protected] [email protected]].each do |valid_address| | |
context "when #{valid_address.inspect}" do | |
it 'has no errors on email' do | |
author(email: valid_address).valid? | |
author.should have(0).errors_on(:email) | |
end | |
end | |
end | |
%w[haxors @blah can@haz].each do |invalid_address| | |
context "when #{invalid_address.inspect}" do | |
it 'has one error on email' do | |
author(email: invalid_address).valid? | |
author.should have(1).error_on(:email) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment