debian-mirror-gitlab/app/models/namespaces/traversal/recursive.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
2.9 KiB
Ruby
Raw Normal View History

2021-03-11 19:13:27 +05:30
# frozen_string_literal: true
module Namespaces
module Traversal
module Recursive
extend ActiveSupport::Concern
2021-10-27 15:23:28 +05:30
include RecursiveScopes
2021-03-11 19:13:27 +05:30
def root_ancestor
2021-11-11 11:23:49 +05:30
if persisted? && !parent_id.nil?
2021-04-29 21:17:54 +05:30
strong_memoize(:root_ancestor) do
2021-11-11 11:23:49 +05:30
recursive_ancestors.reorder(nil).find_by(parent_id: nil)
2021-04-29 21:17:54 +05:30
end
2021-11-11 11:23:49 +05:30
elsif parent.nil?
self
2021-04-29 21:17:54 +05:30
else
parent.root_ancestor
2021-03-11 19:13:27 +05:30
end
end
2021-09-04 01:27:46 +05:30
alias_method :recursive_root_ancestor, :root_ancestor
2021-03-11 19:13:27 +05:30
# 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
2021-04-29 21:17:54 +05:30
alias_method :recursive_self_and_hierarchy, :self_and_hierarchy
2021-03-11 19:13:27 +05:30
# Returns all the ancestors of the current namespaces.
2021-09-30 23:02:18 +05:30
def ancestors(hierarchy_order: nil)
2021-03-11 19:13:27 +05:30
return self.class.none unless parent_id
2021-04-17 20:07:23 +05:30
object_hierarchy(self.class.where(id: parent_id))
2021-09-30 23:02:18 +05:30
.base_and_ancestors(hierarchy_order: hierarchy_order)
2021-03-11 19:13:27 +05:30
end
2021-04-29 21:17:54 +05:30
alias_method :recursive_ancestors, :ancestors
2021-03-11 19:13:27 +05:30
2021-09-30 23:02:18 +05:30
def ancestor_ids(hierarchy_order: nil)
recursive_ancestors(hierarchy_order: hierarchy_order).pluck(:id)
end
alias_method :recursive_ancestor_ids, :ancestor_ids
2021-03-11 19:13:27 +05:30
# 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
2022-01-26 12:08:38 +05:30
alias_method :recursive_ancestors_upto, :ancestors_upto
2021-03-11 19:13:27 +05:30
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
2021-04-29 21:17:54 +05:30
alias_method :recursive_self_and_ancestors, :self_and_ancestors
2021-03-11 19:13:27 +05:30
2021-09-30 23:02:18 +05:30
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
2021-03-11 19:13:27 +05:30
# 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
2021-04-29 21:17:54 +05:30
alias_method :recursive_descendants, :descendants
2021-03-11 19:13:27 +05:30
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-29 21:17:54 +05:30
alias_method :recursive_self_and_descendants, :self_and_descendants
2021-04-17 20:07:23 +05:30
2021-09-04 01:27:46 +05:30
def self_and_descendant_ids
2021-09-30 23:02:18 +05:30
recursive_self_and_descendants.select(:id)
2021-09-04 01:27:46 +05:30
end
alias_method :recursive_self_and_descendant_ids, :self_and_descendant_ids
2021-04-17 20:07:23 +05:30
def object_hierarchy(ancestors_base)
2021-09-30 23:02:18 +05:30
Gitlab::ObjectHierarchy.new(ancestors_base)
2021-04-17 20:07:23 +05:30
end
2021-03-11 19:13:27 +05:30
end
end
end