summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2017-10-10 15:45:35 +0200
committerBob Van Landuyt <bob@vanlanduyt.co>2017-10-10 16:55:02 +0200
commit9d1348d66838b4c5e25ba133d486239482973fca (patch)
treed7c56904b3f991c462be9c593ef9e130addd1442 /app/models
parent7611e6a0f745393aa432e4201e8053072a263c2e (diff)
downloadgitlab-ce-9d1348d66838b4c5e25ba133d486239482973fca.tar.gz
Move the `ancestors_upto` to `Project` and `Namespace`
Diffstat (limited to 'app/models')
-rw-r--r--app/models/concerns/group_descendant.rb21
-rw-r--r--app/models/namespace.rb7
-rw-r--r--app/models/project.rb7
3 files changed, 25 insertions, 10 deletions
diff --git a/app/models/concerns/group_descendant.rb b/app/models/concerns/group_descendant.rb
index 8c8f7cb5e0d..01957da0bf3 100644
--- a/app/models/concerns/group_descendant.rb
+++ b/app/models/concerns/group_descendant.rb
@@ -1,9 +1,20 @@
module GroupDescendant
+ # Returns the hierarchy of a project or group in the from of a hash upto a
+ # given top.
+ #
+ # > project.hierarchy
+ # => { parent_group => { child_group => project } }
def hierarchy(hierarchy_top = nil, preloaded = nil)
preloaded ||= ancestors_upto(hierarchy_top)
expand_hierarchy_for_child(self, self, hierarchy_top, preloaded)
end
+ # Merges all hierarchies of the given groups or projects into an array of
+ # hashes. All ancestors need to be loaded into the given `descendants` to avoid
+ # queries down the line.
+ #
+ # > GroupDescendant.merge_hierarchy([project, child_group, child_group2, parent])
+ # => { parent => [{ child_group => project}, child_group2] }
def self.build_hierarchy(descendants, hierarchy_top = nil)
descendants = Array.wrap(descendants).uniq
return [] if descendants.empty?
@@ -21,16 +32,6 @@ module GroupDescendant
private
- def ancestors_upto(hierarchy_top = nil)
- if self.is_a?(Group)
- Gitlab::GroupHierarchy.new(Group.where(id: id))
- .ancestors(upto: hierarchy_top)
- else
- Gitlab::GroupHierarchy.new(Group.where(id: parent_id))
- .base_and_ancestors(upto: hierarchy_top)
- end
- end
-
def expand_hierarchy_for_child(child, hierarchy, hierarchy_top, preloaded)
parent = hierarchy_top if hierarchy_top && child.parent_id == hierarchy_top.id
parent ||= preloaded.detect { |possible_parent| possible_parent.is_a?(Group) && possible_parent.id == child.parent_id }
diff --git a/app/models/namespace.rb b/app/models/namespace.rb
index e279d8dd8c5..a6f517ce2ce 100644
--- a/app/models/namespace.rb
+++ b/app/models/namespace.rb
@@ -160,6 +160,13 @@ class Namespace < ActiveRecord::Base
.base_and_ancestors
end
+ # returns all ancestors upto but excluding the the given namespace
+ # when no namespace is given, all ancestors upto the top are returned
+ def ancestors_upto(top = nil)
+ Gitlab::GroupHierarchy.new(self.class.where(id: id))
+ .ancestors(upto: top)
+ end
+
def self_and_ancestors
return self.class.where(id: id) unless parent_id
diff --git a/app/models/project.rb b/app/models/project.rb
index a60aa28cd36..fdbc1f5d50c 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -471,6 +471,13 @@ class Project < ActiveRecord::Base
end
end
+ # returns all ancestor-groups upto but excluding the given namespace
+ # when no namespace is given, all ancestors upto the top are returned
+ def ancestors_upto(top = nil)
+ Gitlab::GroupHierarchy.new(Group.where(id: namespace_id))
+ .base_and_ancestors(upto: top)
+ end
+
def lfs_enabled?
return namespace.lfs_enabled? if self[:lfs_enabled].nil?