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

[#64515] use same query code in filter and select

Tobias Dillmann759ddba73bd7
diff --git a/app/models/queries/project_phase_definitions/database_queries.rb b/app/models/queries/project_phase_definitions/database_queries.rb
new file mode 100644
index 00000000000..89ab8f1bba3
--- /dev/null
+++ b/app/models/queries/project_phase_definitions/database_queries.rb
@@ -0,0 +1,64 @@
+# 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::ProjectPhaseDefinitions
+ module DatabaseQueries
+ # JOIN statement for project phase definitions. We ensure that only active project phases are considered.
+ # Note that one project might have an active phase while another project has set the phase with the same definition
+ # to inactive. Additionally, the permissions to view project phases are considered on a project level, too.
+ def join_project_phase_definitions_based_on_permissions_and_active_phases
+ # The project is joined here anew which should not be necessary for many use-cases but is.
+ # The necessity comes from AR's behavior of automatically determining the alias for tables LEFT JOINed via includes.
+ # To avoid conflicts, AR will search strings for occurrences of the table name and if found, an included table
+ # will be aliased (potentially with a numbering). In this case, if the permission checks are part of the query,
+ # it will include a reference to the projects table. Therefore, the include for projects, which happens in
+ # the query itself, will be considered needing an alias. That assumption is wrong in this case as the reference
+ # to projects is in a subquery but AR does not know that.
+ <<~SQL.squish
+ LEFT OUTER JOIN "projects" ON "projects"."id" = "work_packages"."project_id"
+ LEFT OUTER JOIN (
+ SELECT
+ ph.id,
+ ph.project_id,
+ ph.definition_id AS active_phase_definition_id
+ FROM project_phases ph
+ WHERE ph.project_id IN (#{projects_with_view_phases_permissions.to_sql})
+ AND ph.active = true
+ ) AS active_phases
+ ON active_phases.active_phase_definition_id = work_packages.project_phase_definition_id
+ AND active_phases.project_id = work_packages.project_id
+ SQL
+ end
+
+ def projects_with_view_phases_permissions
+ Project.allowed_to(User.current, :view_project_phases).select(:id)
+ end
+ end
+end
diff --git a/app/models/queries/work_packages/filter/project_phase_filter.rb b/app/models/queries/work_packages/filter/project_phase_filter.rb
index 082d7b1e8a4..5b630cef93d 100644
--- a/app/models/queries/work_packages/filter/project_phase_filter.rb
+++ b/app/models/queries/work_packages/filter/project_phase_filter.rb
@@ -29,6 +29,8 @@
#++
class Queries::WorkPackages::Filter::ProjectPhaseFilter < Queries::WorkPackages::Filter::WorkPackageFilter
+ include Queries::ProjectPhaseDefinitions::DatabaseQueries
+
def allowed_values
@allowed_values ||= project_phase_definitions.map { |s| [s.name, s.id.to_s] }
end
@@ -55,26 +57,8 @@ class Queries::WorkPackages::Filter::ProjectPhaseFilter < Queries::WorkPackages:
values.filter_map { |id| available_definitions[id.to_i] }
end
- # TODO: copied over from ProjectPhaseSelect, refactor to use the same truth
def joins
- <<~SQL.squish
- LEFT OUTER JOIN "projects" ON "projects"."id" = "work_packages"."project_id"
- LEFT OUTER JOIN (
- SELECT
- ph.id,
- ph.project_id,
- ph.definition_id AS active_phase_definition_id
- FROM project_phases ph
- WHERE ph.project_id IN (#{project_with_view_phases_permissions.to_sql})
- AND ph.active = true
- ) AS active_phases
- ON active_phases.active_phase_definition_id = work_packages.project_phase_definition_id
- AND active_phases.project_id = work_packages.project_id
- SQL
- end
-
- def project_with_view_phases_permissions
- Project.allowed_to(User.current, :view_project_phases).select(:id)
+ join_project_phase_definitions_based_on_permissions_and_active_phases
end
def where
diff --git a/app/models/queries/work_packages/selects/project_phase_select.rb b/app/models/queries/work_packages/selects/project_phase_select.rb
index ca996ab16a6..ea2c7946887 100644
--- a/app/models/queries/work_packages/selects/project_phase_select.rb
+++ b/app/models/queries/work_packages/selects/project_phase_select.rb
@@ -29,6 +29,8 @@
#++
class Queries::WorkPackages::Selects::ProjectPhaseSelect < Queries::WorkPackages::Selects::WorkPackageSelect
+ include Queries::ProjectPhaseDefinitions::DatabaseQueries
+
def initialize
super(:project_phase,
association: :project_phase_definition,
@@ -52,30 +54,8 @@ class Queries::WorkPackages::Selects::ProjectPhaseSelect < Queries::WorkPackages
active_phase_null_case(true_case: "1", false_case: "0")
end
- # Is called when the query is grouped by project phase definition. We ensure that only active project phases are considered.
- # Note that one project might have an active phase while another project has set the phase with the same definition to inactive.
- # Additionally, the permissions to view project phases are considered on a project level, too.
def group_by_join_statement
- # The project is joined here anew which should not be necessary but is.
- # The necessity comes from AR's behaviour of automatically determining the alias for tables LEFT JOINed via includes.
- # To avoid conflicts, AR will search strings for occurrences of the table name and if found, an included table will be aliased
- # (potentially with a numbering). In this case, if the permission checks are part of the query, it will include a reference
- # to the projects table. Therefore, the include for projects, which happens in the query itself, will be considered needing
- # an alias. That assumption is wrong in this case as the reference to projects is in a subquery but AR does not know that.
- <<~SQL.squish
- LEFT OUTER JOIN "projects" ON "projects"."id" = "work_packages"."project_id"
- LEFT OUTER JOIN (
- SELECT
- ph.id,
- ph.project_id,
- ph.definition_id AS active_phase_definition_id
- FROM project_phases ph
- WHERE ph.project_id IN (#{project_with_view_phases_permissions.to_sql})
- AND ph.active = true
- ) AS active_phases
- ON active_phases.active_phase_definition_id = work_packages.project_phase_definition_id
- AND active_phases.project_id = work_packages.project_id
- SQL
+ join_project_phase_definitions_based_on_permissions_and_active_phases
end
def sortable_join_statement(_query)