summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2016-12-13 20:49:25 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2016-12-15 20:57:03 +0200
commit82f9957d466810314b8f3af672bcdd706905007a (patch)
tree3d871650d653633cea0f2419f912b7cc106d1699
parentf8a17a383562a1ce623e64d7b5f2902052432b2f (diff)
downloadgitlab-ce-82f9957d466810314b8f3af672bcdd706905007a.tar.gz
Refactor Namespace#parents methoddz-nested-groups-title-ui
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
-rw-r--r--app/models/namespace.rb12
-rw-r--r--spec/models/namespace_spec.rb8
2 files changed, 8 insertions, 12 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb
index b3cefc01b99..3830379be3e 100644
--- a/app/models/namespace.rb
+++ b/app/models/namespace.rb
@@ -172,17 +172,7 @@ class Namespace < ActiveRecord::Base
end
def parents
- @parents ||=
- begin
- parents = []
-
- if parent
- parents << parent
- parents += parent.parents
- end
-
- parents
- end
+ @parents ||= parent ? parent.parents + [parent] : []
end
private
diff --git a/spec/models/namespace_spec.rb b/spec/models/namespace_spec.rb
index 90e8f6b7227..1b10dd23c50 100644
--- a/spec/models/namespace_spec.rb
+++ b/spec/models/namespace_spec.rb
@@ -145,7 +145,13 @@ describe Namespace, models: true do
let(:group) { create(:group) }
let(:nested_group) { create(:group, parent: group) }
let(:deep_nested_group) { create(:group, parent: nested_group) }
+ let(:very_deep_nested_group) { create(:group, parent: deep_nested_group) }
- it { expect(deep_nested_group.parents).to eq([nested_group, group]) }
+ it 'returns the correct parents' do
+ expect(very_deep_nested_group.parents).to eq([group, nested_group, deep_nested_group])
+ expect(deep_nested_group.parents).to eq([group, nested_group])
+ expect(nested_group.parents).to eq([group])
+ expect(group.parents).to eq([])
+ end
end
end