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

Merge branch 'dev' into implementation/67453-enable-the-custom-field-formats-for-project-attribute-usage

Eric Schubertd416a1685ebb
diff --cc app/controllers/admin/custom_fields/hierarchy/items_base_controller.rb
index aed7e2bf356,00000000000..4edbd0d4e2a
mode 100644,000000..100644
--- a/app/controllers/admin/custom_fields/hierarchy/items_base_controller.rb
+++ b/app/controllers/admin/custom_fields/hierarchy/items_base_controller.rb
@@@ -1,231 -1,0 +1,240 @@@
+# 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 Admin
+ module CustomFields
+ module Hierarchy
+ class ItemsBaseController < ApplicationController
+ include OpTurbo::ComponentStream
+ include Dry::Monads[:result]
+
+ layout :admin_or_frame_layout
+ model_object CustomField
+
+ before_action :require_admin, :find_model_object, :find_active_item
+
+ # See https://github.com/hotwired/turbo-rails?tab=readme-ov-file#a-note-on-custom-layouts
+ def admin_or_frame_layout
+ return "turbo_rails/frame" if turbo_frame_request?
+
+ "admin"
+ end
+
+ def index; end
+
+ def show
+ render action: :index
+ end
+
+ def new
+ @new_item = @active_item.children.build(sort_order: params[:position])
+ end
+
+ def edit; end
+
+ def create
+ item_service
+ .insert_item(contract_class: create_contract, **item_input)
+ .either(
+ lambda { |item| redirect_to action: :new, position: item.sort_order + 1 },
+ lambda do |validation_result|
+ add_errors_to_form(validation_result)
+ render :new
+ end
+ )
+ end
+
+ def update
+ input = item_input
+ item_service
+ .update_item(contract_class: update_contract,
+ item: @active_item,
+ label: input[:label],
+ short: input[:short],
+ score: input[:score])
+ .either(
+ ->(*) { redirect_to action: :show, id: @active_item.parent, status: :see_other },
+ lambda do |validation_result|
+ add_errors_to_edit_form(validation_result)
+ update_via_turbo_stream(component: ItemComponent.new(item: @active_item, show_edit_form: true))
+ respond_with_turbo_streams
+ end
+ )
+ end
+
+ def move
+ item_service
+ .reorder_item(item: @active_item, new_sort_order: params.require(:new_sort_order))
+
+ redirect_to action: :show, id: @active_item.parent, status: :see_other
+ end
+
- def change_parent # rubocop:disable Metrics/AbcSize
- input = params.require(:custom_field_hierarchy_forms_new_parent_form_model).require(:new_parent)
-
- parse_parent_input(input).bind do |new_parent|
++ def change_parent
++ result = parse_parent_input(new_parent_params).bind do |new_parent|
+ validate_new_parent(new_parent).bind do
+ item_service.move_item(item: @active_item, new_parent:)
+ end
- end.either(
++ end
++
++ result.either(
+ ->(result) do
+ redirect_to action: :show,
+ id: result.parent,
+ status: :see_other,
+ notice: I18n.t(:notice_successful_update)
+ end,
+ ->(error) do
+ render_error_flash_message_via_turbo_stream(message: error)
+ respond_with_turbo_streams(&:html)
+ end
+ )
+ end
+
+ def destroy
+ item_service
+ .delete_branch(item: @active_item)
+ .either(
+ ->(_) { update_via_turbo_stream(component: ItemsComponent.new(item: @active_item.parent.reload)) },
+ ->(errors) { render_error_flash_message_via_turbo_stream(message: errors.full_messages) }
+ )
+
+ respond_with_turbo_streams(&:html)
+ end
+
+ def deletion_dialog
+ respond_with_dialog DeleteItemDialogComponent.new(custom_field: @custom_field, hierarchy_item: @active_item)
+ end
+
+ def change_parent_dialog
+ respond_with_dialog ChangeItemParentDialogComponent.new(
+ custom_field: @custom_field,
+ hierarchy_item: @active_item
+ )
+ end
+
+ private
+
+ def item_service
+ ::CustomFields::Hierarchy::HierarchicalItemService.new
+ end
+
+ def item_input # rubocop:disable Metrics/AbcSize
+ input = { parent: @active_item, label: params[:label] }
+ input[:short] = params[:short] if params[:short].present?
+ input[:score] = params[:score] if params[:score].present?
+ input[:sort_order] = params[:sort_order].to_i if params[:sort_order].present?
+
+ input
+ end
+
++ def new_parent_params
++ params.require(:custom_field_hierarchy_forms_new_parent_form_model).require(:new_parent)
++ end
++
+ def create_contract
+ case @custom_field.field_format
+ when "hierarchy"
+ ::CustomFields::Hierarchy::InsertListItemContract
+ when "scored_list"
+ ::CustomFields::Hierarchy::InsertScoredItemContract
+ else
+ raise ArgumentError, "unsupported custom field format '#{@custom_field.field_format}'"
+ end
+ end
+
+ def update_contract
+ case @custom_field.field_format
+ when "hierarchy"
+ ::CustomFields::Hierarchy::UpdateListItemContract
+ when "scored_list"
+ ::CustomFields::Hierarchy::UpdateScoredItemContract
+ else
+ raise ArgumentError, "unsupported custom field format '#{@custom_field.field_format}'"
+ end
+ end
+
+ def add_errors_to_form(validation_result)
+ @new_item = ::CustomField::Hierarchy::Item.new(**item_input)
+ validation_result.errors(full: true).to_h.each do |attribute, errors|
+ @new_item.errors.add(attribute, errors.join(", "))
+ end
+ end
+
+ def add_errors_to_edit_form(validation_result)
+ @active_item.assign_attributes(**validation_result.to_h.slice(:label, :short, :score))
+
+ validation_result.errors(full: true).to_h.each do |attribute, errors|
+ @active_item.errors.add(attribute, errors.join(", "))
+ end
+ end
+
+ def parse_parent_input(new_parent_input)
+ case new_parent_input
+ in [new_parent]
+ input = MultiJson.load(new_parent, symbolize_keys: true)[:value]
- new_parent = CustomField::Hierarchy::Item.including_children.find(input)
- Success(new_parent)
++ new_parent = CustomField::Hierarchy::Item.including_children.find_by(id: input)
++
++ if new_parent.present?
++ Success(new_parent)
++ else
++ Failure(I18n.t(:notice_parent_item_not_found))
++ end
+ else
+ Failure("Invalid input: #{new_parent_input}")
+ end
+ end
+
+ def validate_new_parent(new_parent)
- return Failure(I18n.t(:notice_change_parent_same_target)) if @active_item.parent.id == new_parent.id
- return Failure(I18n.t(:notice_change_parent_to_self)) if @active_item.id == new_parent.id
++ return Failure("Parent must not be the same as before.") if @active_item.parent.id == new_parent.id
++ return Failure("Parent must not be the current item.") if @active_item.id == new_parent.id
+
+ Success()
+ end
+
+ def find_model_object
+ @object = find_custom_field
+ @custom_field = @object
+ end
+
+ def find_custom_field
+ raise NotImplementedError, "SubclassResponsibility"
+ end
+
+ def find_active_item
+ @active_item = if params[:id].present?
+ CustomField::Hierarchy::Item.including_children.find(params[:id])
+ else
+ @object.hierarchy_root
+ end
+ end
+ end
+ end
+ end
+end
diff --cc config/locales/en.yml
index c2e5bdd6b82,d8cb22f457f..ccb6f5de25e
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@@ -3779,6 -3777,6 +3777,7 @@@ en
notice_password_confirmation_failed: "Your password is not correct. Cannot continue."
notice_principals_found_multiple: "There are %{number} results found. \n Tab to focus the first result."
notice_principals_found_single: "There is one result. \n Tab to focus it."
++ notice_parent_item_not_found: "Parent item not found."
notice_project_not_deleted: "The project wasn't deleted."
notice_project_not_found: "Project not found."
notice_successful_connection: "Successful connection."