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

activation service that reschedules instead of plain upsert

Ivan Kuchin6ead8b8d144f
diff --git a/app/contracts/project_life_cycle_steps/activation_contract.rb b/app/contracts/project_life_cycle_steps/activation_contract.rb
new file mode 100644
index 00000000000..b120150633a
--- /dev/null
+++ b/app/contracts/project_life_cycle_steps/activation_contract.rb
@@ -0,0 +1,49 @@
+# 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 ProjectLifeCycleSteps
+ class ActivationContract < ::ModelContract
+ alias_method :project, :model
+
+ validate :validate_select_project_phases_permission
+
+ def validate_select_project_phases_permission
+ return if user.allowed_in_project?(:select_project_phases, project)
+
+ errors.add :base, :error_unauthorized
+ end
+
+ protected
+
+ def validate_model?
+ false
+ end
+ end
+end
diff --git a/app/controllers/projects/settings/life_cycle_steps_controller.rb b/app/controllers/projects/settings/life_cycle_steps_controller.rb
index a45d938a19d..263866e74c6 100644
--- a/app/controllers/projects/settings/life_cycle_steps_controller.rb
+++ b/app/controllers/projects/settings/life_cycle_steps_controller.rb
@@ -40,17 +40,17 @@ class Projects::Settings::LifeCycleStepsController < Projects::SettingsControlle
def toggle
definition = Project::PhaseDefinition.where(id: params[:id])
- upsert_steps(definition, active: params["value"])
+ set_steps_active_status(definition, active: params["value"])
end
def disable_all
- upsert_steps(@life_cycle_definitions, active: false)
+ set_steps_active_status(@life_cycle_definitions, active: false)
redirect_to action: :index
end
def enable_all
- upsert_steps(@life_cycle_definitions, active: true)
+ set_steps_active_status(@life_cycle_definitions, active: true)
redirect_to action: :index
end
@@ -65,18 +65,9 @@ class Projects::Settings::LifeCycleStepsController < Projects::SettingsControlle
deny_access(not_found: true) unless OpenProject::FeatureDecisions.stages_and_gates_active?
end
- def upsert_steps(definitions, active:)
- Project::Phase.upsert_all(
- definitions.map do |definition|
- {
- project_id: @project.id,
- definition_id: definition.id,
- active:
- }
- end,
- unique_by: %i[project_id definition_id]
- )
-
- @project.touch_and_save_journals
+ def set_steps_active_status(definitions, active:)
+ ::ProjectLifeCycleSteps::ActivationService
+ .new(user: current_user, project: @project, definitions:)
+ .call(active:)
end
end
diff --git a/app/services/project_life_cycle_steps/activation_service.rb b/app/services/project_life_cycle_steps/activation_service.rb
new file mode 100644
index 00000000000..0426a8709c8
--- /dev/null
+++ b/app/services/project_life_cycle_steps/activation_service.rb
@@ -0,0 +1,78 @@
+# 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 ProjectLifeCycleSteps
+ class ActivationService < ::BaseServices::BaseContracted
+ alias_method :project, :model
+
+ attr_reader :definitions
+
+ def initialize(user:, project:, definitions:, contract_class: nil, contract_options: {})
+ super(user:, contract_class:, contract_options:)
+ self.model = project
+ @definitions = definitions
+ end
+
+ private
+
+ def persist(service_call)
+ active = params[:active]
+
+ upsert(active:)
+
+ first_changed_phase = project.phases.find_by(definition_id: definitions.min_by(&:position)&.id)
+
+ if first_changed_phase
+ UpdateService.new(user:, model: first_changed_phase).call
+ else
+ project.touch_and_save_journals
+
+ service_call
+ end
+ end
+
+ def upsert(active:)
+ Project::Phase.upsert_all(
+ definitions.map do |definition|
+ {
+ project_id: project.id,
+ definition_id: definition.id,
+ active:
+ }
+ end,
+ unique_by: %i[project_id definition_id]
+ )
+ end
+
+ def default_contract_class
+ ProjectLifeCycleSteps::ActivationContract
+ end
+ end
+end