Class: ActiveModel::Validations::EmailValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/validate_email.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ EmailValidator

Returns a new instance of EmailValidator.



10
11
12
13
# File 'lib/validate_email.rb', line 10

def initialize(options)
  options.reverse_merge!(:message => :email)
  super(options)
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/validate_email.rb', line 15

def validate_each(record, attribute, value)

  begin
    mail = Mail::Address.new(value)

    unless mail.address == value && mail.domain.split(".").length > 1
      record.errors.add(attribute, options[:message])
    end
  rescue
    record.errors.add(attribute, options[:message])
  end
end