1
0
Fork 0

Add method OrgUnit#requires_parent?

This commit is contained in:
Alex Kotov 2019-10-21 12:22:47 +05:00
parent 5da3660fa9
commit 7d54eb3192
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08

View file

@ -36,13 +36,8 @@ class OrgUnit < ApplicationRecord
validates :name, good_small_text: true, uniqueness: true validates :name, good_small_text: true, uniqueness: true
validates :parent_unit, validates :parent_unit,
presence: { presence: { if: :requires_parent?, message: :required },
if: ->(record) { record.kind&.parent_kind }, absence: { unless: :requires_parent? }
message: :required,
},
absence: {
unless: ->(record) {record.kind&.parent_kind },
}
validates :resource, validates :resource,
presence: { presence: {
@ -58,6 +53,14 @@ class OrgUnit < ApplicationRecord
before_validation :set_level before_validation :set_level
###########
# Methods #
###########
def requires_parent?
kind&.parent_kind
end
private private
def parent_matches_kind def parent_matches_kind