Class: Challah::EmailValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/challah/validators/email_validator.rb

Overview

Used to validate reasonably-email-looking strings.

Examples:

Usage

class User < ActiveRecord::Base
  validates :email, :presence => true, :email => true
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.patternObject

A reasonable-email-looking regexp pattern



17
18
19
# File 'lib/challah/validators/email_validator.rb', line 17

def self.pattern
  /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,}\z/
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object

Called automatically by ActiveModel validation..



10
11
12
13
14
# File 'lib/challah/validators/email_validator.rb', line 10

def validate_each(record, attribute, value)
  unless value =~ EmailValidator.pattern
    record.errors.add(attribute, options[:message] || :invalid_email)
  end
end