Skip to content

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

fix[Op#54459]: Replace unneeded `list_contains` filter and use `list`

Kabiru Mwenja4383421e6cfa
diff --git a/app/models/projects/scopes/with_available_custom_fields.rb b/app/models/projects/scopes/with_available_custom_fields.rb
index e9eee25e596..cac1a5a0ff1 100644
--- a/app/models/projects/scopes/with_available_custom_fields.rb
+++ b/app/models/projects/scopes/with_available_custom_fields.rb
@@ -32,10 +32,21 @@ module Projects::Scopes
class_methods do
def with_available_custom_fields(custom_field_ids)
- mappings_subquery = ProjectCustomFieldProjectMapping.select(:project_id)
+ subquery = project_custom_fields_project_mapping_subquery(custom_field_ids:)
+ where(id: subquery)
+ end
+
+ def without_available_custom_fields(custom_field_ids)
+ subquery = project_custom_fields_project_mapping_subquery(custom_field_ids:)
+ where.not(id: subquery)
+ end
+
+ private
+
+ def project_custom_fields_project_mapping_subquery(custom_field_ids:)
+ ProjectCustomFieldProjectMapping.select(:project_id)
.where(custom_field_id: custom_field_ids)
.distinct
- where(id: mappings_subquery)
end
end
end
diff --git a/app/models/queries/filters.rb b/app/models/queries/filters.rb
index 13c01bf5436..4f5fbf38d98 100644
--- a/app/models/queries/filters.rb
+++ b/app/models/queries/filters.rb
@@ -31,7 +31,6 @@ module Queries::Filters
list: Queries::Filters::Strategies::List,
list_all: Queries::Filters::Strategies::ListAll,
list_optional: Queries::Filters::Strategies::ListOptional,
- list_contains: Queries::Filters::Strategies::ListContains,
shared_with_user_list_optional: Queries::Filters::Strategies::WorkPackages::SharedWithUser::ListOptional,
integer: Queries::Filters::Strategies::Integer,
date: Queries::Filters::Strategies::Date,
diff --git a/app/models/queries/filters/strategies/list_contains.rb b/app/models/queries/filters/strategies/list_contains.rb
deleted file mode 100644
index 4f10971d9be..00000000000
--- a/app/models/queries/filters/strategies/list_contains.rb
+++ /dev/null
@@ -1,34 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2024 the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2013 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See COPYRIGHT and LICENSE files for more details.
-#++
-
-module Queries::Filters::Strategies
- class ListContains < List
- self.supported_operators = ["~"]
- self.default_operator = "~"
- end
-end
diff --git a/app/models/queries/projects/filters/available_project_attributes_filter.rb b/app/models/queries/projects/filters/available_project_attributes_filter.rb
index d6847af4d62..1eab1b6ef8a 100644
--- a/app/models/queries/projects/filters/available_project_attributes_filter.rb
+++ b/app/models/queries/projects/filters/available_project_attributes_filter.rb
@@ -35,7 +35,7 @@ class Queries::Projects::Filters::AvailableProjectAttributesFilter < Queries::Pr
end
def type
- :list_contains
+ :list
end
def allowed_values
@@ -47,7 +47,14 @@ class Queries::Projects::Filters::AvailableProjectAttributesFilter < Queries::Pr
end
def scope
- model.with_available_custom_fields(values)
+ case operator
+ when "="
+ model.with_available_custom_fields(values)
+ when "!"
+ model.without_available_custom_fields(values)
+ else
+ raise "unsupported operator"
+ end
end
def human_name
diff --git a/spec/models/projects/scopes/with_available_custom_fields_spec.rb b/spec/models/projects/scopes/with_available_custom_fields_spec.rb
index 9766f08d46f..83f384f5d98 100644
--- a/spec/models/projects/scopes/with_available_custom_fields_spec.rb
+++ b/spec/models/projects/scopes/with_available_custom_fields_spec.rb
@@ -41,4 +41,10 @@ RSpec.describe Projects::Scopes::WithAvailableCustomFields do
expect(Project.with_available_custom_fields([project_custom_field.id])).to contain_exactly(project)
end
end
+
+ describe ".without_available_custom_fields" do
+ it "returns projects without the given custom fields" do
+ expect(Project.without_available_custom_fields([project_custom_field.id])).to be_empty
+ end
+ end
end
diff --git a/spec/models/queries/projects/filters/available_project_attributes_filter_spec.rb b/spec/models/queries/projects/filters/available_project_attributes_filter_spec.rb
index 9e8ae246517..fb910185f89 100644
--- a/spec/models/queries/projects/filters/available_project_attributes_filter_spec.rb
+++ b/spec/models/queries/projects/filters/available_project_attributes_filter_spec.rb
@@ -31,13 +31,15 @@ require "spec_helper"
RSpec.describe Queries::Projects::Filters::AvailableProjectAttributesFilter do
it_behaves_like "basic query filter" do
let(:class_key) { :available_project_attributes }
- let(:type) { :list_contains }
+ let(:type) { :list }
let(:human_name) { "Available project attributes" }
end
- it_behaves_like "list_contains query filter" do
+ it_behaves_like "list query filter", scope: false do
let(:project_custom_field_project_mapping1) { build_stubbed(:project_custom_field_project_mapping) }
let(:project_custom_field_project_mapping2) { build_stubbed(:project_custom_field_project_mapping) }
+ let(:valid_values) { [project_custom_field_project_mapping1.id, project_custom_field_project_mapping2.id] }
+ let(:name) { "Available project attributes" }
before do
allow(ProjectCustomFieldProjectMapping)
@@ -47,8 +49,52 @@ RSpec.describe Queries::Projects::Filters::AvailableProjectAttributesFilter do
project_custom_field_project_mapping2.id])
end
- let(:name) { "Available project attributes" }
- let(:valid_values) { [project_custom_field_project_mapping1.id, project_custom_field_project_mapping2.id] }
+ describe "#scope" do
+ let(:values) { valid_values }
+
+ let(:project_custom_field_project_mapping_handwritten_sql_subquery) do
+ <<-SQL.squish
+ SELECT DISTINCT "project_custom_field_project_mappings"."project_id"
+ FROM "project_custom_field_project_mappings"
+ WHERE "project_custom_field_project_mappings"."custom_field_id"
+ IN (#{values.join(', ')})
+ SQL
+ end
+
+ context 'for "="' do
+ let(:operator) { "=" }
+
+ it "is the same as handwriting the query" do
+ handwritten_scope_sql = <<-SQL.squish
+ SELECT "projects".* FROM "projects"
+ WHERE "projects"."id" IN (#{project_custom_field_project_mapping_handwritten_sql_subquery})
+ SQL
+
+ expect(instance.scope.to_sql).to eql handwritten_scope_sql
+ end
+ end
+
+ context 'for "!"' do
+ let(:operator) { "!" }
+
+ it "is the same as handwriting the query" do
+ handwritten_scope_sql = <<-SQL.squish
+ SELECT "projects".* FROM "projects"
+ WHERE "projects"."id" NOT IN (#{project_custom_field_project_mapping_handwritten_sql_subquery})
+ SQL
+
+ expect(instance.scope.to_sql).to eql handwritten_scope_sql
+ end
+ end
+
+ context "for an unsupported operator" do
+ let(:operator) { "!=" }
+
+ it "raises an error" do
+ expect { instance.scope }.to raise_error("unsupported operator")
+ end
+ end
+ end
describe "#allowed_values" do
it "is a list of the possible values" do
diff --git a/spec/support/queries/filters/shared_filter_examples.rb b/spec/support/queries/filters/shared_filter_examples.rb
index 0e40e88c300..de923493c9c 100644
--- a/spec/support/queries/filters/shared_filter_examples.rb
+++ b/spec/support/queries/filters/shared_filter_examples.rb
@@ -370,54 +370,6 @@ RSpec.shared_examples_for "list_all query filter" do
end
end
-RSpec.shared_examples_for "list_contains query filter" do
- include_context "filter tests"
- let(:type) { :list_contains }
-
- describe "#scope" do
- let(:values) { valid_values }
-
- context 'for "~"' do
- let(:operator) { "~" }
-
- it "is the same as handwriting the query" do
- handwritten_scope_sql = <<-SQL.squish
- SELECT "projects".* FROM "projects"
- INNER JOIN "project_custom_field_project_mappings" ON "project_custom_field_project_mappings"."project_id" = "projects"."id"
- WHERE "project_custom_field_project_mappings"."custom_field_id" IN (#{values.join(', ')})
- SQL
-
- expect(instance.scope.to_sql).to eql handwritten_scope_sql
- end
- end
- end
-
- describe "#valid?" do
- let(:operator) { "~" }
- let(:values) { valid_values }
-
- it "is valid" do
- expect(instance).to be_valid
- end
-
- context "for an invalid operator" do
- let(:operator) { "=" }
-
- it "is invalid" do
- expect(instance).not_to be_valid
- end
- end
-
- context "for an invalid value" do
- let(:values) { ["inexistent"] }
-
- it "is invalid" do
- expect(instance).not_to be_valid
- end
- end
- end
-end
-
RSpec.shared_examples_for "boolean query filter" do |scope: true|
include_context "filter tests"
let(:attribute) { raise "needs to be defined" }