Class: ActiveModel::Validations::EmailValidator

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

Instance Method Summary collapse

Instance Method Details

#validate_disposable_email(record, attribute, value, options) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/validators/validates_email_format_of.rb', line 23

def validate_disposable_email(record, attribute, value, options)
  hostname = value.to_s.split("@").last.to_s.downcase

  record.errors.add(
    attribute, :disposable_email,
    :value => value
  ) if Validators::DisposableHostnames.all.include?(hostname)
end

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/validators/validates_email_format_of.rb', line 4

def validate_each(record, attribute, value)
  allow_disposable = options.fetch(:disposable, false)

  return if value.blank? && options[:allow_blank]
  return if value.nil? && options[:allow_nil]

  validate_email_format(record, attribute, value, options)
  validate_disposable_email(record, attribute, value, options) unless allow_disposable
end

#validate_email_format(record, attribute, value, options) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/validators/validates_email_format_of.rb', line 14

def validate_email_format(record, attribute, value, options)
  if value.to_s !~ Validators::EMAIL_FORMAT
    record.errors.add(
      attribute, :invalid_email,
      :message => options[:message], :value => value
    )
  end
end