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

adds the `not equals` operator to subprojects

ulfertsabac033cd420
diff --git a/app/models/queries/work_packages/filter/subproject_filter.rb b/app/models/queries/work_packages/filter/subproject_filter.rb
index bf67731c31a..8ecc6aae611 100644
--- a/app/models/queries/work_packages/filter/subproject_filter.rb
+++ b/app/models/queries/work_packages/filter/subproject_filter.rb
@@ -37,9 +37,9 @@ class Queries::WorkPackages::Filter::SubprojectFilter <
end
def available_operators
- [::Queries::Operators::All,
- ::Queries::Operators::None,
- ::Queries::Operators::Equals]
+ all_and_none = [::Queries::Operators::All, ::Queries::Operators::None]
+
+ all_and_none + (super - all_and_none)
end
def available?
@@ -49,7 +49,7 @@ class Queries::WorkPackages::Filter::SubprojectFilter <
end
def type
- :list
+ :list_optional
end
def human_name
@@ -71,21 +71,25 @@ class Queries::WorkPackages::Filter::SubprojectFilter <
end
def where
- ids = [project.id]
-
- case operator
- when '='
- # include the selected subprojects
- ids += values.each(&:to_i)
- when '*'
- ids += visible_subproject_array.map(&:first)
- end
-
- "#{Project.table_name}.id IN (%s)" % ids.join(',')
+ "#{Project.table_name}.id IN (%s)" % ids_for_where.join(',')
end
private
+ def ids_for_where
+ [project.id] + case operator
+ when ::Queries::Operators::Equals.symbol
+ # include the selected subprojects
+ value_ints
+ when ::Queries::Operators::All.symbol
+ visible_subproject_ids
+ when ::Queries::Operators::NotEquals.symbol
+ visible_subproject_ids - value_ints
+ else # None
+ []
+ end
+ end
+
def visible_subproject_array
visible_subprojects.pluck(:id, :name)
end
@@ -101,14 +105,11 @@ class Queries::WorkPackages::Filter::SubprojectFilter <
end
end
- def operator_strategy
- case operator
- when '*'
- ::Queries::Operators::All
- when '!*'
- ::Queries::Operators::None
- when '='
- ::Queries::Operators::Equals
- end
+ def visible_subproject_ids
+ visible_subproject_array.map(&:first)
+ end
+
+ def value_ints
+ values.map(&:to_i)
end
end
diff --git a/spec/models/queries/work_packages/filter/subproject_filter_spec.rb b/spec/models/queries/work_packages/filter/subproject_filter_spec.rb
index c445451e6e8..fed31690ef1 100644
--- a/spec/models/queries/work_packages/filter/subproject_filter_spec.rb
+++ b/spec/models/queries/work_packages/filter/subproject_filter_spec.rb
@@ -30,7 +30,7 @@ require 'spec_helper'
describe Queries::WorkPackages::Filter::SubprojectFilter, type: :model do
it_behaves_like 'basic query filter' do
- let(:type) { :list }
+ let(:type) { :list_optional }
let(:class_key) { :subproject_id }
let(:name) { I18n.t('query_fields.subproject_id') }
let(:project) { FactoryBot.build_stubbed :project }
@@ -125,5 +125,59 @@ describe Queries::WorkPackages::Filter::SubprojectFilter, type: :model do
.to be_ar_object_filter
end
end
+
+ describe '#available_operators' do
+ it 'is the same as for every list_optional but the `all` operator comes first' do
+ expect(instance.available_operators)
+ .to eql([::Queries::Operators::All, ::Queries::Operators::None,
+ ::Queries::Operators::Equals, ::Queries::Operators::NotEquals])
+ end
+ end
+
+ describe '#where' do
+ let(:subproject1) { FactoryBot.build_stubbed(:project) }
+ let(:subproject2) { FactoryBot.build_stubbed(:project) }
+ let(:projects) { [subproject1, subproject2] }
+
+ context 'for the equals operator' do
+ let(:operator) { '=' }
+ let(:values) { [subproject1.id.to_s] }
+
+ it 'returns an sql filtering for project id eql self or specified values' do
+ expect(instance.where)
+ .to eql("projects.id IN (#{project.id},#{subproject1.id})")
+ end
+ end
+
+ context 'for the not equals operator' do
+ let(:operator) { '!' }
+ let(:values) { [subproject1.id.to_s] }
+
+ it 'returns an sql filtering for project id eql self and all subprojects excluding specified values' do
+ expect(instance.where)
+ .to eql("projects.id IN (#{project.id},#{subproject2.id})")
+ end
+ end
+
+ context 'for the all operator' do
+ let(:operator) { '*' }
+ let(:values) { [] }
+
+ it 'returns an sql filtering for project id eql self and all subprojects' do
+ expect(instance.where)
+ .to eql("projects.id IN (#{project.id},#{subproject1.id},#{subproject2.id})")
+ end
+ end
+
+ context 'for the none operator' do
+ let(:operator) { '!*' }
+ let(:values) { [] }
+
+ it 'returns an sql filtering for only the project id (and by that excluding all subprojects)' do
+ expect(instance.where)
+ .to eql("projects.id IN (#{project.id})")
+ end
+ end
+ end
end
end