2018-12-09 22:32:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Person < ApplicationRecord
|
2019-03-26 16:40:53 -04:00
|
|
|
include Nameable
|
2019-03-26 16:09:10 -04:00
|
|
|
|
2019-09-01 10:42:16 -04:00
|
|
|
ACCOUNT_CONNECTION_TOKEN_RE = /\A\w+\z/.freeze
|
|
|
|
|
2019-03-25 20:56:31 -04:00
|
|
|
################
|
|
|
|
# Associations #
|
|
|
|
################
|
|
|
|
|
2019-08-28 12:59:39 -04:00
|
|
|
has_one_attached :photo
|
|
|
|
|
2019-07-29 01:36:39 -04:00
|
|
|
belongs_to :contact_list
|
2019-06-26 20:21:37 -04:00
|
|
|
|
2019-07-20 06:25:23 -04:00
|
|
|
has_one :account
|
2018-12-14 23:20:13 -05:00
|
|
|
|
2019-07-20 08:16:48 -04:00
|
|
|
has_many :all_relationships,
|
|
|
|
class_name: 'Relationship',
|
2019-07-20 06:25:23 -04:00
|
|
|
inverse_of: :person
|
2019-04-28 09:08:02 -04:00
|
|
|
|
2019-07-20 06:25:23 -04:00
|
|
|
has_many :person_comments
|
2019-07-15 17:19:11 -04:00
|
|
|
|
2019-07-20 06:25:23 -04:00
|
|
|
has_many :passports
|
2019-01-30 22:04:42 -05:00
|
|
|
|
2019-06-26 20:21:37 -04:00
|
|
|
###############
|
|
|
|
# Validations #
|
|
|
|
###############
|
|
|
|
|
2019-07-29 01:36:39 -04:00
|
|
|
validates :contact_list, uniqueness: true
|
2019-07-26 00:46:01 -04:00
|
|
|
|
2019-08-28 12:59:39 -04:00
|
|
|
validates :photo, allow_nil: true, image: true
|
|
|
|
|
2019-09-01 10:42:16 -04:00
|
|
|
validates :account_connection_token,
|
|
|
|
allow_nil: true,
|
|
|
|
allow_blank: false,
|
|
|
|
length: { is: 32 },
|
|
|
|
format: { with: ACCOUNT_CONNECTION_TOKEN_RE }
|
|
|
|
|
2019-07-26 00:46:01 -04:00
|
|
|
###########
|
|
|
|
# Methods #
|
|
|
|
###########
|
|
|
|
|
|
|
|
def full_name
|
|
|
|
[
|
|
|
|
last_name,
|
|
|
|
first_name,
|
|
|
|
middle_name,
|
|
|
|
].map(&:presence).compact.join(' ').freeze
|
|
|
|
end
|
2019-09-02 07:48:51 -04:00
|
|
|
|
|
|
|
def generate_account_connection_token
|
|
|
|
update! account_connection_token: SecureRandom.alphanumeric(32)
|
|
|
|
end
|
2019-10-21 08:55:46 -04:00
|
|
|
|
|
|
|
def destroy_account_connection_token
|
|
|
|
update! account_connection_token: nil
|
|
|
|
end
|
2018-12-09 22:32:35 -05:00
|
|
|
end
|