From c9bf4f2a852a71d4802d4bd6413825ea8b7e38c3 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 21 Oct 2019 12:25:52 +0500 Subject: [PATCH] Add method OrgUnit#requires_resource? --- app/models/org_unit.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/app/models/org_unit.rb b/app/models/org_unit.rb index 9d53961..7454685 100644 --- a/app/models/org_unit.rb +++ b/app/models/org_unit.rb @@ -40,13 +40,8 @@ class OrgUnit < ApplicationRecord absence: { unless: :requires_parent? } validates :resource, - presence: { - if: ->(record) { record.kind&.resource_type }, - message: :required, - }, - absence: { - unless: ->(record) { record.kind&.resource_type }, - } + presence: { if: :requires_resource?, message: :required }, + absence: { unless: :requires_resource? } validate :parent_matches_kind @@ -64,6 +59,10 @@ class OrgUnit < ApplicationRecord kind&.parent_kind end + def requires_resource? + kind&.resource_type + end + private def parent_matches_kind