summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2017-10-02 18:21:18 +0200
committerBob Van Landuyt <bob@vanlanduyt.co>2017-10-04 22:49:42 +0200
commit167fd71348d145c6fee953004bf77ceebf6efb1e (patch)
tree8dcead204c15ce39758e7dc9dd55c84940c54917 /app/models
parentef043063f9d6f9f9482707d78214709b09620a78 (diff)
downloadgitlab-ce-167fd71348d145c6fee953004bf77ceebf6efb1e.tar.gz
Always preload all elements in a hierarchy to avoid extra queries.
Diffstat (limited to 'app/models')
-rw-r--r--app/models/concerns/group_descendant.rb15
1 files changed, 12 insertions, 3 deletions
diff --git a/app/models/concerns/group_descendant.rb b/app/models/concerns/group_descendant.rb
index 7d8f051d2eb..f37d23e615e 100644
--- a/app/models/concerns/group_descendant.rb
+++ b/app/models/concerns/group_descendant.rb
@@ -1,5 +1,6 @@
module GroupDescendant
- def hierarchy(hierarchy_top = nil, preloaded = [])
+ def hierarchy(hierarchy_top = nil, preloaded = nil)
+ preloaded ||= ancestors_upto(hierarchy_top)
expand_hierarchy_for_child(self, self, hierarchy_top, preloaded)
end
@@ -24,12 +25,20 @@ module GroupDescendant
private
- def expand_hierarchy_for_child(child, hierarchy, hierarchy_top, preloaded = [])
+ def ancestors_upto(hierarchy_top = nil)
+ if hierarchy_top
+ Gitlab::GroupHierarchy.new(Group.where(id: hierarchy_top)).base_and_descendants
+ else
+ Gitlab::GroupHierarchy.new(Group.where(id: self)).all_groups
+ end
+ end
+
+ def expand_hierarchy_for_child(child, hierarchy, hierarchy_top, preloaded)
parent = preloaded.detect { |possible_parent| possible_parent.is_a?(Group) && possible_parent.id == child.parent_id }
parent ||= child.parent
if parent.nil? && hierarchy_top.present?
- raise ArgumentError.new('specified base is not part of the tree')
+ raise ArgumentError.new('specified top is not part of the tree')
end
if parent && parent != hierarchy_top