1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/app/models/account.rb

144 lines
3.1 KiB
Ruby
Raw Normal View History

2018-12-01 21:03:19 -05:00
# frozen_string_literal: true
class Account < ApplicationRecord
2019-02-01 22:48:23 -05:00
include Rolify::Role
extend Rolify::Dynamic if Rolify.dynamic_shortcuts
2019-03-24 15:27:06 -04:00
NICKNAME_RE = /\A[a-z][_a-z0-9]*[a-z0-9]\z/.freeze
2019-01-31 19:47:44 -05:00
2019-02-01 22:48:23 -05:00
self.role_cname = 'Role'
self.role_table_name = 'roles'
self.strict_rolify = false
self.adapter = Rolify::Adapter::Base.create 'role_adapter', role_cname, name
2019-03-24 10:09:16 -04:00
##########
# Scopes #
##########
scope :guests, -> { includes(:user).where(users: { id: nil }) }
2019-03-24 10:08:47 -04:00
################
# Associations #
################
2019-03-23 19:59:38 -04:00
has_one_attached :avatar
2019-02-01 23:18:22 -05:00
has_many :account_roles,
2019-02-01 23:35:08 -05:00
-> { active },
2019-02-01 23:27:42 -05:00
inverse_of: :account,
2019-04-28 09:34:46 -04:00
dependent: :restrict_with_exception
2019-02-01 22:48:23 -05:00
2019-07-08 09:13:54 -04:00
has_many :roles,
-> { distinct },
through: :account_roles
2018-12-01 21:28:34 -05:00
2018-12-09 22:36:09 -05:00
belongs_to :person, optional: true
2019-06-26 20:21:37 -04:00
belongs_to :contacts_list
2018-12-01 21:50:10 -05:00
has_one :user, dependent: :restrict_with_exception
2019-03-24 10:08:47 -04:00
#############
# Callbacks #
#############
2019-03-24 15:27:06 -04:00
after_initialize :generate_nickname
2019-01-31 19:47:44 -05:00
2019-06-26 20:21:37 -04:00
before_validation do
2019-06-26 20:43:27 -04:00
self.contacts_list ||= person ? person.contacts_list : ContactsList.new
2019-06-26 20:21:37 -04:00
end
before_validation :turn_blanks_into_nils
2019-03-24 10:42:33 -04:00
before_validation :strip_extra_spaces
2019-01-31 20:19:31 -05:00
before_create :generate_guest_token
2018-12-09 21:52:34 -05:00
2019-03-24 10:08:47 -04:00
###############
# Validations #
###############
2019-03-23 21:42:27 -04:00
validates :person, allow_nil: true, uniqueness: true
2019-01-31 23:03:03 -05:00
2019-06-26 20:21:37 -04:00
validates :contacts_list, uniqueness: true
2019-03-24 15:27:06 -04:00
validates :nickname,
2019-04-28 09:34:46 -04:00
presence: true,
length: { in: 3..36 },
format: NICKNAME_RE,
2019-01-31 19:47:44 -05:00
uniqueness: { case_sensitive: false }
validates :public_name, allow_nil: true, length: { in: 3..255 }
validates :biography, allow_nil: true, length: { in: 3..10_000 }
2019-01-31 23:02:51 -05:00
2019-03-24 23:04:53 -04:00
validates :avatar, allow_nil: true, image: true
2019-06-26 20:43:27 -04:00
validate :contacts_list_corresponds_person
2019-03-24 10:08:47 -04:00
###########
# Methods #
###########
2019-02-01 16:36:10 -05:00
def to_param
2019-03-24 15:27:06 -04:00
nickname
2019-02-01 16:36:10 -05:00
end
2018-12-04 20:49:26 -05:00
def guest?
user.nil?
end
2019-02-08 02:26:08 -05:00
def add_role(role_name, resource = nil)
raise 'can not add role to guest account' if guest?
2019-02-08 02:35:35 -05:00
role = self.class.role_class.make! role_name, resource
return role if roles.include? role
if Rolify.dynamic_shortcuts
self.class.define_dynamic_method role.name, resource
end
account_roles.where(role: role).first_or_create!
2019-02-08 02:35:35 -05:00
role
end
2019-02-01 23:18:22 -05:00
def remove_role(role_name, resource = nil)
role = self.class.role_class.find_by name: role_name, resource: resource
return if role.nil?
2019-02-01 23:27:42 -05:00
2019-02-01 23:18:22 -05:00
account_roles.where(role: role).update_all(deleted_at: Time.zone.now)
end
def can_access_sidekiq_web_interface?
is_superuser?
end
2019-01-31 19:47:44 -05:00
private
2019-03-24 15:27:06 -04:00
def generate_nickname
self.nickname = "noname_#{SecureRandom.hex(8)}" if nickname.nil?
2019-01-31 19:47:44 -05:00
end
2019-01-31 20:19:31 -05:00
def generate_guest_token
self.guest_token = SecureRandom.hex
end
def turn_blanks_into_nils
self.public_name = nil if public_name.blank?
self.biography = nil if biography.blank?
end
2019-03-24 10:42:33 -04:00
def strip_extra_spaces
self.public_name = public_name&.strip
self.biography = biography&.strip
end
2019-06-26 20:43:27 -04:00
def contacts_list_corresponds_person
return if person.nil?
errors.add :contacts_list unless contacts_list == person.contacts_list
end
2018-12-01 21:03:19 -05:00
end