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

56 lines
1.1 KiB
Ruby
Raw Normal View History

2019-09-30 12:33:05 -04:00
# frozen_string_literal: true
class OrgUnitKind < ApplicationRecord
################
# Associations #
################
belongs_to :parent_kind,
class_name: 'OrgUnitKind',
inverse_of: :children_kinds,
optional: true
has_many :children_kinds,
class_name: 'OrgUnitKind',
inverse_of: :parent_kind,
foreign_key: :parent_kind_id
2019-09-30 18:20:45 -04:00
has_many :instances,
class_name: 'OrgUnit',
inverse_of: :kind,
foreign_key: :kind_id
has_many :relation_statuses,
inverse_of: :org_unit_kind
2019-09-30 12:33:05 -04:00
###############
# Validations #
###############
validates :codename, codename: true, uniqueness: { case_sensitive: false }
validates :short_name, good_small_text: true, uniqueness: true
validates :name, good_small_text: true, uniqueness: true
2019-10-01 21:31:12 -04:00
#############
# Callbacks #
#############
before_validation :set_level
2019-09-30 12:33:05 -04:00
###########
# Methods #
###########
def to_param
codename
end
2019-10-01 21:31:12 -04:00
private
def set_level
self.level = parent_kind.nil? ? 0 : parent_kind.level + 1
end
2019-09-30 12:33:05 -04:00
end