diff options
-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 |