diff --git a/app/components/work_packages/types/settings_component.html.erb b/app/components/work_packages/types/settings_component.html.erb new file mode 100644 index 000000000000..f9c74101ee83 --- /dev/null +++ b/app/components/work_packages/types/settings_component.html.erb @@ -0,0 +1,34 @@ +<%#-- 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. + +++#%> + +<%= + primer_form_with(**form_options) do |f| + render(WorkPackages::Types::SettingsForm.new(f)) + end +%> diff --git a/app/components/work_packages/types/settings_component.rb b/app/components/work_packages/types/settings_component.rb new file mode 100644 index 000000000000..948af9a38df4 --- /dev/null +++ b/app/components/work_packages/types/settings_component.rb @@ -0,0 +1,63 @@ +# 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 WorkPackages + module Types + class SettingsComponent < ApplicationComponent + include OpPrimer::ComponentHelpers + include OpTurbo::Streamable + + def initialize(model, copy_workflow_from: nil, **) + @copy_workflow_from = copy_workflow_from + super(model, **) + end + + def form_options + if model.new_record? + create_form_options + else + update_form_options + end + end + + private + + attr_reader :copy_workflow_from + + def create_form_options + { url: types_path, method: :post, model:, copy_workflow_from: } + end + + def update_form_options + { url: type_path(id: model.id), method: :patch, model: } + end + end + end +end diff --git a/app/controllers/types_controller.rb b/app/controllers/types_controller.rb index 3c2bf3b7293e..57f61b721967 100644 --- a/app/controllers/types_controller.rb +++ b/app/controllers/types_controller.rb @@ -62,19 +62,17 @@ def edit end end - def create # rubocop:disable Metrics/AbcSize + def create CreateTypeService .new(current_user) - .call(permitted_type_params, copy_workflow_from: params[:copy_workflow_from]) do |call| + .call(permitted_type_params, copy_workflow_from: params.dig(:type, :copy_workflow_from)) do |call| @type = call.result call.on_success do redirect_to_type_tab_path(@type, t(:notice_successful_create)) end - call.on_failure do |result| - flash[:error] = result.errors.full_messages.join("\n") - load_projects_and_types + call.on_failure do render action: :new, status: :unprocessable_entity end end @@ -137,7 +135,7 @@ def load_projects_and_types def redirect_to_type_tab_path(type, notice) tab = params["tab"] || "settings" - redirect_to(edit_tab_type_path(type, tab:), notice:) + redirect_to(edit_tab_type_path(type, tab:), notice:, status: :see_other) end def render_edit_tab(type, status: :ok) diff --git a/app/forms/work_packages/types/settings_form.rb b/app/forms/work_packages/types/settings_form.rb new file mode 100644 index 000000000000..9b50d854f69d --- /dev/null +++ b/app/forms/work_packages/types/settings_form.rb @@ -0,0 +1,116 @@ +# 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 WorkPackages + module Types + class SettingsForm < ApplicationForm + form do |settings_form| + settings_form.text_field( + name: :name, + label: label(:name), + placeholder: I18n.t(:label_name), + input_width: :large, + required: true, + disabled: model.is_standard? + ) + + settings_form.color_select_list( + name: :color_id, + label: Color.model_name.human, + caption: I18n.t("types.edit.settings.type_color_text"), + input_width: :large + ) + + if show_work_flow_copy? + settings_form.select_list( + name: :copy_workflow_from, + label: label(:copy_workflow_from), + include_blank: true, + input_width: :large + ) do |other_types| + work_package_types.each do |type| + other_types.option( + value: type.id, + label: type.name, + selected: type.id == prefilled_copy_workflow_from + ) + end + end + end + + settings_form.rich_text_area( + name: :description, + label: label(:description), + input_width: :large, + rich_text_options: { showAttachments: false } + ) + + settings_form.check_box( + name: :is_milestone, + label: label(:is_milestone) + ) + + settings_form.check_box( + name: :is_in_roadmap, + label: label(:is_in_roadmap) + ) + + settings_form.check_box( + name: :is_default, + label: label(:is_default) + ) + + settings_form.submit( + name: :submit, + label: I18n.t(:button_save), + scheme: :primary + ) + end + + private + + def label(attribute) + model.class.human_attribute_name(attribute) + end + + def show_work_flow_copy? + model.new_record? + end + + def work_package_types + Type.all + end + + def prefilled_copy_workflow_from + @builder.options[:copy_workflow_from] + end + end + end +end diff --git a/app/helpers/conversion_helper.rb b/app/helpers/conversion_helper.rb new file mode 100644 index 000000000000..e4ed37cee0e4 --- /dev/null +++ b/app/helpers/conversion_helper.rb @@ -0,0 +1,39 @@ +# 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 ConversionHelper + include Dry::Monads[:maybe] + + def maybe_integer(value) + Some(Integer(value || "", 10)) + rescue ArgumentError + None() + end +end diff --git a/app/helpers/types_helper.rb b/app/helpers/types_helper.rb index 934ce2ed2e7b..2e9c38b7012a 100644 --- a/app/helpers/types_helper.rb +++ b/app/helpers/types_helper.rb @@ -34,9 +34,9 @@ def types_tabs tabs = [ { name: "settings", - partial: "types/form/settings", path: edit_tab_type_path(id: @type.id, tab: :settings), - label: "types.edit.settings.tab" + label: "types.edit.settings.tab", + view_component: WorkPackages::Types::SettingsComponent }, { name: "form_configuration", diff --git a/app/views/types/form/_settings.html.erb b/app/views/types/form/_settings.html.erb deleted file mode 100644 index 13ba08e6a564..000000000000 --- a/app/views/types/form/_settings.html.erb +++ /dev/null @@ -1,75 +0,0 @@ -<%#-- 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. - -++#%> - -