summaryrefslogtreecommitdiff
path: root/app/models/user_account.rb
blob: cd994413aa4123a95dce864821b1778f1e69f0fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true

# UserAccount model
# email: string
# password_digest (secure password): string
# first_name: string
# last_name: string
# session_key: string
class UserAccount < ApplicationRecord
  has_secure_password validations: false
  validates :email, presence: true
  validates :email, uniqueness: true
  validates :password, presence: true, if: :no_password?
  validates_format_of :email,
                      with: /\A(|(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})\z/i
  validates :first_name, presence: true
  validates :last_name, presence: true
  validates :role, presence: true, inclusion: { in: %w[master regular] }

  def full_name
    "#{first_name} #{last_name}"
  end

  private

  def no_password?
    password_digest.nil?
  end
end