Sha256: d3a74e2da3b94839697eb87c9d90ae9553df2c8420657aad01816145a60b135a

Contents?: true

Size: 1019 Bytes

Versions: 1

Compression:

Stored size: 1019 Bytes

Contents

# frozen_string_literals: true

module Jobshop
  class User < ApplicationRecord
    self.primary_keys = %i[ organization_id user_id ]

    after_initialize { self.user_id ||= SecureRandom.uuid if new_record? }

    belongs_to :organization, inverse_of: :users

    has_many :role_assignments, inverse_of: :user,
      foreign_key: %i[ organization_id user_id ]

    has_many :roles, through: :role_assignments

    validates :email,
      presence:   { if: :email_required? },
      format:     { if: :email_required?, with: /\A[^@\s]+@[^@\s]+\z/ },
      uniqueness: { if: :email_changed?, scope: :organization_id, case_sensitive: false }

    validates :encrypted_password, presence: true

    validates :password,
      presence:     { if: :password_required? },
      confirmation: { if: :password_required? }

    devise :database_authenticatable

  private
    def password_required?
      !persisted? || !password.nil? || !password_confirmation.nil?
    end

    def email_required?
      true
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jobshop-0.0.167 app/models/jobshop/user.rb