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

92 lines
1.7 KiB
Ruby
Raw Normal View History

2018-12-01 21:03:19 -05:00
# frozen_string_literal: true
class Account < ApplicationRecord
2019-03-24 10:08:47 -04:00
################
# Associations #
################
2019-03-23 19:59:38 -04:00
has_one_attached :avatar
2018-12-09 22:36:09 -05:00
belongs_to :person, optional: true
belongs_to :contact_list
2019-06-26 20:21:37 -04:00
has_one :user
2018-12-01 21:50:10 -05:00
2019-09-03 09:43:24 -04:00
has_many :sessions
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
before_validation :turn_blanks_into_nils
2019-03-24 10:42:33 -04:00
before_validation :strip_extra_spaces
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
validates :contact_list, uniqueness: true
2019-06-26 20:21:37 -04:00
2019-09-30 08:30:43 -04:00
validates :nickname, codename: true, uniqueness: { case_sensitive: false }
2019-01-31 19:47:44 -05:00
2019-09-30 08:30:43 -04:00
validates :public_name, allow_nil: true, good_small_text: true
2019-09-30 07:30:19 -04:00
validates :biography, allow_nil: true, good_big_text: true
2019-01-31 23:02:51 -05:00
2019-03-24 23:04:53 -04:00
validates :avatar, allow_nil: true, image: true
2019-09-30 07:30:19 -04:00
validates :timezone, timezone: true
2019-09-03 13:16:21 -04:00
validate :contact_list_corresponds_person
validate :person_corresponds_contact_list
2019-06-26 20:43:27 -04:00
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
2019-09-09 18:11:10 -04:00
def restricted?
!superuser?
end
def can_access_sidekiq_web_interface?
2019-08-11 15:27:06 -04:00
superuser?
end
2019-01-31 19:47:44 -05:00
private
2019-03-24 15:27:06 -04:00
def generate_nickname
2019-07-18 23:32:01 -04:00
self.nickname ||= "noname_#{SecureRandom.hex(8)}"
2019-01-31 19:47:44 -05:00
end
2019-01-31 20:19:31 -05:00
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 contact_list_corresponds_person
2019-06-26 20:43:27 -04:00
return if person.nil?
errors.add :contact_list unless contact_list == person.contact_list
2019-06-26 20:43:27 -04:00
end
def person_corresponds_contact_list
return if contact_list.nil?
errors.add :contact_list unless person == contact_list.person
end
2018-12-01 21:03:19 -05:00
end