Improve GeoNode#project_ids to save one query

parent 5a699322
...@@ -109,9 +109,9 @@ class GeoNode < ActiveRecord::Base ...@@ -109,9 +109,9 @@ class GeoNode < ActiveRecord::Base
end end
def project_ids def project_ids
return unless namespaces.any? namespaces.flat_map { |namespace| namespace.all_projects.select(:id).pluck(:id) }
.uniq
namespaces.flat_map { |namespace| namespace.all_projects.pluck(:id) }.uniq .presence
end end
private private
......
...@@ -318,14 +318,14 @@ describe GeoNode, type: :model do ...@@ -318,14 +318,14 @@ describe GeoNode, type: :model do
end end
describe '#project_ids' do describe '#project_ids' do
context 'without group restriction' do context 'without namespace restriction' do
it 'returns nil' do it 'returns nil' do
expect(node.project_ids).to be_nil expect(node.project_ids).to be_nil
end end
end end
context 'with group restrictions' do context 'with namespace restrictions' do
it 'returns an array with unique project ids that belong to the groups' do it 'returns an array with unique project ids that belong to the namespaces' do
group_1 = create(:group) group_1 = create(:group)
group_2 = create(:group) group_2 = create(:group)
nested_group_1 = create(:group, parent: group_1) nested_group_1 = create(:group, parent: group_1)
......
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