Thursday, September 19, 2024 11:58:56 PM
> settings

Customize


Authenticate

> user.rb
# frozen_string_literal: true

class User < ApplicationRecord
  include PublicIdConcern

  devise :database_authenticatable, :registerable,
    :rememberable, :validatable, :timeoutable, :trackable,
    authentication_keys: [:username]

  ###########################################
  # Attributes
  attribute :public_id, :public_id
  attribute :role_id, :integer
  attribute :username, :string
  attribute :encrypted_password, :string
  attribute :sign_in_count, :integer
  attribute :current_sign_in_ip, :string
  attribute :last_sign_in_ip, :string
  attribute :current_sign_in_at, :datetime
  attribute :last_sign_in_at, :datetime
  attribute :remember_created_at, :datetime
  attribute :created_at, :datetime
  attribute :updated_at, :datetime

  alias_attribute :name, :username

  ###########################################
  # Associations
  belongs_to :role, class_name: "Role", inverse_of: :users

  ###########################################
  # Validations
  validates :username, uniqueness: true

  ###########################################
  # Scopes
  default_scope { joins(:role) }

  ###########################################

  delegate :permission?, to: :role

  def self.guest_user
    find_by(username: "guest")
  end

  #
  # Shits and giggles. It means nothing
  #
  def fake_session_token
    Rails.cache.fetch("user-#{id}-session-token") do
      public_id.chars.shuffle.sample(7).join
    end
  end

  def clear_fake_session_token!
    Rails.cache.delete("user-#{id}-session-token")
  end

  protected

  # Devise shit. It's because I'm using `username`
  def email_changed?
    false
  end

  def email_required?
    false
  end

  def will_save_change_to_email?
    false
  end
end
All opinions represented herein are my own
- © 2024 itsthedevman
- build 3c15a1b