summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2017-09-06 16:29:52 +0200
committerBob Van Landuyt <bob@vanlanduyt.co>2017-10-04 22:46:49 +0200
commit438a0773dc850d3fa690881ea0b022bc27435e1e (patch)
treea312a5d2eb890022d0b9ffdd64e2b1ab68f0283f /app/models
parent9f3995a0ca3d56fd8d219a2b01c3e6555fd91f27 (diff)
downloadgitlab-ce-438a0773dc850d3fa690881ea0b022bc27435e1e.tar.gz
Add a concern to build hierarchies of groups
Diffstat (limited to 'app/models')
-rw-r--r--app/models/concerns/group_hierarchy.rb27
-rw-r--r--app/models/group.rb1
-rw-r--r--app/models/project.rb1
3 files changed, 29 insertions, 0 deletions
diff --git a/app/models/concerns/group_hierarchy.rb b/app/models/concerns/group_hierarchy.rb
new file mode 100644
index 00000000000..03864023771
--- /dev/null
+++ b/app/models/concerns/group_hierarchy.rb
@@ -0,0 +1,27 @@
+module GroupHierarchy
+ def hierarchy(hierarchy_base = nil)
+ @hierarchy ||= tree_for_child(self, self, hierarchy_base)
+ end
+
+ def parent
+ if self.is_a?(Project)
+ namespace
+ else
+ super
+ end
+ end
+
+ def tree_for_child(child, tree, hierarchy_base)
+ if child.parent.nil? && hierarchy_base.present?
+ raise ArgumentError.new('specified base is not part of the tree')
+ end
+
+ if child.parent != hierarchy_base
+ tree_for_child(child.parent,
+ { child.parent => tree },
+ hierarchy_base)
+ else
+ tree
+ end
+ end
+end
diff --git a/app/models/group.rb b/app/models/group.rb
index e746e4a12c9..848e422e067 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -6,6 +6,7 @@ class Group < Namespace
include Avatarable
include Referable
include SelectForProjectAuthorization
+ include GroupHierarchy
has_many :group_members, -> { where(requested_at: nil) }, dependent: :destroy, as: :source # rubocop:disable Cop/ActiveRecordDependent
alias_method :members, :group_members
diff --git a/app/models/project.rb b/app/models/project.rb
index 59b5a5b3cd7..c221055f8b4 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -17,6 +17,7 @@ class Project < ActiveRecord::Base
include ProjectFeaturesCompatibility
include SelectForProjectAuthorization
include Routable
+ include GroupHierarchy
extend Gitlab::ConfigHelper
extend Gitlab::CurrentSettings