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 :parent_unit,
presence: {
if: ->(record) { record.kind&.parent_kind },
message: :required,
},
absence: {
unless: ->(record) {record.kind&.parent_kind },
}
presence: { if: :requires_parent?, message: :required },
absence: { unless: :requires_parent? }
validates :resource,
presence: {
@ -58,6 +53,14 @@ class OrgUnit < ApplicationRecord
before_validation :set_level
###########
# Methods #
###########
def requires_parent?
kind&.parent_kind
end
private
def parent_matches_kind