From d806230f9f95a2d08253f6c534ba69d1b9a498ea Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Tue, 13 Dec 2016 16:59:49 +0200 Subject: Add parents method to Namespace Signed-off-by: Dmitriy Zaporozhets --- app/models/namespace.rb | 25 ++++++++++++++++++++----- 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 -- cgit v1.2.1