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
[#62979] filter by phase definitions
Tobias Dillmann425294c14554
diff --git a/app/models/queries/project_phase_definitions/phase_definition_query.rb b/app/models/queries/project_phase_definitions/phase_definition_query.rbnew file mode 100644index 00000000000..6ead34da436--- /dev/null+++ b/app/models/queries/project_phase_definitions/phase_definition_query.rb@@ -0,0 +1,51 @@+# 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.+#++++module Queries::Project+ module PhaseDefinitions+ class PhaseDefinitionQuery+ include Queries::BaseQuery+ include Queries::UnpersistedQuery++ def self.model+ Project::PhaseDefinition+ end++ # def results+ # super+ # # .includes(:roles, { principal: :preference }, :member_roles)+ # end++ def default_scope+ Project::PhaseDefinition.order(:position)+ end+ end+ end+enddiff --git a/app/models/queries/project_phases/phase_query.rb b/app/models/queries/project_phases/phase_query.rbnew file mode 100644index 00000000000..9e7a6e04641--- /dev/null+++ b/app/models/queries/project_phases/phase_query.rb@@ -0,0 +1,51 @@+# 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.+#++++module Queries::Project+ module Phases+ class PhaseQuery+ include Queries::BaseQuery+ include Queries::UnpersistedQuery++ def self.model+ Project::Phase+ end++ # def results+ # super+ # # .includes(:roles, { principal: :preference }, :member_roles)+ # end++ def default_scope+ Project::Phase.visible(User.current)+ end+ end+ end+enddiff --git a/app/models/queries/work_packages.rb b/app/models/queries/work_packages.rbindex e880e9aa8e2..64af5d66811 100644--- a/app/models/queries/work_packages.rb+++ b/app/models/queries/work_packages.rb@@ -28,6 +28,9 @@# See COPYRIGHT and LICENSE files for more details.#+++# require_relative "../../../app/models/queries/project_phases/phase_query"+require_relative "../../../app/models/queries/project_phase_definitions/phase_definition_query"+module Queries::WorkPackages::Queries::Register.register(Query) dofilter Filter::AncestorFilter@@ -62,6 +65,7 @@ module Queries::WorkPackagesfilter Filter::DatesIntervalFilterfilter Filter::ParentFilterfilter Filter::PrecedesFilter+ filter Filter::ProjectPhaseFilterfilter Filter::FollowsFilterfilter Filter::RelatesFilterfilter Filter::DuplicatesFilter@@ -92,4 +96,13 @@ module Queries::WorkPackagesselect Selects::ManualSortingSelectselect Selects::TypeaheadSelectend++ # Queries::Register.register(Queries::Project::Phases::PhaseQuery) do+ # filter Filter::ProjectPhaseFilter+ # end++ #::Queries::Project::PhaseDefinitions::PhaseDefinitionQuery or ::PhaseDefinitionQuery+ Queries::Register.register(Queries::Project::PhaseDefinitions::PhaseDefinitionQuery) do+ filter Filter::ProjectPhaseFilter+ endenddiff --git a/app/models/queries/work_packages/filter/project_phase_filter.rb b/app/models/queries/work_packages/filter/project_phase_filter.rbnew file mode 100644index 00000000000..6f6d6c9a295--- /dev/null+++ b/app/models/queries/work_packages/filter/project_phase_filter.rb@@ -0,0 +1,84 @@+# 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::WorkPackages::Filter::ProjectPhaseFilter < Queries::WorkPackages::Filter::WorkPackageFilter+ def allowed_values+ @allowed_values ||= project_phase_definitions.map { |s| [s.name, s.id.to_s] }+ end++ def available?+ project_phase_definitions.any?+ end++ def type+ :list+ end++ def self.key+ :project_phase_definition_id+ end++ def ar_object_filter?+ true+ end++ def value_objects+ available_definitions = project_phase_definitions.index_by(&:id)++ values.filter_map { |id| available_definitions[id.to_i] }+ end++ def where+ if operator_strategy.to_sym == :"="+ "active_phases.active_phase_definition_id IS NOT NULL"+ elsif operator_strategy.to_sym == :!+ Arel.sql(+ <<~SQL.squish+ active_phases.active_phase_definition_id IS NULL OR+ active_phases.active_phase_definition_id NOT IN (#{values.join(',')})+ SQL+ )+ else+ ""+ end+ end++ private++ def project_phase_feature_flag_enabled?+ OpenProject::FeatureDecisions.stages_and_gates_active?+ end++ def project_phase_definitions+ return Project::PhaseDefinition.none unless project_phase_feature_flag_enabled?++ Project::PhaseDefinition.order(Arel.sql("position"))+ end+enddiff --git a/app/services/params_to_query_service.rb b/app/services/params_to_query_service.rbindex 98fcd811df7..61bd409769f 100644--- a/app/services/params_to_query_service.rb+++ b/app/services/params_to_query_service.rb@@ -123,6 +123,8 @@ class ParamsToQueryServiceelsemodel_name = model.name+ puts "FOOBAR searching for query class: ::Queries::#{model_name.pluralize}::#{model_name.demodulize}Query or ::#{model_name.demodulize}Query"+# Some queries exist as Queries::Models::ModelQuery others as ModelQuery"::Queries::#{model_name.pluralize}::#{model_name.demodulize}Query".safe_constantize ||"::#{model_name.demodulize}Query".constantizediff --git a/lib/api/v3/project_phase_definitions/project_phase_definitions_api.rb b/lib/api/v3/project_phase_definitions/project_phase_definitions_api.rbindex 2193579c84c..f5ebdd8511a 100644--- a/lib/api/v3/project_phase_definitions/project_phase_definitions_api.rb+++ b/lib/api/v3/project_phase_definitions/project_phase_definitions_api.rb@@ -37,6 +37,12 @@ module APIauthorize_in_any_project(%i[view_project_phases select_project_phases])end+ get &::API::V3::Utilities::Endpoints::Index+ .new(model: Project::PhaseDefinition,+ scope: -> { Project::PhaseDefinition.order(:position) },+ api_name: "ProjectPhaseDefinitions")+ .mount+route_param :id doafter_validation do@phase_definition = ::Project::PhaseDefinition.find(params[:id])diff --git a/lib/api/v3/project_phase_definitions/project_phase_definitions_collection_representer.rb b/lib/api/v3/project_phase_definitions/project_phase_definitions_collection_representer.rbnew file mode 100644index 00000000000..939536c93ab--- /dev/null+++ b/lib/api/v3/project_phase_definitions/project_phase_definitions_collection_representer.rb@@ -0,0 +1,38 @@+# 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.+#++++module API+ module V3+ module ProjectPhaseDefinitions+ class ProjectPhaseDefinitionsCollectionRepresenter < ::API::Decorators::OffsetPaginatedCollection+ end+ end+ end+enddiff --git a/lib/api/v3/project_phase_definitions/project_phase_definitions_representer.rb b/lib/api/v3/project_phase_definitions/project_phase_definitions_representer.rbnew file mode 100644index 00000000000..a3d279a000b--- /dev/null+++ b/lib/api/v3/project_phase_definitions/project_phase_definitions_representer.rb@@ -0,0 +1,38 @@+# 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.+# ++++module API+ module V3+ module ProjectPhaseDefinitions+ class ProjectPhaseDefinitionsRepresenter < ProjectPhaseDefinitionRepresenter+ end+ end+ end+enddiff --git a/lib/api/v3/project_phases/project_phase_representer.rb b/lib/api/v3/project_phases/project_phase_representer.rbindex 9afa84905bd..b4093fe5aba 100644--- a/lib/api/v3/project_phases/project_phase_representer.rb+++ b/lib/api/v3/project_phases/project_phase_representer.rb@@ -61,6 +61,9 @@ module API"ProjectPhase"endend++ class ProjectPhasesRepresenter < ProjectPhaseRepresenter+ endendendenddiff --git a/lib/api/v3/project_phases/project_phases_api.rb b/lib/api/v3/project_phases/project_phases_api.rbindex 4e26824a867..19de601c232 100644--- a/lib/api/v3/project_phases/project_phases_api.rb+++ b/lib/api/v3/project_phases/project_phases_api.rb@@ -33,13 +33,20 @@ module APImodule ProjectPhasesclass ProjectPhasesAPI < ::API::OpenProjectAPIresources :project_phases do+ get &::API::V3::Utilities::Endpoints::Index+ .new(model: Project::Phase,+ # TODO: add visible-scope here:+ scope: -> { Project::Phase.where(active: true) },+ api_name: "ProjectPhases")+ .mount+route_param :id doafter_validation do@phase = ::Project::Phase.visible(current_user).find(params[:id])endget &::API::V3::Utilities::Endpoints::Show- .new(model: ::Project::Phase,+ .new(model: Project::Phase,render_representer: API::V3::ProjectPhases::ProjectPhaseRepresenter).mountenddiff --git a/lib/api/v3/project_phases/project_phases_collection_representer.rb b/lib/api/v3/project_phases/project_phases_collection_representer.rbnew file mode 100644index 00000000000..0f37daa4813--- /dev/null+++ b/lib/api/v3/project_phases/project_phases_collection_representer.rb@@ -0,0 +1,38 @@+# 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.+#++++module API+ module V3+ module ProjectPhases+ class ProjectPhasesCollectionRepresenter < ::API::Decorators::OffsetPaginatedCollection+ end+ end+ end+enddiff --git a/lib/api/v3/projects/projects_api.rb b/lib/api/v3/projects/projects_api.rbindex 39cb18af7f3..67e11d525d3 100644--- a/lib/api/v3/projects/projects_api.rb+++ b/lib/api/v3/projects/projects_api.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbHdiff --git a/lib/api/v3/queries/schemas/project_phase_filter_dependency_representer.rb b/lib/api/v3/queries/schemas/project_phase_filter_dependency_representer.rbnew file mode 100644index 00000000000..7f249b8b864--- /dev/null+++ b/lib/api/v3/queries/schemas/project_phase_filter_dependency_representer.rb@@ -0,0 +1,68 @@+# 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.+#++++module API+ module V3+ module Queries+ module Schemas+ class ProjectPhaseFilterDependencyRepresenter < FilterDependencyRepresenter+ def json_cache_key+ if filter.project+ super + [filter.project.id]+ else+ super+ end+ end++ def href_callback+ params = CGI.escape(::JSON.dump(filter_query))++ # if filter.project.present?+ # "#{api_v3_paths.project_phases_by_project(filter.project.identifier)}?filters=#{params}"+ # else+ "#{api_v3_paths.project_phase_definitions}?filters=#{params}"+ # end+ end++ # FIXME only request the project phases that are actually used in the query!+ # alternatively, fix the API query to return only active and visible project phase definitions+ def filter_query+ # [{ project_phase: { operator: "*", values: [] } }]+ []+ end++ def type+ "[]ProjectPhaseDefinition"+ end+ end+ end+ end+ end+enddiff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rbindex dc18eb9c9a6..0bc5ca26eba 100644--- a/lib/api/v3/utilities/path_helper.rb+++ b/lib/api/v3/utilities/path_helper.rb@@ -347,9 +347,21 @@ module APIresources :project+ index :project_phaseshow :project_phase++ def self.project_phases_by_project(project_id)+ "#{project(project_id)}/project_phases"+ end++ index :project_phase_definitionshow :project_phase_definition+ # alias used by autogenerated query filter instance representer code+ def self.phase_definition(id)+ "#{project_phase_definitions}/#{id}"+ end+show :project_statusdef self.projects_available_parentsdiff --git a/spec/models/queries/work_packages/filter/project_phase_filter_spec.rb b/spec/models/queries/work_packages/filter/project_phase_filter_spec.rbnew file mode 100644index 00000000000..064a3d8e2a3--- /dev/null+++ b/spec/models/queries/work_packages/filter/project_phase_filter_spec.rb@@ -0,0 +1,141 @@+# 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.+#++++require "spec_helper"++RSpec.describe Queries::WorkPackages::Filter::ProjectPhaseFilter do+ it_behaves_like "basic query filter" do+ let(:type) { :list }+ let(:class_key) { :project_phase_definition_id }++ context "with feature flag being active", with_flag: { stages_and_gates: true } do+ describe "#available?" do+ context "with existing phases" do+ before do+ create(:project_phase_definition)+ end++ it "is true" do+ expect(instance).to be_available+ end+ end++ context "without existing phases" do+ it "is false" do+ expect(instance).not_to be_available+ end+ end+ end++ describe "#allowed_values" do+ context "with existing phases" do+ let!(:definition) { create(:project_phase_definition) }+ let!(:other_definition) { create(:project_phase_definition) }+ let!(:first_definition) { create(:project_phase_definition, position: 0) }++ it "returns an array of phase definition options, ordered by position" do+ expect(instance.allowed_values)+ .to contain_exactly([first_definition.name, first_definition.id.to_s],+ [definition.name, definition.id.to_s],+ [other_definition.name, other_definition.id.to_s])+ end+ end++ context "without existing phases" do+ it "returns an empty array" do+ expect(instance.allowed_values)+ .to be_empty+ end+ end+ end++ describe "#value_objects" do+ let(:definition1) { create(:project_phase_definition) }+ let(:definition2) { create(:project_phase_definition) }++ before do+ instance.values = [definition1.id.to_s, definition2.id.to_s]+ end++ it "returns an array of types" do+ expect(instance.value_objects)+ .to contain_exactly(definition1, definition2)+ end+ end+ end++ context "with feature flag being inactive" do+ context "with existing phases" do+ let!(:definition) { create(:project_phase_definition) }+ let!(:other_definition) { create(:project_phase_definition) }+ let!(:first_definition) { create(:project_phase_definition, position: 0) }++ before do+ create(:project_phase_definition)+ end++ describe "#available?" do+ it "is false" do+ expect(instance).not_to be_available+ end+ end++ describe "#allowed_values" do+ it "returns an empty array" do+ expect(instance.allowed_values)+ .to be_empty+ end+ end+ end++ context "without existing phases" do+ describe "#available?" do+ it "is false" do+ expect(instance).not_to be_available+ end+ end++ describe "#allowed_values" do+ it "returns an empty array" do+ expect(instance.allowed_values)+ .to be_empty+ end+ end+ end+ end++ describe "#ar_object_filter?" do+ it "is true" do+ expect(instance)+ .to be_ar_object_filter+ end+ end+ end+end