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/relationship.rb

43 lines
914 B
Ruby

# frozen_string_literal: true
class Relationship < ApplicationRecord
################
# Associations #
################
belongs_to :org_unit, inverse_of: :all_relationships
belongs_to :parent_rel,
class_name: 'Relationship',
inverse_of: :children_rels,
optional: true
belongs_to :status, class_name: 'RelationStatus'
belongs_to :person, inverse_of: :all_relationships
has_many :children_rels,
class_name: 'Relationship',
inverse_of: :parent_rel,
foreign_key: :parent_rel_id
###############
# Validations #
###############
validates :from_date,
presence: true,
uniqueness: { scope: %i[person_id org_unit_id] }
#############
# Callbacks #
#############
before_validation :set_level
private
def set_level
self.level = parent_rel.nil? ? 0 : parent_rel.level + 1
end
end