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

[65257] Update the Types Subject Configuration Controller (#19364)

Marcello Rochab73683b7e7b1
diff --git a/app/components/work_package_types/subject_configuration_component.rb b/app/components/work_package_types/subject_configuration_component.rb
index a407c2b5b94..1e0eb784468 100644
--- a/app/components/work_package_types/subject_configuration_component.rb
+++ b/app/components/work_package_types/subject_configuration_component.rb
@@ -43,7 +43,7 @@ module WorkPackageTypes
form_model = subject_form_object
{
- url: subject_configuration_type_path(id: model.id),
+ url: type_subject_configuration_path(type_id: model.id),
method: :put,
model: form_model,
data: {
diff --git a/app/controllers/work_package_types/base_tab_controller.rb b/app/controllers/work_package_types/base_tab_controller.rb
new file mode 100644
index 00000000000..83f6800d77f
--- /dev/null
+++ b/app/controllers/work_package_types/base_tab_controller.rb
@@ -0,0 +1,44 @@
+# 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 BaseTabController < ApplicationController
+ layout "admin"
+
+ before_action :require_admin
+ before_action :find_type
+
+ private
+
+ def find_type
+ @type = ::Type.find(params[:type_id])
+ end
+ end
+end
diff --git a/app/controllers/work_package_types/projects_tab_controller.rb b/app/controllers/work_package_types/projects_tab_controller.rb
index 21b4f2715a7..e99c1f54755 100644
--- a/app/controllers/work_package_types/projects_tab_controller.rb
+++ b/app/controllers/work_package_types/projects_tab_controller.rb
@@ -23,17 +23,13 @@
#
# 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.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
module WorkPackageTypes
- class ProjectsTabController < ApplicationController
- layout "admin"
-
- before_action :require_admin
- before_action :find_type
+ class ProjectsTabController < BaseTabController
before_action :load_projects, only: :edit
current_menu_item [:edit, :update] do
diff --git a/app/controllers/work_package_types/subject_configuration_tab_controller.rb b/app/controllers/work_package_types/subject_configuration_tab_controller.rb
index c2c269f82e4..bbe09fe7793 100644
--- a/app/controllers/work_package_types/subject_configuration_tab_controller.rb
+++ b/app/controllers/work_package_types/subject_configuration_tab_controller.rb
@@ -29,62 +29,42 @@
#++
module WorkPackageTypes
- class SubjectConfigurationTabController < ApplicationController
- layout "admin"
+ class SubjectConfigurationTabController < BaseTabController
+ current_menu_item [:edit, :update] do
+ :types
+ end
- before_action :require_admin
- before_action :find_type, only: %i[update_subject_configuration]
+ def edit; end
- def update_subject_configuration
- form_params = params.expect(
- work_package_types_forms_subject_configuration_form_model: %i[subject_configuration pattern]
- ).to_h
+ def update
+ permitted = params.expect(work_package_types_forms_subject_configuration_form_model: %i[subject_configuration pattern]).to_h
- UpdateTypeService.new(@type, current_user)
- .call({ patterns: pattern_collection_update(form_params) }) do |call|
- call.on_success do
- redirect_to tab_path, notice: I18n.t(:notice_successful_update)
- end
+ result = UpdateService.new(model: @type, user: current_user, contract_class: UpdateSubjectPatternContract)
+ .call(patterns: build_patterns(permitted))
- call.on_failure do
- params[:tab] = "subject_configuration"
- render template: "types/edit", status: :unprocessable_entity
- end
+ if result.success?
+ redirect_to edit_type_subject_configuration_path(@type), status: :see_other
+ else
+ render :edit, status: :unprocessable_entity
end
end
private
- def find_type
- @type = ::Type.find(params[:id])
- end
-
- def tab_path = edit_tab_type_path(id: @type.id, tab: :subject_configuration)
-
- def pattern_collection_update(form_params)
- patterns = @type.patterns.to_h.symbolize_keys
-
- subject_pattern =
- case form_params
- in { subject_configuration: "generated", pattern: String => blueprint }
- { subject: { blueprint:, enabled: true } }
- in { subject_configuration: "manual", pattern: String => blueprint }
- if blueprint.empty?
- # Submitting the form with an empty blueprint and manual subject configuration will
- # remove the subject pattern from the collection
- nil
- else
- { subject: { blueprint:, enabled: false } }
- end
- else
+ def build_patterns(form_params)
+ case form_params
+ in { subject_configuration: "generated", pattern: String => blueprint }
+ { subject: { blueprint:, enabled: true } }
+ in { subject_configuration: "manual", pattern: String => blueprint }
+ if blueprint.empty?
+ # Submitting the form with an empty blueprint and manual subject configuration will
+ # remove the subject pattern from the collection
nil
+ else
+ { subject: { blueprint:, enabled: false } }
end
-
- if subject_pattern.nil?
- patterns.delete(:subject)
- patterns
else
- patterns.merge(subject_pattern)
+ nil
end
end
end
diff --git a/app/helpers/tabs_helper.rb b/app/helpers/tabs_helper.rb
index e572d158880..a33b92413cb 100644
--- a/app/helpers/tabs_helper.rb
+++ b/app/helpers/tabs_helper.rb
@@ -65,8 +65,7 @@ module TabsHelper
end
def selected_tab(tabs)
- selected = tabs.detect { |t| t[:name].to_s == params[:tab].to_s } || tabs.detect { |t| tab_route_shown?(t) }
-
+ selected = tabs.detect { |t| t[:name].to_s == params[:tab].to_s } || tabs.detect { tab_route_shown?(it) }
return selected unless selected.nil?
tabs.first
@@ -80,8 +79,15 @@ module TabsHelper
end
def tab_route_shown?(tab)
- # Check not only for exact matches but also for sub-routes, like
- # /module_a/items and /module_a/items/:id
- request&.path&.starts_with?(tab[:path])
+ path = request&.path
+ return false if path.blank?
+
+ # Check not only for exact matches but also for sub-routes
+ # The first test matches cases when the current path is a subset of the tab path, like edit routes:
+ # Ex: /module_a/items/:id/edit matches /module_a/items/:id
+ # The second test is the other way around, when the current path is a subset of the tab path, like hierarchy cf paths
+ # /module_a/items & /module_a/items/:id
+
+ tab[:path].starts_with?(path) || path.starts_with?(tab[:path])
end
end
diff --git a/app/helpers/types_helper.rb b/app/helpers/types_helper.rb
index d815e34d23d..0c5ac0a53fd 100644
--- a/app/helpers/types_helper.rb
+++ b/app/helpers/types_helper.rb
@@ -35,7 +35,7 @@ module ::TypesHelper
{
name: "settings",
path: edit_type_settings_path(@type),
- label: I18n.t("types.edit.settings.tab"),
+ label: I18n.t("types.edit.settings.tab")
},
{
name: "form_configuration",
@@ -44,9 +44,8 @@ module ::TypesHelper
},
{
name: "subject_configuration",
- path: edit_tab_type_path(id: @type.id, tab: :subject_configuration),
+ path: edit_type_subject_configuration_path(type_id: @type.id),
label: I18n.t("types.edit.subject_configuration.tab"),
- view_component: WorkPackageTypes::SubjectConfigurationComponent,
enterprise_feature: :work_package_subject_generation
},
{
diff --git a/app/views/work_package_types/subject_configuration_tab/edit.html.erb b/app/views/work_package_types/subject_configuration_tab/edit.html.erb
new file mode 100644
index 00000000000..0ec659dcfb5
--- /dev/null
+++ b/app/views/work_package_types/subject_configuration_tab/edit.html.erb
@@ -0,0 +1,35 @@
+<%#-- 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) %>
+
+<%= render WorkPackageTypes::SubjectConfigurationComponent
+ .new(@type, subject_configuration_form_data: params[:work_package_types_forms_subject_configuration_form_model]) %>
diff --git a/config/routes.rb b/config/routes.rb
index dc714210c08..f013daf18dc 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -141,13 +141,11 @@ Rails.application.routes.draw 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]
+ resource :subject_configuration, controller: "work_package_types/subject_configuration_tab", only: %i[update edit]
member do
get "edit/:tab" => "types#edit", as: "edit_tab"
match "update/:tab" => "types#update", as: "update_tab", via: %i[post patch]
- put :subject_configuration,
- controller: "work_package_types/subject_configuration_tab",
- action: "update_subject_configuration"
end
resources :pdf_export_template, only: %i[],
diff --git a/spec/controllers/work_package_types/projects_tab_controller_spec.rb b/spec/controllers/work_package_types/projects_tab_controller_spec.rb
index ab90aa9b858..0a0e3a853d8 100644
--- a/spec/controllers/work_package_types/projects_tab_controller_spec.rb
+++ b/spec/controllers/work_package_types/projects_tab_controller_spec.rb
@@ -23,7 +23,7 @@
#
# 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.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
diff --git a/spec/controllers/work_package_types/subject_configuration_tab_controller_spec.rb b/spec/controllers/work_package_types/subject_configuration_tab_controller_spec.rb
index 8cade82168b..9efe1bc1137 100644
--- a/spec/controllers/work_package_types/subject_configuration_tab_controller_spec.rb
+++ b/spec/controllers/work_package_types/subject_configuration_tab_controller_spec.rb
@@ -31,85 +31,79 @@
require "spec_helper"
-RSpec.describe WorkPackageTypes::SubjectConfigurationTabController do
- let(:user) { create(:admin) }
- let(:wp_type) { create(:type) }
+module WorkPackageTypes
+ RSpec.describe SubjectConfigurationTabController do
+ let(:user) { create(:admin) }
+ let(:wp_type) { create(:type) }
- current_user { user }
+ current_user { user }
- context "when the user is not logged in" do
- let(:user) { User.anonymous }
+ context "when the user is not logged in" do
+ let(:user) { User.anonymous }
- it "requires login" do
- put :update_subject_configuration, params: { id: wp_type.id }
- expect(response.status).to redirect_to signin_url(back_url: subject_configuration_type_url(wp_type))
+ it "requires login" do
+ get :edit, params: { type_id: wp_type.id }
+ expect(response.status).to redirect_to signin_url(back_url: edit_type_subject_configuration_url(wp_type))
+ end
end
- end
- context "when the user is not an admin" do
- let(:user) { create(:user) }
+ context "when the user is not an admin" do
+ let(:user) { create(:user) }
- it "responds with forbidden" do
- put :update_subject_configuration, params: { id: wp_type.id }
- expect(response).to have_http_status :forbidden
+ it "responds with forbidden" do
+ put :update, params: { type_id: wp_type.id }
+ expect(response).to have_http_status :forbidden
+ end
end
- end
- describe "PUT #update_subject_configuration" do
- before do
- service_double = instance_double(UpdateTypeService)
- allow(service_double)
- .to(receive(:call))
- .with({ patterns: expected_pattern_data })
- .and_yield(service_result)
- allow(UpdateTypeService).to receive(:new).and_return(service_double)
-
- put :update_subject_configuration,
- params: {
- id: wp_type.id,
- work_package_types_forms_subject_configuration_form_model: form_data
- }
- end
+ describe "PUT #update" do
+ before do
+ put :update, params: {
+ type_id: wp_type.id,
+ work_package_types_forms_subject_configuration_form_model: form_data
+ }
+ end
- context "if form data is invalid" do
- let(:form_data) { { subject_configuration: "generated", pattern: nil } }
- let(:expected_pattern_data) { { subject: { blueprint: "", enabled: true } } }
- let(:service_result) { ServiceResult.failure }
+ context "if form data is invalid" do
+ let(:form_data) { { subject_configuration: "generated", pattern: nil } }
+ let(:expected_pattern_data) { { subject: { blueprint: "", enabled: true } } }
+ let(:service_result) { ServiceResult.failure }
- it "renders the edit template" do
- expect(response).to have_http_status :unprocessable_entity
- expect(response).to render_template "types/edit"
+ it "renders the edit template" do
+ expect(response).to have_http_status :unprocessable_entity
+ expect(response).to render_template "work_package_types/subject_configuration_tab/edit"
+ end
end
- end
- context "if form data is valid" do
- context "with generated subject configuration" do
- let(:form_data) { { subject_configuration: "generated", pattern: "Vacation - {{assignee}}" } }
- let(:expected_pattern_data) { { subject: { blueprint: "Vacation - {{assignee}}", enabled: true } } }
- let(:service_result) { ServiceResult.success }
+ context "if form data is valid" do
+ context "with generated subject configuration" do
+ let(:form_data) { { subject_configuration: "generated", pattern: "Vacation - {{assignee}}" } }
+ let(:expected_pattern_data) { { subject: { blueprint: "Vacation - {{assignee}}", enabled: true } } }
+ let(:service_result) { ServiceResult.success }
- it "redirects to the current tab path" do
- expect(response).to redirect_to edit_tab_type_path(id: wp_type.id, tab: :subject_configuration)
+ it "redirects to the current tab path" do
+ expect(response).to redirect_to edit_type_subject_configuration_path(type_id: wp_type.id)
+ end
end
- end
- context "with manual subject configuration, but still persisted blueprint" do
- let(:form_data) { { subject_configuration: "manual", pattern: "Vacation - {{assignee}}" } }
- let(:expected_pattern_data) { { subject: { blueprint: "Vacation - {{assignee}}", enabled: false } } }
- let(:service_result) { ServiceResult.success }
+ context "with manual subject configuration, but still persisted blueprint" do
+ let(:form_data) { { subject_configuration: "manual", pattern: "Vacation - {{assignee}}" } }
+ let(:expected_pattern_data) { { subject: { blueprint: "Vacation - {{assignee}}", enabled: false } } }
+ let(:service_result) { ServiceResult.success }
- it "redirects to the current tab path" do
- expect(response).to redirect_to edit_tab_type_path(id: wp_type.id, tab: :subject_configuration)
+ it "redirects to the current tab path" do
+ expect(response).to redirect_to edit_type_subject_configuration_path(type_id: wp_type.id)
+ end
end
- end
- context "with manual subject configuration and no blueprint" do
- let(:form_data) { { subject_configuration: "manual", pattern: nil } }
- let(:expected_pattern_data) { {} }
- let(:service_result) { ServiceResult.success }
+ context "with manual subject configuration and no blueprint" do
+ let(:form_data) { { subject_configuration: "manual", pattern: nil } }
+ let(:expected_pattern_data) { {} }
+ let(:service_result) { ServiceResult.success }
- it "redirects to the current tab path" do
- expect(response).to redirect_to edit_tab_type_path(id: wp_type.id, tab: :subject_configuration)
+ it "redirects to the current tab path" do
+ expect(response).to redirect_to edit_type_subject_configuration_path(type_id: wp_type.id)
+ end
end
end
end