Class: ActiveModel::Validations::EmailValidator

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ EmailValidator

Returns a new instance of EmailValidator.



8
9
10
11
# File 'lib/validates_email.rb', line 8

def initialize(options)
  options.reverse_merge!(:message => "is not valid email")
  super(options)
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



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

def validate_each(record, attribute, value)
  return if options[:allow_nil] && value.nil?

  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