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

Make form validations work for nested project life cycle attributes.

Dombi Attilabf658585a1e6
diff --git a/app/contracts/project_life_cycle_steps/base_contract.rb b/app/contracts/project_life_cycle_steps/base_contract.rb
index db94880c137..dfd78adcf19 100644
--- a/app/contracts/project_life_cycle_steps/base_contract.rb
+++ b/app/contracts/project_life_cycle_steps/base_contract.rb
@@ -28,13 +28,10 @@
module ProjectLifeCycleSteps
class BaseContract < ::ModelContract
- attribute :project_id
- attribute :date
-
validate :select_custom_fields_permission
def select_custom_fields_permission
- return if user.allowed_in_project?(:edit_project_stages_and_gates, model.project)
+ return if user.allowed_in_project?(:edit_project_stages_and_gates, model)
errors.add :base, :error_unauthorized
end
diff --git a/app/forms/projects/life_cycles/form.rb b/app/forms/projects/life_cycles/form.rb
index 2df57d431c4..41dd6331128 100644
--- a/app/forms/projects/life_cycles/form.rb
+++ b/app/forms/projects/life_cycles/form.rb
@@ -48,10 +48,6 @@ module Projects::LifeCycles
model.errors.any?
end
- def validation_message
- model.errors.full_messages.join(", ") if invalid?
- end
-
def qa_field_name
"life-cycle-step-#{model.id}"
end
@@ -62,7 +58,6 @@ module Projects::LifeCycles
leading_visual: { icon: :calendar },
required: true,
invalid: invalid?,
- validation_message:,
datepicker_options: { inDialog: true },
wrapper_data_attributes: {
"qa-field-name": qa_field_name
diff --git a/app/models/permitted_params.rb b/app/models/permitted_params.rb
index ba22823578b..7a7b1b1f5f9 100644
--- a/app/models/permitted_params.rb
+++ b/app/models/permitted_params.rb
@@ -290,6 +290,12 @@ class PermittedParams
whitelist.merge(custom_field_values(:project))
end
+ def project_life_cycles
+ params.require(:project).permit(
+ available_life_cycle_steps_attributes: %i[id date start_date end_date]
+ )
+ end
+
def project_custom_field_project_mapping
params.require(:project_custom_field_project_mapping)
.permit(*self.class.permitted_attributes[:project_custom_field_project_mapping])
diff --git a/app/models/project.rb b/app/models/project.rb
index 78ec43158e8..6226b48729c 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -87,8 +87,12 @@ class Project < ApplicationRecord
has_many :project_storages, dependent: :destroy, class_name: "Storages::ProjectStorage"
has_many :storages, through: :project_storages
has_many :life_cycle_steps, class_name: "Project::LifeCycleStep", dependent: :destroy
-
- accepts_nested_attributes_for :life_cycle_steps
+ has_many :available_life_cycle_steps,
+ -> { active.eager_load(:definition).order(position: :asc) },
+ class_name: "Project::LifeCycleStep",
+ inverse_of: :project,
+ dependent: :destroy
+ accepts_nested_attributes_for :available_life_cycle_steps
store_attribute :settings, :deactivate_work_package_attachments, :boolean
diff --git a/app/services/project_life_cycle_steps/set_attributes_service.rb b/app/services/project_life_cycle_steps/set_attributes_service.rb
new file mode 100644
index 00000000000..c1603d42603
--- /dev/null
+++ b/app/services/project_life_cycle_steps/set_attributes_service.rb
@@ -0,0 +1,32 @@
+#-- 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 SetAttributesService < ::BaseServices::SetAttributes
+ end
+end
diff --git a/app/services/project_life_cycle_steps/update_service.rb b/app/services/project_life_cycle_steps/update_service.rb
new file mode 100644
index 00000000000..a8a5a33aa92
--- /dev/null
+++ b/app/services/project_life_cycle_steps/update_service.rb
@@ -0,0 +1,32 @@
+#-- 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 UpdateService < ::BaseServices::Update
+ end
+end
diff --git a/modules/overviews/app/components/project_life_cycles/sections/edit_component.html.erb b/modules/overviews/app/components/project_life_cycles/sections/edit_component.html.erb
index df496c4cb8f..e5025709bae 100644
--- a/modules/overviews/app/components/project_life_cycles/sections/edit_component.html.erb
+++ b/modules/overviews/app/components/project_life_cycles/sections/edit_component.html.erb
@@ -9,9 +9,8 @@
url: update_project_life_cycles_path(project_id: model.id),
) do |f|
render(Primer::Forms::SpacingWrapper.new) do
- f.fields_for(:life_cycle_steps, life_cycle_steps) do |life_cycle_form|
- render(Projects::LifeCycles::Form.new(life_cycle_form)
- )
+ f.fields_for(:available_life_cycle_steps) do |life_cycle_form|
+ render(Projects::LifeCycles::Form.new(life_cycle_form))
end
end
end
diff --git a/modules/overviews/app/controllers/overviews/overviews_controller.rb b/modules/overviews/app/controllers/overviews/overviews_controller.rb
index c45c1390160..946b89fbdf2 100644
--- a/modules/overviews/app/controllers/overviews/overviews_controller.rb
+++ b/modules/overviews/app/controllers/overviews/overviews_controller.rb
@@ -55,7 +55,6 @@ module ::Overviews
def update_project_custom_values
section = find_project_custom_field_section
-
service_call = ::Projects::UpdateService
.new(
user: current_user,
@@ -87,7 +86,21 @@ module ::Overviews
)
end
- def update_project_life_cycles; end
+ def update_project_life_cycles
+ service_call = ::ProjectLifeCycleSteps::UpdateService
+ .new(user: current_user, model: @project)
+ .call(permitted_params.project_life_cycles)
+
+ if service_call.success?
+ update_sidebar_component
+ else
+ update_via_turbo_stream(
+ component: ::ProjectLifeCycles::Sections::EditComponent.new(service_call.result)
+ )
+ end
+
+ respond_to_with_turbo_streams(status: service_call.success? ? :ok : :unprocessable_entity)
+ end
def jump_to_project_menu_item
if params[:jump]