summaryrefslogtreecommitdiff
path: root/app/models/namespace.rb
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2017-01-05 14:43:50 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2017-01-13 10:05:02 -0500
commit51c4b20c48f29fe34fd1306f7a115f645eb9fb71 (patch)
tree645824aae9ec7fb73bf6be8496f2a3710804ffff /app/models/namespace.rb
parent4b43126d08972c201551fbd1fe42e85847d5e03f (diff)
downloadgitlab-ce-51c4b20c48f29fe34fd1306f7a115f645eb9fb71.tar.gz
Refactor Namespace code related to nested groups
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Diffstat (limited to 'app/models/namespace.rb')
-rw-r--r--app/models/namespace.rb22
1 files changed, 20 insertions, 2 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb
index d41833de66f..d3a4ddbb3bf 100644
--- a/app/models/namespace.rb
+++ b/app/models/namespace.rb
@@ -183,8 +183,26 @@ class Namespace < ActiveRecord::Base
end
end
- def parents
- @parents ||= parent ? parent.parents + [parent] : []
+ # Scopes the model on ancestors of the record
+ def ancestors
+ if parent_id
+ path = route.path
+ paths = []
+
+ until path.blank?
+ path = path.rpartition('/').first
+ paths << path
+ end
+
+ self.class.joins(:route).where('routes.path IN (?)', paths).order_id_asc
+ else
+ self.class.none
+ end
+ end
+
+ # Scopes the model on direct and indirect children of the record
+ def descendants
+ self.class.joins(:route).where('routes.path LIKE ?', "#{route.path}/%").order_id_asc
end
private