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
Project::LifeCycleStepDefinition.with_project_count
Ivan Kuchin1af2048e38ea
diff --git a/app/models/project/life_cycle_step_definition.rb b/app/models/project/life_cycle_step_definition.rbindex 1f3c8f3b09e..031e72d4ee5 100644--- a/app/models/project/life_cycle_step_definition.rb+++ b/app/models/project/life_cycle_step_definition.rb@@ -27,6 +27,8 @@#++class Project::LifeCycleStepDefinition < ApplicationRecord+ include ::Scopes::Scoped+has_many :life_cycle_steps,class_name: "Project::LifeCycleStep",foreign_key: :definition_id,@@ -45,6 +47,8 @@ class Project::LifeCycleStepDefinition < ApplicationRecorddefault_scope { order(:position) }+ scopes :with_project_count+def step_classraise NotImplementedErrorenddiff --git a/app/models/project/life_cycle_step_definitions/scopes/with_project_count.rb b/app/models/project/life_cycle_step_definitions/scopes/with_project_count.rbnew file mode 100644index 00000000000..53cf99c582d--- /dev/null+++ b/app/models/project/life_cycle_step_definitions/scopes/with_project_count.rb@@ -0,0 +1,48 @@+#-- 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 Project::LifeCycleStepDefinitions::Scopes+ module WithProjectCount+ extend ActiveSupport::Concern++ class_methods do+ def with_project_count+ project_counts = Project::LifeCycleStep+ .where(active: true)+ .group(:definition_id)+ .select(:definition_id, "COUNT(project_id) AS count")++ Project::LifeCycleStepDefinition+ .with(project_counts:)+ .joins("LEFT OUTER JOIN project_counts ON #{quoted_table_name}.id = project_counts.definition_id")+ .select("#{quoted_table_name}.*")+ .select("COALESCE(project_counts.count, 0) AS project_count")+ end+ end+ end+enddiff --git a/spec/models/project/life_cycle_step_definitions/scopes/with_project_count_spec.rb b/spec/models/project/life_cycle_step_definitions/scopes/with_project_count_spec.rbnew file mode 100644index 00000000000..5161d12ded6--- /dev/null+++ b/spec/models/project/life_cycle_step_definitions/scopes/with_project_count_spec.rb@@ -0,0 +1,57 @@+#-- 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 Project::LifeCycleStepDefinitions::Scopes::WithProjectCount do+ let!(:definition_a) { create(:project_stage_definition, name: "foo") }+ let!(:definition_b) { create(:project_gate_definition, name: "bar") }+ let!(:definition_c) { create(:project_stage_definition, name: "baz") }++ before do+ create(:project).tap do |project|+ create(:project_stage, project:, definition: definition_a)+ create(:project_stage, project:, definition: definition_b)+ end++ create(:project).tap do |project|+ create(:project_stage, project:, definition: definition_a)+ create(:project_stage, project:, definition: definition_b, active: false)+ end+ end++ describe ".with_project_count" do+ it "queries project counts alongside definitions" do+ expect(Project::LifeCycleStepDefinition.with_project_count).to contain_exactly(+ having_attributes(id: definition_a.id, name: "foo", project_count: 2),+ having_attributes(id: definition_b.id, name: "bar", project_count: 1),+ having_attributes(id: definition_c.id, name: "baz", project_count: 0)+ )+ end+ end+end