OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
rewrite project ancestor filter with exists
ulferts40bc17fa1bcf
diff --git a/app/models/queries/projects/filters/ancestor_filter.rb b/app/models/queries/projects/filters/ancestor_filter.rbindex 9f7fad23a98..b7c821fbfc1 100644--- a/app/models/queries/projects/filters/ancestor_filter.rb+++ b/app/models/queries/projects/filters/ancestor_filter.rb@@ -33,10 +33,11 @@ module Queriesdef scopecase operatorwhen '='- Project.joins(join_specific_ancestor_projects.join_sources)+ Project+ .where(exists_condition.exists)when '!'- Project.joins(left_join_ancestor_projects.join_sources)- .where(ancestor_not_in_values_condition)+ Project+ .where.not(exists_condition.exists)elseraise "unsupported operator"end@@ -60,21 +61,10 @@ module Queries@type_strategy ||= ::Queries::Filters::Strategies::IntegerList.new(self)end- def join_specific_ancestor_projects- projects_table- .join(projects_ancestor_table)- .on(specific_ancestor_condition)- end-- def left_join_ancestor_projects- projects_table- .outer_join(projects_ancestor_table)- .on(ancestor_condition)- end-- def specific_ancestor_condition- ancestor_condition- .and(ancestor_in_values_condition)+ def exists_condition+ Project.from("#{Project.table_name} ancestors")+ .where(ancestor_condition.and(ancestor_in_values_condition))+ .arelenddef ancestor_condition@@ -87,18 +77,12 @@ module Queriesprojects_ancestor_table[:id].in(values)end- def ancestor_not_in_values_condition- projects_ancestor_table[:id]- .not_in(values)- .or(projects_ancestor_table[:id].eq(nil))- end-def projects_tableProject.arel_tableenddef projects_ancestor_table- projects_table.alias(:ancestor_projects)+ projects_table.alias(:ancestors)endendenddiff --git a/spec/models/queries/projects/project_query_results_spec.rb b/spec/models/queries/projects/project_query_results_spec.rbindex ee0a4fc638f..9bf2b010a17 100644--- a/spec/models/queries/projects/project_query_results_spec.rb+++ b/spec/models/queries/projects/project_query_results_spec.rb@@ -31,44 +31,47 @@ require 'spec_helper'RSpec.describe Queries::Projects::ProjectQuery, 'results' dolet(:instance) { described_class.new }let(:base_scope) { Project.order(id: :desc) }- let(:current_user) { build_stubbed(:admin) }- before do- login_as(current_user)+ shared_let(:view_role) { create(:project_role, permissions: %i[view_project]) }+ shared_let(:non_member_role) { create(:non_member, permissions: %i[view_project]) }+ shared_let(:grandparent) { create(:project, name: 'Grandparent') }+ shared_let(:parent) { create(:project, parent: grandparent, name: 'Parent') }+ shared_let(:child) { create(:project, parent:, name: 'Child') }+ shared_let(:grandchild) { create(:project, parent: child, name: 'Grandchild') }+ shared_let(:sibling) { create(:project, parent:, name: 'Sibling') }+ shared_let(:not_member) { create(:project, name: 'Not member') }+ shared_let(:public) { create(:public_project, name: 'Public') }+ shared_let(:no_hierarchy) { create(:project, name: 'No Hierarchy') }++ shared_let(:user) do+ create(:user, member_with_roles: {+ grandparent => [view_role],+ parent => [view_role],+ child => [view_role],+ grandchild => [view_role],+ sibling => [view_role],+ no_hierarchy => [view_role]+ })end- context 'without a filter' do- context 'as an admin' do- it 'is the same as getting all projects' do- expect(instance.results.to_sql).to eql base_scope.to_sql- end- end+ current_user { user }- context 'as a non admin' do- let(:current_user) { build_stubbed(:user) }-- it 'is the same as getting all visible projects' do- expect(instance.results.to_sql).to eql base_scope.where(id: Project.visible).to_sql- end+ context 'without a filter' do+ it 'gets all visible projects' do+ expect(instance.results)+ .to contain_exactly(grandparent, parent, child, sibling, grandchild, public, no_hierarchy)endendcontext 'with a parent filter' docontext 'with a "=" operator' dobefore do- allow(Project)- .to receive_message_chain(:visible, :pluck) # rubocop:disable RSpec/MessageChain- .with(:id)- .and_return([8])-- instance.where('parent_id', '=', ['8'])+ instance.where('parent_id', '=', [parent.id])end- it 'is the same as handwriting the query' do- expected = base_scope- .where(projects: { parent_id: ['8'] })-- expect(instance.results.to_sql).to eql expected.to_sql+ it 'returns all children of the specified parent' do+ expect(instance.results)+ .to contain_exactly(child, sibling)endendend@@ -76,68 +79,31 @@ RSpec.describe Queries::Projects::ProjectQuery, 'results' docontext 'with an ancestor filter' docontext 'with a "=" operator' dobefore do- instance.where('ancestor', '=', ['8'])+ instance.where('ancestor', '=', [grandparent.id])end- describe '#results' do- it 'is the same as handwriting the query' do- projects_table = Project.arel_table- projects_ancestor_table = projects_table.alias(:ancestor_projects)-- condition = projects_table[:lft]- .gt(projects_ancestor_table[:lft])- .and(projects_table[:rgt].lt(projects_ancestor_table[:rgt]))- .and(projects_ancestor_table[:id].in(['8']))-- arel = projects_table- .join(projects_ancestor_table)- .on(condition)-- expected = base_scope.joins(arel.join_sources)-- expect(instance.results.to_sql).to eql expected.to_sql- end+ it 'gets all projects that are descendants' do+ expect(instance.results)+ .to contain_exactly(parent, child, sibling, grandchild)endendcontext 'with a "!" operator' dobefore do- instance.where('ancestor', '!', ['8'])+ instance.where('ancestor', '!', [grandparent.id])end- describe '#results' do- it 'is the same as handwriting the query' do- projects_table = Project.arel_table- projects_ancestor_table = projects_table.alias(:ancestor_projects)-- condition = projects_table[:lft]- .gt(projects_ancestor_table[:lft])- .and(projects_table[:rgt].lt(projects_ancestor_table[:rgt]))-- arel = projects_table- .outer_join(projects_ancestor_table)- .on(condition)-- where_condition = projects_ancestor_table[:id]- .not_in(['8'])- .or(projects_ancestor_table[:id].eq(nil))-- expected = base_scope- .joins(arel.join_sources)- .where(where_condition)-- expect(instance.results.to_sql).to eql expected.to_sql- end+ it 'gets all projects that are not descendants' do+ expect(instance.results)+ .to contain_exactly(grandparent, public, no_hierarchy)endendendcontext 'with an order by id asc' do- describe '#results' do- it 'returns all visible projects ordered by id asc' do- expect(instance.order(id: :asc).results.to_sql)- .to eql base_scope.except(:order).order(id: :asc).to_sql- end+ it 'returns all visible projects ordered by id asc' do+ expect(instance.order(id: :asc).results.to_a)+ .to eql [grandparent, parent, child, sibling, grandchild, public, no_hierarchy].sort_by(&:id)endend@@ -146,11 +112,9 @@ RSpec.describe Queries::Projects::ProjectQuery, 'results' doinstance.order(typeahead: :asc)end- describe '#results' do- it 'returns all visible projects ordered by lft asc' do- expect(instance.results.to_sql)- .to eql base_scope.except(:order).order(lft: :asc, id: :desc).to_sql- end+ it 'returns all visible projects ordered by lft asc' do+ expect(instance.results.to_a)+ .to eql [grandparent, parent, child, grandchild, sibling, no_hierarchy, public]endendend