Class: EmailValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/email_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/validators/email_validator.rb', line 3

def validate_each(record,attribute,value)
  begin
    m = Mail::Address.new(value)
    # We must check that value contains a domain and that value is an email address
    r = m.domain && m.address == value
    t = m.__send__(:tree)
    # We need to dig into treetop
    # A valid domain must have dot_atom_text elements size > 1
    # user@localhost is excluded
    # treetop must respond to domain
    # We exclude valid email values like <[email protected]>
    # Hence we use m.__send__(tree).domain
    r &&= (t.domain.dot_atom_text.elements.size > 1)
  rescue Exception => e   
    r = false
  end
  record.errors[attribute] << (options[:message] || "is invalid") unless r
end