Class: EmailValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/spree/core/validators/email.rb

Instance Method Summary collapse

Instance Method Details

#valid?(email) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/spree/core/validators/email.rb', line 11

def valid?(email)
  begin
    m = Mail::Address.new(email)
    # We must check that value contains a domain and that value is an email address
    r = m.domain && m.address == email
    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
  r
end

#validate_each(record, attribute, value) ⇒ Object



5
6
7
8
9
# File 'lib/spree/core/validators/email.rb', line 5

def validate_each(record,attribute,value)
  unless valid?(value)
    record.errors.add(attribute, :invalid, {:value => value}.merge!(options))
  end
end