summaryrefslogtreecommitdiff
path: root/app/models/namespaces/traversal/recursive.rb
blob: c1ada715d6dcb4f674d15ce2511d1743093bd50a (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# frozen_string_literal: true

module Namespaces
  module Traversal
    module Recursive
      extend ActiveSupport::Concern
      include RecursiveScopes

      def root_ancestor
        return self if parent.nil?

        if persisted?
          strong_memoize(:root_ancestor) do
            recursive_self_and_ancestors.reorder(nil).find_by(parent_id: nil)
          end
        else
          parent.root_ancestor
        end
      end
      alias_method :recursive_root_ancestor, :root_ancestor

      # Returns all ancestors, self, and descendants of the current namespace.
      def self_and_hierarchy
        object_hierarchy(self.class.where(id: id))
          .all_objects
      end
      alias_method :recursive_self_and_hierarchy, :self_and_hierarchy

      # Returns all the ancestors of the current namespaces.
      def ancestors(hierarchy_order: nil)
        return self.class.none unless parent_id

        object_hierarchy(self.class.where(id: parent_id))
          .base_and_ancestors(hierarchy_order: hierarchy_order)
      end
      alias_method :recursive_ancestors, :ancestors

      def ancestor_ids(hierarchy_order: nil)
        recursive_ancestors(hierarchy_order: hierarchy_order).pluck(:id)
      end
      alias_method :recursive_ancestor_ids, :ancestor_ids

      # returns all ancestors upto but excluding the given namespace
      # when no namespace is given, all ancestors upto the top are returned
      def ancestors_upto(top = nil, hierarchy_order: nil)
        object_hierarchy(self.class.where(id: id))
          .ancestors(upto: top, hierarchy_order: hierarchy_order)
      end

      def self_and_ancestors(hierarchy_order: nil)
        return self.class.where(id: id) unless parent_id

        object_hierarchy(self.class.where(id: id))
          .base_and_ancestors(hierarchy_order: hierarchy_order)
      end
      alias_method :recursive_self_and_ancestors, :self_and_ancestors

      def self_and_ancestor_ids(hierarchy_order: nil)
        recursive_self_and_ancestors(hierarchy_order: hierarchy_order).pluck(:id)
      end
      alias_method :recursive_self_and_ancestor_ids, :self_and_ancestor_ids

      # Returns all the descendants of the current namespace.
      def descendants
        object_hierarchy(self.class.where(parent_id: id))
          .base_and_descendants
      end
      alias_method :recursive_descendants, :descendants

      def self_and_descendants
        object_hierarchy(self.class.where(id: id))
          .base_and_descendants
      end
      alias_method :recursive_self_and_descendants, :self_and_descendants

      def self_and_descendant_ids
        recursive_self_and_descendants.select(:id)
      end
      alias_method :recursive_self_and_descendant_ids, :self_and_descendant_ids

      def object_hierarchy(ancestors_base)
        Gitlab::ObjectHierarchy.new(ancestors_base)
      end
    end
  end
end