module HasEmailAuthentication::Module::ClassMethods

Public Instance Methods

has_email_authentication() click to toggle source

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

@example

class User < ActiveRecord::Base
  has_email_authentication
end

User.create(email: "MICHAEL@example.com")
User.find_by_email("michael@EXAMPLE.com)
# 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