class Ditty::Identity

Attributes

password[RW]
password_confirmation[RW]

Public Class Methods

locate(conditions) click to toggle source
# File lib/ditty/models/identity.rb, line 19
def self.locate(conditions)
  where(conditions).first
end

Public Instance Methods

authenticate(unencrypted) click to toggle source
# File lib/ditty/models/identity.rb, line 23
def authenticate(unencrypted)
  return false if crypted_password.blank?

  self if ::BCrypt::Password.new(crypted_password) == unencrypted
end
before_save() click to toggle source

Callbacks

Calls superclass method
# File lib/ditty/models/identity.rb, line 72
def before_save
  super
  encrypt_password unless password == '' || password.nil?
end
info() click to toggle source

Return whatever we want to pass to the omniauth hash here

# File lib/ditty/models/identity.rb, line 34
def info
  {
    email: username
  }
end
persisted?() click to toggle source
# File lib/ditty/models/identity.rb, line 29
def persisted?
  !new? && @destroyed != true
end
uid() click to toggle source
# File lib/ditty/models/identity.rb, line 40
def uid
  user&.id
end
validate() click to toggle source

Validation

Calls superclass method
# File lib/ditty/models/identity.rb, line 45
def validate
  super
  validates_presence :username
  unless username.blank?
    validates_unique :username
    validates_format(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :username)
  end

  if password_required
    validates_presence :password
    validates_presence :password_confirmation
    validates_format(
      # 1 Uppercase
      # 1 lowercase
      # 1 Special Character
      # 1 Number
      # At least 8 characters
      %r[\A(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#&$*)(}{%^=_+|\\:";'<>,.\-/?\[\]])(?=.*[0-9]).{8,}\Z],
      :password,
      message: 'is not strong enough'
    )
  end

  errors.add(:password_confirmation, 'must match password') if !password.blank? && password != password_confirmation
end

Private Instance Methods

encrypt_password() click to toggle source
# File lib/ditty/models/identity.rb, line 79
def encrypt_password
  self.crypted_password = ::BCrypt::Password.create(password)
end
password_required() click to toggle source
# File lib/ditty/models/identity.rb, line 83
def password_required
  crypted_password.blank? || !password.blank?
end