Files
astuto/app/models/user.rb

90 lines
2.0 KiB
Ruby
Raw Normal View History

2019-08-18 18:51:25 +02:00
class User < ApplicationRecord
2022-07-18 10:47:54 +02:00
include TenantOwnable
devise :database_authenticatable, :registerable,
2022-07-18 10:47:54 +02:00
:recoverable, :rememberable, :confirmable
2019-09-24 21:16:51 +02:00
has_many :posts, dependent: :destroy
2019-09-27 12:32:30 +02:00
has_many :likes, dependent: :destroy
2019-09-24 21:16:51 +02:00
has_many :comments, dependent: :destroy
2019-09-16 18:02:52 +02:00
2019-08-19 10:37:45 +02:00
enum role: [:user, :moderator, :admin]
enum status: [:active, :blocked, :deleted]
2019-08-19 10:37:45 +02:00
after_initialize :set_default_role, if: :new_record?
after_initialize :set_default_status, if: :new_record?
before_save :skip_confirmation
2019-08-19 10:37:45 +02:00
validates :full_name, presence: true, length: { in: 2..32 }
2022-07-18 10:47:54 +02:00
validates :email,
presence: true,
uniqueness: { scope: :tenant_id, case_sensitive: false },
format: { with: URI::MailTo::EMAIL_REGEXP }
validates :password, allow_blank: true, length: { in: 6..128 }
validates :password, presence: true, on: :create
2019-08-19 15:45:44 +02:00
2019-08-19 10:37:45 +02:00
def set_default_role
self.role ||= :user
end
def set_default_status
self.status ||= :active
end
def active_for_authentication?
super && active?
end
def inactive_message
2022-07-23 13:32:40 +02:00
active? ? super : I18n.t('errors.user_blocked_or_deleted')
end
2022-07-18 10:47:54 +02:00
# Override Devise::Confirmable#after_confirmation
# Used to change tenant status from pending to active on owner email confirmation
def after_confirmation
tenant = self.tenant
if tenant.status == "pending" and tenant.users.count == 1
tenant.status = "active"
tenant.save
end
end
def skip_confirmation
return if Rails.application.email_confirmation?
skip_confirmation!
skip_confirmation_notification!
skip_reconfirmation!
end
2019-08-19 17:42:13 +02:00
def gravatar_url
gravatar_id = Digest::MD5::hexdigest(email.downcase)
"https://secure.gravatar.com/avatar/#{gravatar_id}"
end
2019-09-16 19:38:56 +02:00
def power_user?
role == 'admin' || role == 'moderator'
end
2022-05-01 18:00:38 +02:00
def admin?
role == 'admin'
end
def moderator?
role == 'moderator'
end
def user?
role == 'user'
end
def active?
status == 'active'
end
def blocked?
status == 'blocked'
end
2019-08-18 18:51:25 +02:00
end