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

44 lines
914 B
Ruby
Raw Permalink Normal View History

2019-04-27 10:24:04 -04:00
# frozen_string_literal: true
class Relationship < ApplicationRecord
################
# Associations #
################
2019-09-30 22:30:04 -04:00
belongs_to :org_unit, inverse_of: :all_relationships
2019-10-01 21:31:12 -04:00
belongs_to :parent_rel,
class_name: 'Relationship',
inverse_of: :children_rels,
optional: true
2019-09-28 17:21:34 -04:00
belongs_to :status, class_name: 'RelationStatus'
belongs_to :person, inverse_of: :all_relationships
2019-07-20 06:52:03 -04:00
2019-10-01 21:31:12 -04:00
has_many :children_rels,
class_name: 'Relationship',
inverse_of: :parent_rel,
foreign_key: :parent_rel_id
2019-04-28 08:48:51 -04:00
###############
# Validations #
###############
validates :from_date,
presence: true,
uniqueness: { scope: %i[person_id org_unit_id] }
2019-10-01 21:31:12 -04:00
#############
# Callbacks #
#############
before_validation :set_level
private
def set_level
self.level = parent_rel.nil? ? 0 : parent_rel.level + 1
end
2019-04-27 10:24:04 -04:00
end