user.rb 934 Bytes
Newer Older
1 2
# frozen_string_literal: true

Eugen Rochko's avatar
Eugen Rochko committed
3
class User < ApplicationRecord
4
  include Settings::Extend
5

6 7 8
  devise :registerable, :recoverable,
         :rememberable, :trackable, :validatable, :confirmable,
         :two_factor_authenticatable, otp_secret_encryption_key: ENV['OTP_SECRET']
9

Eugen Rochko's avatar
Eugen Rochko committed
10
  belongs_to :account, inverse_of: :user
11
  accepts_nested_attributes_for :account
12

Eugen Rochko's avatar
Eugen Rochko committed
13
  validates :account, presence: true
Eugen Rochko's avatar
Eugen Rochko committed
14
  validates :locale, inclusion: I18n.available_locales.map(&:to_s), unless: 'locale.nil?'
15
  validates :email, email: true
16

17
  scope :prolific, -> { joins('inner join statuses on statuses.account_id = users.account_id').select('users.*, count(statuses.id) as statuses_count').group('users.id').order('statuses_count desc') }
Eugen Rochko's avatar
Eugen Rochko committed
18
  scope :recent,   -> { order('id desc') }
19
  scope :admins,   -> { where(admin: true) }
20

21 22 23
  def send_devise_notification(notification, *args)
    devise_mailer.send(notification, self, *args).deliver_later
  end
Eugen Rochko's avatar
Eugen Rochko committed
24
end