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
refactored project custom field mapping controller
Jonas Jabaridcde93a3721a
diff --git a/app/components/projects/settings/project_custom_field_sections/custom_field_row_component.rb b/app/components/projects/settings/project_custom_field_sections/custom_field_row_component.rbindex 1b78fa474b0..4139e73509a 100644--- a/app/components/projects/settings/project_custom_field_sections/custom_field_row_component.rb+++ b/app/components/projects/settings/project_custom_field_sections/custom_field_row_component.rb@@ -57,8 +57,12 @@ module Projectsdef toggle_buttonrender(Primer::Beta::Button.new(tag: :a,- href: toggle_project_settings_project_custom_fields_path(project_id: @project.id,- project_custom_field_id: @project_custom_field.id),+ href: toggle_project_settings_project_custom_fields_path(+ project_custom_field_project_mapping: {+ project_id: @project.id,+ custom_field_id: @project_custom_field.id+ }+ ),scheme: :invisible,data: { 'turbo-method': :put, 'turbo-stream': true,qa_selector: "toggle-project-custom-field-mapping-#{@project_custom_field.id}" }diff --git a/app/components/projects/settings/project_custom_field_sections/show_component.html.erb b/app/components/projects/settings/project_custom_field_sections/show_component.html.erbindex db840362ceb..78f28405247 100644--- a/app/components/projects/settings/project_custom_field_sections/show_component.html.erb+++ b/app/components/projects/settings/project_custom_field_sections/show_component.html.erb@@ -17,8 +17,10 @@render(Primer::Beta::Button.new(tag: :a,href: enable_all_of_section_project_settings_project_custom_fields_path(- project_id: @project.id,- project_custom_field_section_id: @project_custom_field_section.id+ project_custom_field_project_mapping: {+ project_id: @project.id,+ custom_field_section_id: @project_custom_field_section.id+ }),scheme: :invisible,font_weight: :bold,@@ -34,8 +36,10 @@render(Primer::Beta::Button.new(tag: :a,href: disable_all_of_section_project_settings_project_custom_fields_path(- project_id: @project.id,- project_custom_field_section_id: @project_custom_field_section.id+ project_custom_field_project_mapping: {+ project_id: @project.id,+ custom_field_section_id: @project_custom_field_section.id+ }),scheme: :invisible,font_weight: :bold,diff --git a/app/contracts/project_custom_field_project_mappings/base_contract.rb b/app/contracts/project_custom_field_project_mappings/base_contract.rbnew file mode 100644index 00000000000..aa362cde087--- /dev/null+++ b/app/contracts/project_custom_field_project_mappings/base_contract.rb@@ -0,0 +1,42 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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 ProjectCustomFieldProjectMappings+ class BaseContract < ::ModelContract+ attribute :project_id+ attribute :custom_field_id++ validate :validate_has_select_project_custom_fields_permission++ def validate_has_select_project_custom_fields_permission+ return if user.allowed_in_project?(:select_project_custom_fields, model.project)++ errors.add :base, :error_unauthorized+ end+ end+enddiff --git a/app/contracts/project_custom_field_project_mappings/update_contract.rb b/app/contracts/project_custom_field_project_mappings/update_contract.rbnew file mode 100644index 00000000000..66140ecdf98--- /dev/null+++ b/app/contracts/project_custom_field_project_mappings/update_contract.rb@@ -0,0 +1,32 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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 ProjectCustomFieldProjectMappings+ class UpdateContract < BaseContract+ end+enddiff --git a/app/controllers/projects/settings/project_custom_fields_controller.rb b/app/controllers/projects/settings/project_custom_fields_controller.rbindex cd71cfc6f45..d5c64d2349b 100644--- a/app/controllers/projects/settings/project_custom_fields_controller.rb+++ b/app/controllers/projects/settings/project_custom_fields_controller.rb@@ -37,47 +37,51 @@ class Projects::Settings::ProjectCustomFieldsController < Projects::SettingsContbefore_action :eager_load_project_custom_field_project_mappings,only: %i[show toggle enable_all_of_section disable_all_of_section]+ before_action :set_project_custom_field, only: %i[toggle]+ before_action :set_project_custom_field_section, only: %i[enable_all_of_section disable_all_of_section]+def show; enddef toggle- # TODO: use service instead- @project_custom_field = ProjectCustomField.find(params[:project_custom_field_id])+ call = ProjectCustomFieldProjectMappings::ToggleService+ .new(user: current_user)+ .call(permitted_params.project_custom_field_project_mapping)- mapping = ProjectCustomFieldProjectMapping.find_or_initialize_by(- project_id: @project.id,- custom_field_id: @project_custom_field.id- )+ if call.success?+ eager_load_project_custom_field_project_mappings # reload mappings- # toggle mapping- if mapping.persisted?- mapping.destroy!+ update_custom_field_row_via_turbo_streamelse- mapping.save!+ # TODO: handle errorend- eager_load_project_custom_field_project_mappings # reload mappings-- update_custom_field_row_via_turbo_stream-respond_with_turbo_streamsenddef enable_all_of_section- bulk_edit_mappings_per_section(params[:project_custom_field_section_id], :enable)+ call = bulk_edit_service.call(action: :enable)- eager_load_project_custom_field_project_mappings # reload mappings+ if call.success?+ eager_load_project_custom_field_project_mappings # reload mappings- update_sections_via_turbo_stream # update all sections in order not to mess with stimulus target references+ update_sections_via_turbo_stream # update all sections in order not to mess with stimulus target references+ else+ # TODO: handle error+ endrespond_with_turbo_streamsenddef disable_all_of_section- bulk_edit_mappings_per_section(params[:project_custom_field_section_id], :disable)+ call = bulk_edit_service.call(action: :disable)- eager_load_project_custom_field_project_mappings # reload mappings+ if call.success?+ eager_load_project_custom_field_project_mappings # reload mappings- update_sections_via_turbo_stream # update all sections in order not to mess with stimulus target references+ update_sections_via_turbo_stream # update all sections in order not to mess with stimulus target references+ else+ # TODO: handle error+ endrespond_with_turbo_streamsend@@ -101,26 +105,25 @@ class Projects::Settings::ProjectCustomFieldsController < Projects::SettingsCont.to_aend- def bulk_edit_mappings_per_section(section_id, action = :enable)- # TODO: use service instead- section = ProjectCustomFieldSection.find(section_id)+ def set_project_custom_field+ # required for component rerenderings+ @project_custom_field = ProjectCustomField.find(+ permitted_params.project_custom_field_project_mapping[:custom_field_id]+ )+ end- # TODO: refactor this to use a single database query- section.custom_fields.each do |pcf|- mapping = ProjectCustomFieldProjectMapping.find_or_initialize_by(- project_id: @project.id,- custom_field_id: pcf.id- )+ def set_project_custom_field_section+ @project_custom_field_section = ProjectCustomFieldSection.find(+ permitted_params.project_custom_field_project_mapping[:custom_field_section_id]+ )+ end- if action == :enable- unless mapping.persisted?- mapping.save!- end- elsif action == :disable- if mapping.persisted?- mapping.destroy!- end- end- end+ def bulk_edit_service+ ProjectCustomFieldProjectMappings::BulkEditService+ .new(+ user: current_user,+ project: @project,+ project_custom_field_section: @project_custom_field_section+ )endenddiff --git a/app/models/permitted_params.rb b/app/models/permitted_params.rbindex 28487fdce67..80229c6567f 100644--- a/app/models/permitted_params.rb+++ b/app/models/permitted_params.rb@@ -288,6 +288,11 @@ class PermittedParamswhitelist.merge(custom_field_values(:project))end+ def project_custom_field_project_mapping+ params.require(:project_custom_field_project_mapping)+ .permit(*self.class.permitted_attributes[:project_custom_field_project_mapping])+ end+def newsparams.require(:news).permit(:title, :summary, :description)end@@ -552,6 +557,11 @@ class PermittedParams:name,{ type_ids: [] }],+ project_custom_field_project_mapping: %i(+ project_id+ custom_field_id+ custom_field_section_id+ ),query: %i(namedisplay_sumsdiff --git a/app/services/project_custom_field_project_mappings/bulk_edit_service.rb b/app/services/project_custom_field_project_mappings/bulk_edit_service.rbnew file mode 100644index 00000000000..e504b8aa092--- /dev/null+++ b/app/services/project_custom_field_project_mappings/bulk_edit_service.rb@@ -0,0 +1,102 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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 ProjectCustomFieldProjectMappings+ class BulkEditService < ::BaseServices::BaseCallable+ def initialize(user:, project:, project_custom_field_section:)+ super()+ @user = user+ @project = project+ @project_custom_field_section = project_custom_field_section+ end++ def perform(params)+ service_call = validate_permissions+ service_call = perform_bulk_edit(service_call, params) if service_call.success?++ service_call+ end++ def validate_permissions+ if @user.allowed_in_project?(:select_project_custom_fields, @project)+ ServiceResult.success+ else+ ServiceResult.failure(errors: { base: :error_unauthorized })+ end+ end++ def perform_bulk_edit(service_call, params)+ action = params[:action]+ custom_field_ids = fetch_custom_field_ids++ begin+ case action+ when :enable+ enable_custom_fields(custom_field_ids)+ when :disable+ disable_custom_fields(custom_field_ids)+ end+ rescue StandardError => e+ service_call.success = false+ service_call.errors = e.message+ end++ service_call+ end++ def fetch_custom_field_ids+ @project_custom_field_section.custom_fields.pluck(:id)+ end++ def enable_custom_fields(custom_field_ids)+ existing_mapping_ids = existing_mappings(custom_field_ids)+ new_mapping_ids = custom_field_ids - existing_mapping_ids++ create_mappings(new_mapping_ids) if new_mapping_ids.any?+ end++ def disable_custom_fields(custom_field_ids)+ ProjectCustomFieldProjectMapping+ .where(project_id: @project.id, custom_field_id: custom_field_ids)+ .delete_all+ end++ def existing_mappings(custom_field_ids)+ ProjectCustomFieldProjectMapping+ .where(project_id: @project.id, custom_field_id: custom_field_ids)+ .pluck(:custom_field_id)+ end++ def create_mappings(custom_field_ids)+ new_mappings = custom_field_ids.map do |id|+ { project_id: @project.id, custom_field_id: id }+ end+ ProjectCustomFieldProjectMapping.insert_all(new_mappings)+ end+ end+enddiff --git a/app/services/project_custom_field_project_mappings/set_attributes_service.rb b/app/services/project_custom_field_project_mappings/set_attributes_service.rbnew file mode 100644index 00000000000..4a6c3007920--- /dev/null+++ b/app/services/project_custom_field_project_mappings/set_attributes_service.rb@@ -0,0 +1,32 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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 ProjectCustomFieldProjectMappings+ class SetAttributesService < ::BaseServices::SetAttributes+ end+enddiff --git a/app/services/project_custom_field_project_mappings/toggle_service.rb b/app/services/project_custom_field_project_mappings/toggle_service.rbnew file mode 100644index 00000000000..c9acb5ae4d7--- /dev/null+++ b/app/services/project_custom_field_project_mappings/toggle_service.rb@@ -0,0 +1,62 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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 ProjectCustomFieldProjectMappings+ class ToggleService < ::BaseServices::Write+ def persist(service_result)+ if service_result.result.persisted?+ # destroy the mapping if it exists and catch any errors which would not be caught by active record+ begin+ service_result.result.destroy+ rescue StandardError => e+ service_result.errors = e.message+ service_result.success = false+ end+ else+ # create the mapping if it does not exist+ unless service_result.result.save+ service_result.errors = service_result.result.errors+ service_result.success = false+ end+ end++ service_result+ end++ def instance(params)+ instance_class.find_or_initialize_by(+ project_id: params[:project_id],+ custom_field_id: params[:custom_field_id]+ )+ end++ def default_contract_class+ ProjectCustomFieldProjectMappings::UpdateContract+ end+ end+end