Commit f8625a98 authored by Jan Provaznik's avatar Jan Provaznik Committed by Stan Hu

Optimize namespace.has_parent? method

We can avoid doing an extra SQL query for loading parent when
only checking if parent exists.

Because in Github import service we use nested setting of groups
(so there is a situation when `group` is set but `group_id` not),
we check presence of both group_id and group.
parent b96a8fba
...@@ -256,7 +256,7 @@ class Namespace < ApplicationRecord ...@@ -256,7 +256,7 @@ class Namespace < ApplicationRecord
end end
def has_parent? def has_parent?
parent.present? parent_id.present? || parent.present?
end end
def root_ancestor def root_ancestor
......
...@@ -880,22 +880,6 @@ describe Group do ...@@ -880,22 +880,6 @@ describe Group do
end end
end end
describe '#has_parent?' do
context 'when the group has a parent' do
it 'is truthy' do
group = create(:group, :nested)
expect(group.has_parent?).to be_truthy
end
end
context 'when the group has no parent' do
it 'is falsy' do
group = create(:group, parent: nil)
expect(group.has_parent?).to be_falsy
end
end
end
context 'with uploads' do context 'with uploads' do
it_behaves_like 'model with uploads', true do it_behaves_like 'model with uploads', true do
let(:model_object) { create(:group, :with_avatar) } let(:model_object) { create(:group, :with_avatar) }
......
...@@ -933,4 +933,25 @@ describe Namespace do ...@@ -933,4 +933,25 @@ describe Namespace do
end end
end end
end end
describe '#has_parent?' do
it 'returns true when the group has a parent' do
group = create(:group, :nested)
expect(group.has_parent?).to be_truthy
end
it 'returns true when the group has an unsaved parent' do
parent = build(:group)
group = build(:group, parent: parent)
expect(group.has_parent?).to be_truthy
end
it 'returns false when the group has no parent' do
group = create(:group, parent: nil)
expect(group.has_parent?).to be_falsy
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment