Module: Namespaces::Traversal::Recursive

Extended by:
ActiveSupport::Concern
Includes:
RecursiveScopes
Included in:
Gitlab::BackgroundMigration::ResetStatusOnContainerRepositories::Namespace, Namespace
Defined in:
app/models/namespaces/traversal/recursive.rb

Instance Method Summary collapse

Instance Method Details

#ancestor_ids(hierarchy_order: nil) ⇒ Object Also known as: recursive_ancestor_ids



38
39
40
# File 'app/models/namespaces/traversal/recursive.rb', line 38

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

#ancestors(hierarchy_order: nil) ⇒ Object Also known as: recursive_ancestors

Returns all the ancestors of the current namespaces.



30
31
32
33
34
35
# File 'app/models/namespaces/traversal/recursive.rb', line 30

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

#ancestors_upto(top = nil, hierarchy_order: nil) ⇒ Object Also known as: recursive_ancestors_upto

returns all ancestors upto but excluding the given namespace when no namespace is given, all ancestors upto the top are returned



45
46
47
48
# File 'app/models/namespaces/traversal/recursive.rb', line 45

def ancestors_upto(top = nil, hierarchy_order: nil)
  object_hierarchy(self.class.where(id: id))
    .ancestors(upto: top, hierarchy_order: hierarchy_order)
end

#descendantsObject Also known as: recursive_descendants

Returns all the descendants of the current namespace.



65
66
67
# File 'app/models/namespaces/traversal/recursive.rb', line 65

def descendants
  object_hierarchy(self.class.where(parent_id: id)).base_and_descendants
end

#object_hierarchy(ancestors_base) ⇒ Object



80
81
82
# File 'app/models/namespaces/traversal/recursive.rb', line 80

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

#root_ancestorObject Also known as: recursive_root_ancestor



9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/namespaces/traversal/recursive.rb', line 9

def root_ancestor
  if persisted? && !parent_id.nil?
    strong_memoize(:root_ancestor) do
      recursive_ancestors.reorder(nil).find_by(parent_id: nil)
    end
  elsif parent.nil?
    self
  else
    parent.root_ancestor
  end
end

#self_and_ancestor_ids(hierarchy_order: nil) ⇒ Object Also known as: recursive_self_and_ancestor_ids



59
60
61
# File 'app/models/namespaces/traversal/recursive.rb', line 59

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

#self_and_ancestors(hierarchy_order: nil) ⇒ Object Also known as: recursive_self_and_ancestors



51
52
53
54
55
56
# File 'app/models/namespaces/traversal/recursive.rb', line 51

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

#self_and_descendant_idsObject Also known as: recursive_self_and_descendant_ids



75
76
77
# File 'app/models/namespaces/traversal/recursive.rb', line 75

def self_and_descendant_ids
  object_hierarchy(self.class.where(id: id)).base_and_descendant_ids
end

#self_and_descendantsObject Also known as: recursive_self_and_descendants



70
71
72
# File 'app/models/namespaces/traversal/recursive.rb', line 70

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

#self_and_hierarchyObject Also known as: recursive_self_and_hierarchy

Returns all ancestors, self, and descendants of the current namespace.



23
24
25
26
# File 'app/models/namespaces/traversal/recursive.rb', line 23

def self_and_hierarchy
  object_hierarchy(self.class.where(id: id))
    .all_objects
end