Refactor Namespace#parents method
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
parent
f8a17a3835
commit
82f9957d46
2 changed files with 8 additions and 12 deletions
|
@ -172,17 +172,7 @@ class Namespace < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def parents
|
def parents
|
||||||
@parents ||=
|
@parents ||= parent ? parent.parents + [parent] : []
|
||||||
begin
|
|
||||||
parents = []
|
|
||||||
|
|
||||||
if parent
|
|
||||||
parents << parent
|
|
||||||
parents += parent.parents
|
|
||||||
end
|
|
||||||
|
|
||||||
parents
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -145,7 +145,13 @@ describe Namespace, models: true do
|
||||||
let(:group) { create(:group) }
|
let(:group) { create(:group) }
|
||||||
let(:nested_group) { create(:group, parent: group) }
|
let(:nested_group) { create(:group, parent: group) }
|
||||||
let(:deep_nested_group) { create(:group, parent: nested_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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue