Sha256: ecf7caf2ff3e43840b2696d6ed2acc9aa6378cc2a237ee172b87b9151e36f350

Contents?: true

Size: 848 Bytes

Versions: 5

Compression:

Stored size: 848 Bytes

Contents

require 'active_model'
require 'mail'

class FakeEmailValidator < ActiveModel::EachValidator

  I18N_SCOPE = 'fake_email_validator.validations.email'

  def initialize(options)
    super(options)

    fake_domains_file = File.expand_path('../../config/fake_domains.list', __FILE__)
    @fake_domains = File.readlines(fake_domains_file).map {|fd| fd.strip.downcase }
  end

  def validate_each(record, attribute, value)
    email = Mail::Address.new(value)

    domain = email.domain.strip.downcase
    is_fake = @fake_domains.include?(domain)

    record.errors.add attribute, I18n.t(:fake, scope: I18N_SCOPE) if is_fake
  rescue Mail::Field::ParseError
    record.errors.add attribute, I18n.t(:invalid, scope: I18N_SCOPE)
  end

end

ActiveSupport.on_load(:i18n) do
  I18n.load_path << File.expand_path('../../config/locales/en.yml', __FILE__)
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fake_email_validator-0.0.5 lib/fake_email_validator.rb
fake_email_validator-0.0.4 lib/fake_email_validator.rb
fake_email_validator-0.0.3 lib/fake_email_validator.rb
fake_email_validator-0.0.2 lib/fake_email_validator.rb
fake_email_validator-0.0.1 lib/fake_email_validator.rb