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
feat[Op#54749]: set up foundations for bulk service, get it working first
Kabiru Mwenjaffcc2be126e6
diff --git a/app/controllers/admin/settings/project_custom_fields_controller.rb b/app/controllers/admin/settings/project_custom_fields_controller.rbindex f186a755073..3f8a3451d98 100644--- a/app/controllers/admin/settings/project_custom_fields_controller.rb+++ b/app/controllers/admin/settings/project_custom_fields_controller.rb@@ -43,6 +43,7 @@ module Admin::Settingsbefore_action :prepare_custom_option_position, only: %i(update create)before_action :find_custom_option, only: :delete_optionbefore_action :project_custom_field_mappings_query, only: %i[project_mappings unlink]+ before_action :find_link_project_custom_field_mapping, only: :linkbefore_action :find_unlink_project_custom_field_mapping, only: :unlink# rubocop:enable Rails/LexicallyScopedActionFilter@@ -73,10 +74,16 @@ module Admin::Settingsenddef link- create_service = ProjectCustomFieldProjectMappings::CreateService- .new(user: current_user)- .call(custom_field_id: @custom_field.id,- project_id: permitted_params.project_custom_field_project_mapping["project_id"])+ create_service = if include_sub_projects?+ ProjectCustomFieldProjectMappings::BulkCreateService+ .new(user: current_user, project: @project, project_custom_field: @custom_field)+ .call+ else+ ProjectCustomFieldProjectMappings::CreateService+ .new(user: current_user)+ .call(custom_field_id: @custom_field.id,+ project_id: permitted_params.project_custom_field_project_mapping["project_id"])+ endcreate_service.on_success { render_project_list }@@ -194,6 +201,17 @@ module Admin::Settingsrespond_with_turbo_streamsend+ def find_link_project_custom_field_mapping+ @project = Project.find(permitted_params.project_custom_field_project_mapping[:project_id])+ rescue ActiveRecord::RecordNotFound+ update_flash_message_via_turbo_stream(+ message: t(:notice_file_not_found), full: true, dismiss_scheme: :hide, scheme: :danger+ )+ render_project_list++ respond_with_turbo_streams+ end+def find_custom_field@custom_field = ProjectCustomField.find(params[:id])rescue ActiveRecord::RecordNotFound@@ -206,5 +224,9 @@ module Admin::Settingsupdate_section_via_turbo_stream(project_custom_field_section: call.result[:old_section])endend++ def include_sub_projects?+ ActiveRecord::Type::Boolean.new.cast(permitted_params.project_custom_field_project_mapping[:include_sub_projects])+ endendenddiff --git a/app/services/project_custom_field_project_mappings/bulk_create_service.rb b/app/services/project_custom_field_project_mappings/bulk_create_service.rbnew file mode 100644index 00000000000..177616e0f71--- /dev/null+++ b/app/services/project_custom_field_project_mappings/bulk_create_service.rb@@ -0,0 +1,81 @@+# frozen_string_literal: true++#-- 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 BulkCreateService < ::BaseServices::BaseCallable+ def initialize(user:, project:, project_custom_field:)+ super()+ @user = user+ @project = project+ @project_custom_field = project_custom_field+ end++ def perform+ service_call = validate_permissions+ service_call = perform_bulk_create(service_call) if service_call.success?++ service_call+ end++ def validate_permissions+ if @user.allowed_in_project?(:select_project_custom_fields, [@project, *@project.children])+ ServiceResult.success+ else+ ServiceResult.failure(errors: { base: :error_unauthorized })+ end+ end++ def perform_bulk_create(service_call)+ project_children_ids = @project.children.pluck(:id)+ project_children_mapping_ids = project_children_ids - existing_project_mappings(project_children_ids)+ new_mapping_ids = [@project.id, *project_children_mapping_ids]+ create_mappings(new_mapping_ids) if new_mapping_ids.any?++ service_call+ rescue StandardError => e+ service_call.success = false+ service_call.errors = e.message+ end++ def existing_project_mappings(project_ids)+ ProjectCustomFieldProjectMapping.where(+ custom_field_id: @project_custom_field.id,+ project_id: project_ids+ ).pluck(:project_id)+ end++ def create_mappings(project_ids)+ new_mappings = project_ids.map do |id|+ { project_id: id, custom_field_id: @project_custom_field.id }+ end+ ProjectCustomFieldProjectMapping.insert_all(new_mappings)+ end+ end+end