# frozen_string_literals: true module Jobshop class Person < ApplicationRecord self.primary_keys = %i[ organization_id person_id ] after_initialize { self.person_id ||= SecureRandom.uuid if new_record? } belongs_to :organization, inverse_of: :people has_one :company_person, inverse_of: :person, foreign_key: %i[ organization_id person_id ] has_one :company, through: :company_person, foreign_key: %i[ organization_id company_id ] validates :email, presence: { if: :email_required? }, format: { if: :email_required?, with: /\A[^@\s]+@[^@\s]+\z/ }, uniqueness: { if: :email_changed?, scope: :organization_id, case_sensitive: false } def name @name = [ forename, surname ].join(" ") end private def email_required? true end end end