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

Add not equals all filter for "is not" operator for CF list

Oliver Güntherbd3795741228
diff --git a/app/models/queries/filters/shared/custom_fields/list_optional.rb b/app/models/queries/filters/shared/custom_fields/list_optional.rb
index a25f77f8e4c..eccf1012228 100644
--- a/app/models/queries/filters/shared/custom_fields/list_optional.rb
+++ b/app/models/queries/filters/shared/custom_fields/list_optional.rb
@@ -65,7 +65,8 @@ module Queries::Filters::Shared
end
def customized_strategy?
- operator_strategy == Queries::Operators::CustomFields::EqualsAll
+ operator_strategy == Queries::Operators::CustomFields::EqualsAll ||
+ operator_strategy == Queries::Operators::CustomFields::NotEqualsAll
end
def type_strategy_class
diff --git a/app/models/queries/filters/strategies/cf_list_optional.rb b/app/models/queries/filters/strategies/cf_list_optional.rb
index 13312dfbece..76e0056c3a5 100644
--- a/app/models/queries/filters/strategies/cf_list_optional.rb
+++ b/app/models/queries/filters/strategies/cf_list_optional.rb
@@ -35,6 +35,7 @@ module Queries::Filters::Strategies
def operator_map
super_value = super.dup
super_value['&='] = ::Queries::Operators::CustomFields::EqualsAll
+ super_value['!'] = ::Queries::Operators::CustomFields::NotEqualsAll
super_value['!*'] = ::Queries::Operators::NoneOrBlank
super_value['*'] = ::Queries::Operators::AllAndNonBlank
diff --git a/app/models/queries/operators/custom_fields/not_equals_all.rb b/app/models/queries/operators/custom_fields/not_equals_all.rb
new file mode 100644
index 00000000000..e7a5432fbbc
--- /dev/null
+++ b/app/models/queries/operators/custom_fields/not_equals_all.rb
@@ -0,0 +1,52 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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::Operators
+ module CustomFields
+ class NotEqualsAll < ::Queries::Operators::NotEquals
+ def self.sql_for_customized(values, customized_type, customized_id_join_field)
+ # code expects strings (e.g. for quoting), but ints would work as well: unify them here
+ values = values.map(&:to_s)
+ cv_table = CustomValue.table_name
+
+ if values.present?
+ sql = values.map do |val|
+ "NOT EXISTS (SELECT 1 FROM #{cv_table} WHERE customized_type = '#{connection.quote_string(customized_type)}' " \
+ "AND customized_id = #{customized_id_join_field} " \
+ "AND value ='#{connection.quote_string(val)}')"
+ end
+
+ sql.join(' AND ')
+ else
+ # empty set of allowed values produces no result
+ '0=1'
+ end
+ end
+ end
+ end
+end
diff --git a/app/models/queries/operators/not_equals.rb b/app/models/queries/operators/not_equals.rb
index c9d5567b1b4..d735e761531 100644
--- a/app/models/queries/operators/not_equals.rb
+++ b/app/models/queries/operators/not_equals.rb
@@ -35,8 +35,6 @@ module Queries::Operators
# code expects strings (e.g. for quoting), but ints would work as well: unify them here
values = values.map(&:to_s)
- sql = ''
-
if values.present?
"(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" +
values.map { |val| "'#{connection.quote_string(val)}'" }.join(',') + '))'
diff --git a/spec/models/query/results_custom_field_filter_integration_spec.rb b/spec/models/query/results_custom_field_filter_integration_spec.rb
index 694e9ac96da..f342d079293 100644
--- a/spec/models/query/results_custom_field_filter_integration_spec.rb
+++ b/spec/models/query/results_custom_field_filter_integration_spec.rb
@@ -64,6 +64,10 @@ describe Query::Results, 'Filtering custom fields', with_mail: false do
create(:work_package, subject: 'B', type:, project:, custom_values: custom_values_for('B'))
end
+ shared_let(:wp_c) do
+ create(:work_package, subject: 'C', type:, project:, custom_values: custom_values_for('C'))
+ end
+
shared_let(:wp_a_b) do
create(:work_package, subject: 'A and B', type:, project:, custom_values: custom_values_for('A', 'B'))
end
@@ -115,12 +119,20 @@ describe Query::Results, 'Filtering custom fields', with_mail: false do
end
context 'when filtering for A OR B' do
- let(:values) { ['A', 'B'] }
+ let(:values) { %w[A B] }
it_behaves_like 'filtered work packages' do
let(:expected) { [wp_a, wp_a_b, wp_a_b_c, wp_b, wp_b_c] }
end
end
+
+ context 'when filtering for A OR B OR C' do
+ let(:values) { %w[A B C] }
+
+ it_behaves_like 'filtered work packages' do
+ let(:expected) { [wp_a, wp_a_b, wp_a_b_c, wp_b, wp_b_c, wp_c] }
+ end
+ end
end
describe 'filter for is(AND)' do
@@ -150,4 +162,32 @@ describe Query::Results, 'Filtering custom fields', with_mail: false do
end
end
end
+
+ describe 'filter for is not' do
+ let(:operator) { '!' }
+
+ context 'when filtering for A' do
+ let(:values) { ['A'] }
+
+ it_behaves_like 'filtered work packages' do
+ let(:expected) { [wp_b, wp_b_c, wp_c] }
+ end
+ end
+
+ context 'when filtering for A AND B' do
+ let(:values) { %w[A B] }
+
+ it_behaves_like 'filtered work packages' do
+ let(:expected) { [wp_c] }
+ end
+ end
+
+ context 'when filtering for A AND B AND C' do
+ let(:values) { %w[A B C] }
+
+ it_behaves_like 'filtered work packages' do
+ let(:expected) { [] }
+ end
+ end
+ end
end