Module: HasEmailAuthentication::Module::ClassMethods

Defined in:
lib/has_email_authentication/module.rb

Instance Method Summary collapse

Instance Method Details

#has_email_authenticationObject

Generates the find_by_email class method. Downcases the :email attribute before saving. Validates the presence, format and uniqueness of the :email attribute.

Examples:


class User < ActiveRecord::Base
  has_email_authentication
end

User.create(email: "[email protected]")
User.find_by_email("[email protected])


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/has_email_authentication/module.rb', line 22

def has_email_authentication
  validates :email,
    presence: true,
    format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i },
    uniqueness: { case_sensitive: false }

  before_save do
    self.email = email.try(:downcase)
  end

  extend(FindByEmail)
end