Class: EmailAddressValidator

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.valid?(email) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/email_valid8.rb', line 31

def self.valid?(email)
  (email =~ URI::MailTo::EMAIL_REGEXP && email !~ /[A-Z]/) || false
end

Instance Method Details

#email_checker(email, field, record) ⇒ Object



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

def email_checker(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



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

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| email_checker(email, field, record) } : email_checker(attr.strip, field, record)
      end
    else
      email_checker(attrs, field, record)
    end
  end
end