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

43 lines
943 B
Ruby

# frozen_string_literal: true
class OrgUnit < ApplicationRecord
################
# Associations #
################
belongs_to :kind,
class_name: 'OrgUnitKind',
inverse_of: :instances
belongs_to :parent,
class_name: 'OrgUnit',
inverse_of: :children,
optional: true
has_many :children,
class_name: 'OrgUnit',
inverse_of: :parent,
foreign_key: :parent_id
###############
# Validations #
###############
validates :short_name, good_small_text: true, uniqueness: true
validates :name, good_small_text: true, uniqueness: true
validates :parent,
presence: {
if: ->(record) { record.kind&.parent_kind },
message: :required,
}
validate :parent_matches_kind
private
def parent_matches_kind
errors.add :parent unless parent&.kind == kind&.parent_kind
end
end