summaryrefslogtreecommitdiff
path: root/app/models/work_items/parent_link.rb
blob: 3c405dbce3b73c1b9efe47cc4fdbee9aaaf2755c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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