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
[#64619] Add form configuration tab for work package types (#19319)
Yauheni Suhakou893e0210f8ec
diff --git a/app/controllers/work_package_types/form_configuration_tab_controller.rb b/app/controllers/work_package_types/form_configuration_tab_controller.rbnew file mode 100644index 00000000000..f5c4a2d655f--- /dev/null+++ b/app/controllers/work_package_types/form_configuration_tab_controller.rb@@ -0,0 +1,77 @@+# 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 WorkPackageTypes+ class FormConfigurationTabController < ApplicationController+ include PaginationHelper++ layout "admin"++ before_action :require_admin+ before_action :find_type+ before_action :assign_tab, only: :update++ current_menu_item [:edit, :update] do+ :types+ end++ def edit; end++ def update+ result = WorkPackageTypes::UpdateService+ .new(user: current_user, model: @type, contract_class: UpdateFormConfigurationContract)+ .call(permitted_type_params)++ if result.success?+ redirect_to edit_type_form_configuration_path(@type), notice: t(:notice_successful_update)+ else+ flash.now[:error] = result.errors[:attribute_groups].to_sentence+ render :edit, status: :unprocessable_entity+ end+ end++ private++ def find_type+ @type = ::Type.includes(:projects, :custom_fields).find(params[:type_id])+ show_error_not_found unless @type+ end++ def assign_tab+ params[:tab] = "form_configuration" unless params[:tab]+ end++ def permitted_type_params+ # having to call #to_unsafe_h as a query hash the attribute_groups+ # parameters would otherwise still be an ActiveSupport::Parameter+ permitted_params.type.to_unsafe_h+ end+ end+enddiff --git a/app/controllers/work_package_types/projects_tab_controller.rb b/app/controllers/work_package_types/projects_tab_controller.rbindex 510badf78e6..21b4f2715a7 100644--- a/app/controllers/work_package_types/projects_tab_controller.rb+++ b/app/controllers/work_package_types/projects_tab_controller.rb@@ -36,7 +36,7 @@ module WorkPackageTypesbefore_action :find_typebefore_action :load_projects, only: :edit- current_menu_item [:edit] do+ current_menu_item [:edit, :update] do:typesend@@ -49,6 +49,7 @@ module WorkPackageTypesif result.success?redirect_to edit_type_projects_path(type_id: @type.id), notice: I18n.t(:notice_successful_update)else+ params[:tab] = "projects"flash_error(result)load_projectsrender :edit, status: :unprocessable_entitydiff --git a/app/helpers/types_helper.rb b/app/helpers/types_helper.rbindex e8d8ad85e0b..d815e34d23d 100644--- a/app/helpers/types_helper.rb+++ b/app/helpers/types_helper.rb@@ -29,7 +29,7 @@#++module ::TypesHelper- # rubocop:disable Rails/HelperInstanceVariable, Metrics/AbcSize+ # rubocop:disable Rails/HelperInstanceVariabledef types_tabs[{@@ -39,8 +39,7 @@ module ::TypesHelper},{name: "form_configuration",- partial: "types/form/form_configuration",- path: edit_tab_type_path(id: @type.id, tab: :form_configuration),+ path: edit_type_form_configuration_path(@type),label: I18n.t("types.edit.form_configuration.tab")},{@@ -64,7 +63,7 @@ module ::TypesHelper]end- # rubocop:enable Rails/HelperInstanceVariable, Metrics/AbcSize+ # rubocop:enable Rails/HelperInstanceVariabledef icon_for_type(type)return unless typediff --git a/app/services/work_package_types/attribute_groups/transformer.rb b/app/services/work_package_types/attribute_groups/transformer.rbindex 1c83c3ae1d1..bb5490f8e5a 100644--- a/app/services/work_package_types/attribute_groups/transformer.rb+++ b/app/services/work_package_types/attribute_groups/transformer.rb@@ -40,7 +40,7 @@ module WorkPackageTypesreturn [] if groups.blank?groups.map do |group|- if group["type"] == "query"+ if group[:type] == "query"transform_query_group(group)elsetransform_attribute_group(group)@@ -53,15 +53,15 @@ module WorkPackageTypesattr_reader :groups, :userdef transform_attribute_group(group)- name = group["key"]&.to_sym || group["name"]- attributes = group["attributes"].pluck("key")+ name = group[:key]&.to_sym || group[:name]+ attributes = group[:attributes].pluck(:key)[name, attributes]enddef transform_query_group(group)- name = group["name"]- props = JSON.parse(group["query"])+ name = group[:name]+ props = JSON.parse(group[:query])query = Query.new_default(name: "Embedded table: #{name}")query.extend(OpenProject::ChangedBySystem)diff --git a/app/services/work_package_types/update_service.rb b/app/services/work_package_types/update_service.rbindex 0fb62ac92a4..de92d113f16 100644--- a/app/services/work_package_types/update_service.rb+++ b/app/services/work_package_types/update_service.rb@@ -34,8 +34,54 @@ module WorkPackageTypesdef instance_class = Type+ def validate_params+ # Only set attribute groups when it exists (Regression #28400)+ set_attribute_groups(params) if params[:attribute_groups]+ set_active_custom_fields+ set_active_custom_fields_for_project_ids(params[:project_ids]) if params[:project_ids].present?++ super+ end+privatedef default_contract_class = UpdateSettingsContract++ def set_attribute_groups(params)+ if params[:attribute_groups].empty?+ model.reset_attribute_groups+ else+ model.attribute_groups = AttributeGroups::Transformer.new(groups: params[:attribute_groups], user: user).call+ params.delete(:attribute_groups)+ end+ end++ ##+ # Syncs attribute group settings for custom fields with enabled custom fields+ # for this type. If a custom field is not in a group, it is removed from the+ # custom_field_ids list.+ def set_active_custom_fields+ model.custom_field_ids = model.attribute_groups+ .flat_map(&:members)+ .filter_map do |attr|+ if CustomField.custom_field_attribute?(attr)+ attr.delete_prefix("custom_field_").to_i+ end+ end.uniq+ end++ def set_active_custom_fields_for_project_ids(project_ids)+ new_project_ids_to_activate_cfs = project_ids.reject(&:empty?).map(&:to_i) - model.project_ids++ values = Project+ .where(id: new_project_ids_to_activate_cfs)+ .to_a+ .product(model.custom_field_ids)+ .map { |p, cf_ids| { project_id: p.id, custom_field_id: cf_ids } }++ return if values.empty?++ CustomFieldsProject.insert_all(values)+ endendenddiff --git a/app/views/types/form/_form_configuration.html.erb b/app/views/work_package_types/form_configuration_tab/_form.html.erbsimilarity index 96%rename from app/views/types/form/_form_configuration.html.erbrename to app/views/work_package_types/form_configuration_tab/_form.html.erbindex b60cca58a1e..1ddad734b55 100644--- a/app/views/types/form/_form_configuration.html.erb+++ b/app/views/work_package_types/form_configuration_tab/_form.html.erb@@ -33,9 +33,7 @@ See COPYRIGHT and LICENSE files for more details.<div><div class="grid-block -visible-overflow wrap"><div class="grid-content -visible-overflow small-12 large-10">- <%=- render(EnterpriseEdition::BannerComponent.new(:edit_attribute_groups, mb: 3))- %>+ <%= render(EnterpriseEdition::BannerComponent.new(:edit_attribute_groups, mb: 3)) %><% unless EnterpriseToken.allows_to?(:edit_attribute_groups) %><%= angular_component_tag "opce-no-results",diff --git a/app/views/work_package_types/form_configuration_tab/edit.html.erb b/app/views/work_package_types/form_configuration_tab/edit.html.erbnew file mode 100644index 00000000000..d523df84aff--- /dev/null+++ b/app/views/work_package_types/form_configuration_tab/edit.html.erb@@ -0,0 +1,43 @@+<%#-- 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.++++#%>++<% html_title t(:label_administration), "#{t(:label_edit)} #{t(:label_work_package_types)} #{h @type.name}" %>++<%= render ::Types::EditPageHeaderComponent.new(type: @type, tabs: types_tabs) %>++<%=+ primer_form_with(+ model: @type,+ url: type_form_configuration_path(@type),+ builder: TabularFormBuilder,+ lang: current_language+ ) do |f|+ render partial: "form", locals: { f: f }+ end+%>diff --git a/config/locales/en.yml b/config/locales/en.ymlindex beafdce2656..87307a8bb9a 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -1148,7 +1148,7 @@ en:ongoing: "Ongoing"type:description: "Default text for description"- attribute_groups: ""+ attribute_groups: "Form configuration"is_in_roadmap: "Displayed in roadmap by default"is_default: "Activated for new projects by default"is_milestone: "Is milestone"diff --git a/config/routes.rb b/config/routes.rbindex 50bff9caff9..e6a71b56c24 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -138,6 +138,7 @@ Rails.application.routes.draw doget "/roles/workflow/:id/:role_id/:type_id" => "roles#workflow"resources :types do+ resource :form_configuration, only: %i[edit update], controller: "work_package_types/form_configuration_tab"resource :projects, controller: "work_package_types/projects_tab", only: %i[update edit]resource :settings, controller: "work_package_types/settings_tab", only: %i[update edit]diff --git a/spec/controllers/work_package_types/form_configuration_tab_controller_spec.rb b/spec/controllers/work_package_types/form_configuration_tab_controller_spec.rbnew file mode 100644index 00000000000..54e2dc519d7--- /dev/null+++ b/spec/controllers/work_package_types/form_configuration_tab_controller_spec.rb@@ -0,0 +1,128 @@+# 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.+#++++require "spec_helper"++RSpec.describe WorkPackageTypes::FormConfigurationTabController do+ let(:type) { create(:type) }+ let(:user) { create(:admin) }++ before do+ allow(User).to receive(:current).and_return(user)+ end++ describe "GET #edit" do+ context "with an unauthorized account" do+ let(:user) { create(:user) }++ before { get "edit", params: { type_id: type.id } }++ it { expect(response).to have_http_status(:forbidden) }+ end++ context "with invalid type id" do+ it "renders a 404" do+ get :edit, params: { type_id: "invalid" }+ expect(response).to have_http_status(:not_found)+ end+ end++ it "renders the edit tab" do+ get :edit, params: { type_id: type.id }+ expect(response).to render_template(:edit)+ end+ end++ describe "POST #update" do+ let(:params) do+ {+ type_id: type.id,+ type: {+ attribute_groups: [+ {+ type: "attribute",+ name: "People",+ attributes: [+ { key: "assignee", is_cf: nil, is_required: nil, translation: "Assignee" }+ ],+ query: nil+ }+ ].to_json+ }+ }+ end++ context "with an unauthorized account" do+ let(:user) { create(:user) }++ describe "the access should be restricted" do+ before { post "update", params: { type_id: "123" } }++ it { expect(response).to have_http_status(:forbidden) }+ end+ end++ it "updates the work package type" do+ put :update, params: params++ expect(response).to redirect_to(edit_type_form_configuration_path(type))++ type.reload+ expect(type.attribute_groups.count).to eq(1)+ expect(type.attribute_groups.first.key).to eql("People")+ end++ context "with invalid parameters" do+ let(:params) do+ {+ type_id: type.id,+ type: {+ attribute_groups: [+ {+ type: "attribute",+ name: "",+ attributes: [+ { key: "assignee", is_cf: nil, is_required: nil, translation: "Assignee" }+ ],+ query: nil+ }+ ].to_json+ }+ }+ end++ it "renders the edit tab" do+ put :update, params: params++ expect(response).to render_template(:edit)+ end+ end+ end+enddiff --git a/spec/features/types/form_configuration_query_spec.rb b/spec/features/types/form_configuration_query_spec.rbindex 461c0836754..f0cab72062d 100644--- a/spec/features/types/form_configuration_query_spec.rb+++ b/spec/features/types/form_configuration_query_spec.rb@@ -89,7 +89,7 @@ RSpec.describe "form query configuration", :js dodescribe "with EE token", with_ee: %i[edit_attribute_groups] dobefore dologin_as(admin)- visit edit_tab_type_path(id: type_bug.id, tab: "form_configuration")+ visit edit_type_form_configuration_path(type_bug)endit "can save an empty query group" do@@ -160,7 +160,7 @@ RSpec.describe "form query configuration", :js doarchived.update_attribute(:active, false)- visit edit_tab_type_path(id: type_bug.id, tab: "form_configuration")+ visit edit_type_form_configuration_path(type_bug)form.edit_query_group("Archived project")# Expect we now get the valid subset without the invalid project@@ -278,7 +278,7 @@ RSpec.describe "form query configuration", :js doembedded_table.reference_work_package unrelated_task# Go back to type configuration- visit edit_tab_type_path(id: type_bug.id, tab: "form_configuration")+ visit edit_type_form_configuration_path(type_bug)# Edit query to remove filtersform.edit_query_group("Subtasks")diff --git a/spec/features/types/form_configuration_spec.rb b/spec/features/types/form_configuration_spec.rbindex b89bcb20776..c39d1c0ceb9 100644--- a/spec/features/types/form_configuration_spec.rb+++ b/spec/features/types/form_configuration_spec.rb@@ -53,7 +53,7 @@ RSpec.describe "form configuration", :js, :selenium dobefore dologin_as(admin)- visit edit_tab_type_path(id: type.id, tab: "form_configuration")+ visit edit_type_form_configuration_path(type)endit "resets the form properly after changes" do@@ -258,7 +258,7 @@ RSpec.describe "form configuration", :js, :selenium docustom_fieldlogin_as(admin)- visit edit_tab_type_path(id: type.id, tab: "form_configuration")+ visit edit_type_form_configuration_path(type)endit "shows the field" do@@ -288,7 +288,7 @@ RSpec.describe "form configuration", :js, :selenium docustom_fieldlogin_as(admin)- visit edit_tab_type_path(id: type.id, tab: "form_configuration")+ visit edit_type_form_configuration_path(type)# Should be initially disabledform.expect_inactive(cf_identifier)@@ -372,7 +372,7 @@ RSpec.describe "form configuration", :js, :selenium doit "must disable adding and renaming groups" dologin_as(admin)- visit edit_tab_type_path(id: type.id, tab: "form_configuration")+ visit edit_type_form_configuration_path(type)find(".group-edit-handler", text: "DETAILS").clickdialog.expect_opendiff --git a/spec/features/types/reset_form_configuration_spec.rb b/spec/features/types/reset_form_configuration_spec.rbindex 44d5a03c2cd..71e6c68f6fa 100644--- a/spec/features/types/reset_form_configuration_spec.rb+++ b/spec/features/types/reset_form_configuration_spec.rb@@ -51,7 +51,7 @@ RSpec.describe "Reset form configuration",custom_fieldlogin_as(admin)- visit edit_tab_type_path(id: type.id, tab: "form_configuration")+ visit edit_type_form_configuration_path(type)endit "resets the form properly after changes with CFs (Regression test #27487)" dodiff --git a/spec/services/work_package_types/attribute_groups/transformer_spec.rb b/spec/services/work_package_types/attribute_groups/transformer_spec.rbindex deaff323541..ddb426ff6e8 100644--- a/spec/services/work_package_types/attribute_groups/transformer_spec.rb+++ b/spec/services/work_package_types/attribute_groups/transformer_spec.rb@@ -48,12 +48,12 @@ RSpec.describe WorkPackageTypes::AttributeGroups::Transformer dolet(:raw_groups) do[{- "name" => "Custom",- "key" => "custom",- "type" => "attribute",- "attributes" => [- { "key" => "custom_field_1" },- { "key" => "custom_field_2" }+ name: "Custom",+ key: "custom",+ type: "attribute",+ attributes: [+ { key: "custom_field_1" },+ { key: "custom_field_2" }]}]@@ -68,9 +68,9 @@ RSpec.describe WorkPackageTypes::AttributeGroups::Transformer dolet(:raw_groups) do[{- "name" => "General Info",- "type" => "attribute",- "attributes" => [{ "key" => "subject" }]+ name: "General Info",+ type: "attribute",+ attributes: [{ key: "subject" }]}]end@@ -84,10 +84,10 @@ RSpec.describe WorkPackageTypes::AttributeGroups::Transformer dolet(:raw_groups) do[{- "name" => "Embedded Table",- "type" => "query",- "attributes" => nil,- "query" => {+ name: "Embedded Table",+ type: "query",+ attributes: nil,+ query: {"columns[]" => %w[id subject],"filters" => "[]","sortBy" => "[['id', 'asc']]",@@ -111,9 +111,9 @@ RSpec.describe WorkPackageTypes::AttributeGroups::Transformer dolet(:raw_groups) do[{- "name" => "Broken",- "type" => "query",- "query" => "not a json"+ name: "Broken",+ type: "query",+ query: "not a json"}]end