Files
fizzy/app/models/user.rb
T
2024-12-18 12:38:53 -06:00

60 lines
1.5 KiB
Ruby

class User < ApplicationRecord
include Transferable
belongs_to :account
has_many :sessions, dependent: :destroy
has_secure_password validations: false
has_many :accesses, dependent: :destroy
has_many :buckets, through: :accesses
has_many :accessible_bubbles, through: :buckets, source: :bubbles
has_many :filters, foreign_key: :creator_id, inverse_of: :creator, dependent: :destroy
has_many :pops, dependent: :nullify
has_many :assignments, foreign_key: :assignee_id, dependent: :destroy
has_many :assignings, foreign_key: :assigner_id, class_name: "Assignment"
has_many :assigned_bubbles, through: :assignments, source: :bubble
has_one_attached :avatar
validates_presence_of :email_address
normalizes :email_address, with: ->(value) { value.strip.downcase }
after_create_commit :grant_access_to_buckets
scope :active, -> { where(active: true) }
scope :alphabetically, -> { order("LOWER(name)") }
def initials
name.to_s.scan(/\b\p{L}/).join.upcase
end
def deactivate
transaction do
sessions.destroy_all
accesses.destroy_all
update! active: false, email_address: deactived_email_address
end
end
def can_remove?(other)
other != self
end
def to_combobox_display
name
end
private
def deactived_email_address
email_address.sub(/@/, "-deactivated-#{SecureRandom.uuid}@")
end
def grant_access_to_buckets
Access.insert_all account.buckets.all_access.pluck(:id).collect { |bucket_id| { bucket_id: bucket_id, user_id: id } }
end
end