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
[Op#5715]: Define `AvailableCustomFieldsProjectFilter`
Kabiru Mwenjab608b108d841
diff --git a/app/models/queries/projects.rb b/app/models/queries/projects.rbindex f1dc49b0b1c..4cf3c4f501c 100644--- a/app/models/queries/projects.rb+++ b/app/models/queries/projects.rb@@ -30,6 +30,7 @@ module Queries::Projects::Queries::Register.register(ProjectQuery) dofilter Filters::AncestorFilterfilter Filters::AvailableProjectAttributesFilter+ filter Filters::AvailableProjectCustomFieldsFilterfilter Filters::TypeFilterfilter Filters::ActiveFilterfilter Filters::TemplatedFilterdiff --git a/app/models/queries/projects/filters/available_project_attributes_filter.rb b/app/models/queries/projects/filters/available_project_attributes_filter.rbindex 0fe1ca82526..56bdf29449d 100644--- a/app/models/queries/projects/filters/available_project_attributes_filter.rb+++ b/app/models/queries/projects/filters/available_project_attributes_filter.rb@@ -52,9 +52,9 @@ class Queries::Projects::Filters::AvailableProjectAttributesFilter < Queries::Prdef apply_to(_query_scope)case operatorwhen "="- super.with_available_custom_fields(values)+ super.with_available_project_custom_fields(values)when "!"- super.without_available_custom_fields(values)+ super.without_available_project_custom_fields(values)elseraise "unsupported operator"enddiff --git a/app/models/queries/projects/filters/available_project_custom_fields_filter.rb b/app/models/queries/projects/filters/available_project_custom_fields_filter.rbnew file mode 100644index 00000000000..459e5268657--- /dev/null+++ b/app/models/queries/projects/filters/available_project_custom_fields_filter.rb@@ -0,0 +1,70 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+# +++#++class Queries::Projects::Filters::AvailableProjectCustomFieldsFilter < Queries::Projects::Filters::Base+ def self.key+ :available_project_custom_fields+ end++ def type+ :list+ end++ def allowed_values+ @allowed_values ||= CustomFieldsProject+ .includes(:custom_field)+ .distinct+ .pluck(:name, :custom_field_id)+ end++ def available?+ User.current.admin?+ end++ def apply_to(_query_scope)+ case operator+ when "="+ super.with_available_custom_fields(values)+ when "!"+ super.without_available_custom_fields(values)+ else+ raise "unsupported operator"+ end+ end++ def where+ nil+ end++ def human_name+ I18n.t(:label_available_project_custom_fields)+ end+enddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex d647c65ab1d..0f1dbeced65 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -2066,6 +2066,7 @@ en:label_authentication_settings: "Authentication settings"label_available_global_roles: "Available global roles"label_available_project_attributes: "Available project attributes"+ label_available_project_custom_fields: "Available project custom fields"label_available_project_forums: "Available forums"label_available_project_repositories: "Available repositories"label_available_project_versions: "Available versions"diff --git a/spec/factories/custom_fields_project_factory.rb b/spec/factories/custom_fields_project_factory.rbnew file mode 100644index 00000000000..3f8f56fa79e--- /dev/null+++ b/spec/factories/custom_fields_project_factory.rb@@ -0,0 +1,34 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+#++++FactoryBot.define do+ factory :custom_fields_project do+ custom_field+ project+ end+enddiff --git a/spec/models/queries/projects/filters/available_project_custom_fields_filter_spec.rb b/spec/models/queries/projects/filters/available_project_custom_fields_filter_spec.rbnew file mode 100644index 00000000000..ec484664b73--- /dev/null+++ b/spec/models/queries/projects/filters/available_project_custom_fields_filter_spec.rb@@ -0,0 +1,107 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+#++++require "spec_helper"++RSpec.describe Queries::Projects::Filters::AvailableProjectCustomFieldsFilter do+ it_behaves_like "basic query filter" do+ let(:class_key) { :available_project_custom_fields }+ let(:type) { :list }+ let(:human_name) { "Available project custom fields" }+ end++ it_behaves_like "list query filter", scope: false do+ shared_let(:project) { create(:project) }++ let(:custom_field_project1) { create(:custom_fields_project, project:) }+ let(:custom_field_project2) { create(:custom_fields_project, project:) }++ let(:valid_values) do+ [custom_field_project1.custom_field_id.to_s, custom_field_project2.custom_field_id.to_s]+ end+ let(:name) { "Available project attributes" }++ describe "#apply_to" do+ let(:values) { valid_values }++ let(:custom_field_project_handwritten_sql_subquery) do+ <<-SQL.squish+ SELECT "custom_fields_projects"."project_id"+ FROM "custom_fields_projects"+ WHERE "custom_fields_projects"."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 (#{custom_field_project_handwritten_sql_subquery})+ SQL++ expect(instance.apply_to(Project).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 (#{custom_field_project_handwritten_sql_subquery})+ SQL++ expect(instance.apply_to(Project).to_sql).to eql handwritten_scope_sql+ end+ end++ context "for an unsupported operator" do+ let(:operator) { "!=" }++ it "raises an error" do+ expect { instance.apply_to(Project) }.to raise_error("unsupported operator")+ end+ end+ end++ describe "#allowed_values" do+ it "is a list of the possible values" do+ expected = [+ [custom_field_project1.custom_field.name, custom_field_project1.custom_field_id],+ [custom_field_project2.custom_field.name, custom_field_project2.custom_field_id]+ ]++ expect(instance.allowed_values).to match_array(expected)+ end+ end+ end+end