Class: EmailAddressValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/email_valid8.rb

Instance Method Summary collapse

Instance Method Details

#check_email(email, field, record) ⇒ Object



24
25
26
27
28
# File 'lib/email_valid8.rb', line 24

def check_email(email, field, record)
  return if email =~ URI::MailTo::EMAIL_REGEXP && email !~ /[A-Z]/

  record.errors.add(field, 'is not written in a valid format')
end

#validate(record) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/email_valid8.rb', line 4

def validate(record)
  return if record.blank?

  fields = options[:fields]
  multiples = options[:multiples]

  fields.each do |field|
    next if record.send(field).blank?

    attrs = record.send(field)
    if multiples
      attrs.split(',').each do |attr|
        attr.class == Array ? attr.each { |email| check_email(email, field, record) } : check_email(attr.strip, field, record)
      end
    else
      check_email(attrs, field, record)
    end
  end
end