module Elabs class User < ApplicationRecord self.table_name = 'users' validates :username, presence: true, uniqueness: true validates :role, presence: false # Username should: # - not start with '-', '_' or '.' # - contain only a to z characters (upper/lower cased), numbers, '.', '-' or '_' # - not contain successive '.', '-' or '_' validates_format_of :username, with: /\A(?![_.-])(?!.*[_.]{2})[a-zA-Z0-9._-]+[^_\-.]\z/ has_many :albums, dependent: :destroy has_many :articles, dependent: :destroy has_many :notes, dependent: :destroy has_many :projects, dependent: :destroy has_many :uploads, dependent: :destroy has_many :reports, dependent: :destroy has_one :preference, dependent: :delete def admin? role == 'admin' end after_create do Preference.create! user: self, show_nsfw: false end end end