Improve GeoNode#project_ids to save one query

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