diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-10-04 16:57:33 +0200 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-10-05 11:10:57 +0200 |
commit | 57bd3bb34a19bf812fd6a74f394a69c491b05dd0 (patch) | |
tree | 1407f3824a3d0b22bd64fefdfd8a2b4e6ee2c8f2 /app/models/concerns/group_descendant.rb | |
parent | 06e00913f505268e0a45f4f1516a93a84600c242 (diff) | |
download | gitlab-ce-57bd3bb34a19bf812fd6a74f394a69c491b05dd0.tar.gz |
Force parents to be preloaded for building a hierarchy
Diffstat (limited to 'app/models/concerns/group_descendant.rb')
-rw-r--r-- | app/models/concerns/group_descendant.rb | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/app/models/concerns/group_descendant.rb b/app/models/concerns/group_descendant.rb index 6c465079753..8c8f7cb5e0d 100644 --- a/app/models/concerns/group_descendant.rb +++ b/app/models/concerns/group_descendant.rb @@ -22,16 +22,22 @@ module GroupDescendant private def ancestors_upto(hierarchy_top = nil) - if hierarchy_top - Gitlab::GroupHierarchy.new(Group.where(id: hierarchy_top)).base_and_descendants + if self.is_a?(Group) + Gitlab::GroupHierarchy.new(Group.where(id: id)) + .ancestors(upto: hierarchy_top) else - Gitlab::GroupHierarchy.new(Group.where(id: self)).all_groups + 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 = preloaded.detect { |possible_parent| possible_parent.is_a?(Group) && possible_parent.id == child.parent_id } - parent ||= child.parent + 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 } + + if parent.nil? && !child.parent_id.nil? + raise ArgumentError.new('parent was not preloaded') + end if parent.nil? && hierarchy_top.present? raise ArgumentError.new('specified top is not part of the tree') |