diff options
Diffstat (limited to 'app/models/work_items/parent_link.rb')
-rw-r--r-- | app/models/work_items/parent_link.rb | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/app/models/work_items/parent_link.rb b/app/models/work_items/parent_link.rb new file mode 100644 index 00000000000..3c405dbce3b --- /dev/null +++ b/app/models/work_items/parent_link.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +module WorkItems + class ParentLink < ApplicationRecord + self.table_name = 'work_item_parent_links' + + MAX_CHILDREN = 100 + + belongs_to :work_item + belongs_to :work_item_parent, class_name: 'WorkItem' + + validates :work_item, :work_item_parent, presence: true + validate :validate_child_type + validate :validate_parent_type + validate :validate_same_project + validate :validate_max_children + + private + + def validate_child_type + return unless work_item + + unless work_item.task? + errors.add :work_item, _('Only Task can be assigned as a child in hierarchy.') + end + end + + def validate_parent_type + return unless work_item_parent + + unless work_item_parent.issue? + errors.add :work_item_parent, _('Only Issue can be parent of Task.') + end + end + + def validate_same_project + return if work_item.nil? || work_item_parent.nil? + + if work_item.resource_parent != work_item_parent.resource_parent + errors.add :work_item_parent, _('Parent must be in the same project as child.') + end + end + + def validate_max_children + return unless work_item_parent + + max = persisted? ? MAX_CHILDREN : MAX_CHILDREN - 1 + if work_item_parent.child_links.count > max + errors.add :work_item_parent, _('Parent already has maximum number of children.') + end + end + end +end |