2021-03-11 19:13:27 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Namespaces
|
|
|
|
module Traversal
|
|
|
|
module Recursive
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
def root_ancestor
|
|
|
|
return self if persisted? && parent_id.nil?
|
|
|
|
|
|
|
|
strong_memoize(:root_ancestor) do
|
|
|
|
self_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
|
2021-04-17 20:07:23 +05:30
|
|
|
object_hierarchy(self.class.where(id: id))
|
2021-03-11 19:13:27 +05:30
|
|
|
.all_objects
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns all the ancestors of the current namespaces.
|
|
|
|
def ancestors
|
|
|
|
return self.class.none unless parent_id
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
object_hierarchy(self.class.where(id: parent_id))
|
2021-03-11 19:13:27 +05:30
|
|
|
.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)
|
2021-04-17 20:07:23 +05:30
|
|
|
object_hierarchy(self.class.where(id: id))
|
2021-03-11 19:13:27 +05:30
|
|
|
.ancestors(upto: top, hierarchy_order: hierarchy_order)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self_and_ancestors(hierarchy_order: nil)
|
|
|
|
return self.class.where(id: id) unless parent_id
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
object_hierarchy(self.class.where(id: id))
|
2021-03-11 19:13:27 +05:30
|
|
|
.base_and_ancestors(hierarchy_order: hierarchy_order)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns all the descendants of the current namespace.
|
|
|
|
def descendants
|
2021-04-17 20:07:23 +05:30
|
|
|
object_hierarchy(self.class.where(parent_id: id))
|
2021-03-11 19:13:27 +05:30
|
|
|
.base_and_descendants
|
|
|
|
end
|
|
|
|
|
|
|
|
def self_and_descendants
|
2021-04-17 20:07:23 +05:30
|
|
|
object_hierarchy(self.class.where(id: id))
|
2021-03-11 19:13:27 +05:30
|
|
|
.base_and_descendants
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
def object_hierarchy(ancestors_base)
|
|
|
|
Gitlab::ObjectHierarchy.new(ancestors_base, options: { use_distinct: Feature.enabled?(:use_distinct_in_object_hierarchy, self) })
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|