diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2017-01-25 13:17:43 +0000 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2017-01-25 13:17:43 +0000 |
commit | 8a9597fc737b9c848b8e1935551edb2f1d09aab0 (patch) | |
tree | 90148cee8da324667d09226486d50e3ca9042327 /app/models/namespace.rb | |
parent | bc7febfff094531d385bb6dc3f53885fb0a2f400 (diff) | |
parent | 52c5f9c97f20529b608f5b47a7c361383ccadb54 (diff) | |
download | gitlab-ce-8a9597fc737b9c848b8e1935551edb2f1d09aab0.tar.gz |
Merge branch 'dz-nested-groups-access-improvements' into 'master'
Nested groups feature improvemetns
See merge request !8448
Diffstat (limited to 'app/models/namespace.rb')
-rw-r--r-- | app/models/namespace.rb | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb index dd33975731f..05f01445e67 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -185,8 +185,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).reorder('routes.path 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}/%").reorder('routes.path ASC') end private |