Welcome to mirror list, hosted at ThFree Co, Russian Federation.

recursive_traversal.rb « namespace « concerns « models « user_mentions « background_migration « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 75759ed0111d905a43d190f5d5bb513aaf960642 (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
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    module UserMentions
      module Models
        module Concerns
          module Namespace
            # isolate recursive traversal code for namespace hierarchy
            module RecursiveTraversal
              extend ActiveSupport::Concern

              def root_ancestor
                return self if persisted? && parent_id.nil?

                strong_memoize(:root_ancestor) do
                  Gitlab::ObjectHierarchy
                    .new(self.class.where(id: id))
                    .base_and_ancestors
                    .reorder(nil)
                    .find_by(parent_id: nil)
                end
              end

              # Returns all ancestors, self, and descendants of the current namespace.
              def self_and_hierarchy
                Gitlab::ObjectHierarchy
                  .new(self.class.where(id: id))
                  .all_objects
              end

              # Returns all the ancestors of the current namespaces.
              def ancestors
                return self.class.none unless parent_id

                Gitlab::ObjectHierarchy
                  .new(self.class.where(id: parent_id))
                  .base_and_ancestors
              end

              # 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)
                Gitlab::ObjectHierarchy.new(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

                Gitlab::ObjectHierarchy
                  .new(self.class.where(id: id))
                  .base_and_ancestors(hierarchy_order: hierarchy_order)
              end

              # Returns all the descendants of the current namespace.
              def descendants
                Gitlab::ObjectHierarchy
                  .new(self.class.where(parent_id: id))
                  .base_and_descendants
              end

              def self_and_descendants
                Gitlab::ObjectHierarchy
                  .new(self.class.where(id: id))
                  .base_and_descendants
              end
            end
          end
        end
      end
    end
  end
end