From 7d54eb319232d6012e61a9471901b278a752dac8 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 21 Oct 2019 12:22:47 +0500 Subject: [PATCH] Add method OrgUnit#requires_parent? --- app/models/org_unit.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app/models/org_unit.rb b/app/models/org_unit.rb index f499488..6c02ce6 100644 --- a/app/models/org_unit.rb +++ b/app/models/org_unit.rb @@ -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