diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2016-12-13 16:59:49 +0200 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2016-12-13 16:59:49 +0200 |
commit | d806230f9f95a2d08253f6c534ba69d1b9a498ea (patch) | |
tree | 96b9c5158648ae6e16ccb85ca2c284d0d4c68034 | |
parent | 5c06875c39c8b525db41e73560f8cb1746119dd9 (diff) | |
download | gitlab-ce-d806230f9f95a2d08253f6c534ba69d1b9a498ea.tar.gz |
Add parents method to Namespace
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
-rw-r--r-- | app/models/namespace.rb | 25 | ||||
-rw-r--r-- | spec/models/namespace_spec.rb | 8 |
2 files changed, 28 insertions, 5 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb index 464be910f5a..b3cefc01b99 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -163,11 +163,26 @@ class Namespace < ActiveRecord::Base end def full_name - if parent - parent.full_name + ' / ' + name - else - name - end + @full_name ||= + if parent + parent.full_name + ' / ' + name + else + name + end + end + + def parents + @parents ||= + begin + parents = [] + + if parent + parents << parent + parents += parent.parents + end + + parents + end end private diff --git a/spec/models/namespace_spec.rb b/spec/models/namespace_spec.rb index 7f82e85563b..265ffc330e3 100644 --- a/spec/models/namespace_spec.rb +++ b/spec/models/namespace_spec.rb @@ -132,4 +132,12 @@ describe Namespace, models: true do it { expect(group.full_path).to eq(group.path) } it { expect(nested_group.full_path).to eq("#{group.path}/#{nested_group.path}") } end + + describe '#parents' do + let(:group) { create(:group) } + let(:nested_group) { create(:group, parent: group) } + let(:deep_nested_group) { create(:group, parent: nested_group) } + + it { expect(deep_nested_group.parents).to eq([nested_group, group]) } + end end |