summaryrefslogtreecommitdiff
path: root/lib/gitlab/work_items/work_item_hierarchy.rb
blob: e71bf2bce6ab63260c1a6504a632700d6d402882 (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
# frozen_string_literal: true

module Gitlab
  module WorkItems
    class WorkItemHierarchy < ObjectHierarchy
      extend ::Gitlab::Utils::Override

      private

      def middle_table
        ::WorkItems::ParentLink.arel_table
      end

      def from_tables(cte)
        [objects_table, cte.table, middle_table]
      end

      override :parent_id_column
      def parent_id_column(cte)
        middle_table[:work_item_parent_id]
      end

      override :ancestor_conditions
      def ancestor_conditions(cte)
        conditions = middle_table[:work_item_parent_id].eq(objects_table[:id]).and(
          middle_table[:work_item_id].eq(cte.table[:id])
        )

        with_type_filter(conditions, cte)
      end

      override :descendant_conditions
      def descendant_conditions(cte)
        conditions = middle_table[:work_item_id].eq(objects_table[:id]).and(
          middle_table[:work_item_parent_id].eq(cte.table[:id])
        )

        with_type_filter(conditions, cte)
      end

      def with_type_filter(conditions, cte)
        return conditions unless options[:same_type]

        conditions.and(objects_table[:work_item_type_id].eq(cte.table[:work_item_type_id]))
      end
    end
  end
end