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
refactor: Start to decouple base bulk create service
Kabiru Mwenjae979a3492026
diff --git a/app/services/bulk_services/project_mappings/base_create_service.rb b/app/services/bulk_services/project_mappings/base_create_service.rbindex bf5518cd583..c46f3976257 100644--- a/app/services/bulk_services/project_mappings/base_create_service.rb+++ b/app/services/bulk_services/project_mappings/base_create_service.rb@@ -28,20 +28,79 @@# See COPYRIGHT and LICENSE files for more details.#+++# Gives the projects to map and the attributes to be used for the mapping.+class ProjectsMapper+ attr_reader :model,+ :projects,+ :mapping_model_class,+ :model_foreign_key_id,+ :include_sub_projects++ def initialize(model:,+ projects:,+ mapping_model_class:,+ model_foreign_key_id:,+ include_sub_projects: false)+ @model = model+ @projects = projects+ @mapping_model_class = mapping_model_class+ @model_foreign_key_id = model_foreign_key_id+ @include_sub_projects = include_sub_projects+ end++ def mapping_attributes_for_all_projects(params)+ project_ids_to_map.map do |project_id|+ {+ project_id:,+ model_foreign_key_id => model.id+ }.merge(attributes_from_params(params))+ end+ end++ def incoming_projects+ projects.each_with_object(Set.new) do |project, projects_set|+ next unless project.active?++ projects_set << project+ projects_set.merge(project.active_subprojects.to_a) if include_sub_projects+ end.to_a+ end++ private++ def project_ids_to_map+ project_ids = incoming_projects.pluck(:id)+ project_ids - project_ids_already_mapped(project_ids)+ end++ def project_ids_already_mapped(project_ids)+ mapping_model_class.where(+ model_foreign_key_id => @model.id,+ project_id: project_ids+ ).pluck(:project_id)+ end+end+module BulkServicesmodule ProjectMappingsclass BaseCreateService < ::BaseServices::BaseCallable- def initialize(user:, projects:, model:, include_sub_projects: false)+ attr_reader :projects_mapper++ def initialize(user:, projects:, model:, include_sub_projects: false, projects_mapper: nil)super()@user = user- @projects = projects- @model = model- @include_sub_projects = include_sub_projects+ @projects_mapper = projects_mapper || ProjectsMapper.new(+ model:,+ projects:,+ mapping_model_class:,+ model_foreign_key_id:,+ include_sub_projects:+ )enddef perform(params = {})service_call = validate_permissions- service_call = validate_contract(service_call, incoming_mapping_ids, params) if service_call.success?+ service_call = validate_contract(service_call, nil, params) if service_call.success?service_call = perform_bulk_create(service_call) if service_call.success?service_call = after_perform(service_call, params) if service_call.success?@@ -51,18 +110,18 @@ module BulkServicesprivatedef validate_permissions- return ServiceResult.failure(errors: I18n.t(:label_not_found)) if incoming_projects.empty?+ return ServiceResult.failure(errors: I18n.t(:label_not_found)) if projects_mapper.incoming_projects.empty?- if @user.allowed_in_project?(permission, incoming_projects)+ if @user.allowed_in_project?(permission, projects_mapper.incoming_projects)ServiceResult.successelseServiceResult.failure(errors: I18n.t("activerecord.errors.messages.error_unauthorized"))endend- def validate_contract(service_call, project_ids, _params)- set_attributes_results = project_ids.map do |id|- set_attributes(project_id: id, model_foreign_key_id => @model.id)+ def validate_contract(service_call, _project_ids, params)+ set_attributes_results = projects_mapper.mapping_attributes_for_all_projects(params).map do |mapping_attributes|+ set_attributes(mapping_attributes)endif (failures = set_attributes_results.select(&:failure?)).any?@@ -75,6 +134,11 @@ module BulkServicesservice_callend+ # override in subclasses to pass additional parameters to the `SetAttributesService`.+ def attributes_from_params(_params)+ {}+ end+def perform_bulk_create(service_call)mapping_model_class.insert_all(service_call.result.map { |model| model.attributes.slice("project_id", model_foreign_key_id.to_s) },@@ -88,27 +152,6 @@ module BulkServicesservice_call # Subclasses can override this method to add additional logicend- def incoming_mapping_ids- project_ids = incoming_projects.pluck(:id)- project_ids - existing_project_mappings(project_ids)- end-- def incoming_projects- @projects.each_with_object(Set.new) do |project, projects_set|- next unless project.active?-- projects_set << project- projects_set.merge(project.active_subprojects.to_a) if @include_sub_projects- end.to_a- end-- def existing_project_mappings(project_ids)- mapping_model_class.where(- model_foreign_key_id => @model.id,- project_id: project_ids- ).pluck(:project_id)- end-def set_attributes(params)attributes_service_class.new(user: @user,diff --git a/app/services/bulk_services/project_mappings/projects_mapper.rb b/app/services/bulk_services/project_mappings/projects_mapper.rbnew file mode 100644index 00000000000..f4edf7310b9--- /dev/null+++ b/app/services/bulk_services/project_mappings/projects_mapper.rb@@ -0,0 +1,86 @@+# 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.+#++++# Gives the projects to map and the attributes to be used for the mapping.+module BulkServices+ module ProjectMappings+ class ProjectsMapper < ProjectsMapperBase+ attr_reader :model,+ :projects,+ :mapping_model_class,+ :model_foreign_key_id,+ :include_sub_projects++ def initialize(model:,+ projects:,+ mapping_model_class:,+ model_foreign_key_id:,+ include_sub_projects: false)+ @model = model+ @projects = projects+ @mapping_model_class = mapping_model_class+ @model_foreign_key_id = model_foreign_key_id+ @include_sub_projects = include_sub_projects+ end++ def mapping_attributes_for_all_projects(params)+ project_ids_to_map.map do |project_id|+ {+ project_id:,+ model_foreign_key_id => model.id+ }.merge(attributes_from_params(params))+ end+ end++ def incoming_projects+ projects.each_with_object(Set.new) do |project, projects_set|+ next unless project.active?++ projects_set << project+ projects_set.merge(project.active_subprojects.to_a) if include_sub_projects+ end.to_a+ end++ private++ def project_ids_to_map+ project_ids = incoming_projects.pluck(:id)+ project_ids - project_ids_already_mapped(project_ids)+ end++ def project_ids_already_mapped(project_ids)+ mapping_model_class.where(+ model_foreign_key_id => @model.id,+ project_id: project_ids+ ).pluck(:project_id)+ end+ end+ end+enddiff --git a/app/services/bulk_services/project_mappings/projects_mapper_base.rb b/app/services/bulk_services/project_mappings/projects_mapper_base.rbnew file mode 100644index 00000000000..dc3321a7b7c--- /dev/null+++ b/app/services/bulk_services/project_mappings/projects_mapper_base.rb@@ -0,0 +1,86 @@+# 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.+#++++# Gives the projects to map and the attributes to be used for the mapping.+module BulkServices+ module ProjectMappings+ class ProjectsMapper+ attr_reader :model,+ :projects,+ :mapping_model_class,+ :model_foreign_key_id,+ :include_sub_projects++ def initialize(model:,+ projects:,+ mapping_model_class:,+ model_foreign_key_id:,+ include_sub_projects: false)+ @model = model+ @projects = projects+ @mapping_model_class = mapping_model_class+ @model_foreign_key_id = model_foreign_key_id+ @include_sub_projects = include_sub_projects+ end++ def mapping_attributes_for_all_projects(params)+ project_ids_to_map.map do |project_id|+ {+ project_id:,+ model_foreign_key_id => model.id+ }.merge(attributes_from_params(params))+ end+ end++ def incoming_projects+ projects.each_with_object(Set.new) do |project, projects_set|+ next unless project.active?++ projects_set << project+ projects_set.merge(project.active_subprojects.to_a) if include_sub_projects+ end.to_a+ end++ private++ def project_ids_to_map+ project_ids = incoming_projects.pluck(:id)+ project_ids - project_ids_already_mapped(project_ids)+ end++ def project_ids_already_mapped(project_ids)+ mapping_model_class.where(+ model_foreign_key_id => @model.id,+ project_id: project_ids+ ).pluck(:project_id)+ end+ end+ end+enddiff --git a/modules/storages/app/services/storages/last_project_folders/bulk_create_service.rb b/modules/storages/app/services/storages/last_project_folders/bulk_create_service.rbindex bd3ea5a26b0..9596b10381a 100644--- a/modules/storages/app/services/storages/last_project_folders/bulk_create_service.rb+++ b/modules/storages/app/services/storages/last_project_folders/bulk_create_service.rb@@ -29,82 +29,27 @@#++module Storages::LastProjectFolders- class BulkCreateService < ::BaseServices::BaseCallable+ class BulkCreateService < ::BulkServices::ProjectMappings::BaseCreateService+ attr_reader :something+def initialize(user:, project_storages:)- super()- @user = user- @project_storages = project_storages+ super(user:, projects: nil, model: nil, include_sub_projects: nil,+ projects_mapper: ProjectStoragesProjectsMapper.new(project_storages))end- def perform- service_call = validate_permissions- service_call = validate_contract(service_call) if service_call.success?- service_call = perform_bulk_create(service_call) if service_call.success?-- service_call- end+ def permission = :manage_files_in_project+ def mapping_model_class = ::Storages::LastProjectFolderprivate- def validate_permissions- return ServiceResult.failure(errors: I18n.t(:label_not_found)) if incoming_projects.empty?-- if @user.allowed_in_project?(:manage_files_in_project, incoming_projects)- ServiceResult.success- else- ServiceResult.failure(errors: I18n.t("activerecord.errors.messages.error_unauthorized"))- end- end-- def validate_contract(service_call)- set_attributes_results = @project_storages.map do |project_storage|- set_attributes(project_storage_id: project_storage.id, origin_folder_id: project_storage.project_folder_id,- mode: project_storage.project_folder_mode)- end-- if (failures = set_attributes_results.select(&:failure?)).any?- service_call.success = false- service_call.errors = failures.map(&:errors)- else- service_call.result = set_attributes_results.map(&:result)- end-- service_call- end-def perform_bulk_create(service_call)- bulk_insertion = ::Storages::LastProjectFolder.insert_all(+ bulk_insertion = mapping_model_class.insert_all(service_call.result.map { |model| model.attributes.slice("project_storage_id", "origin_folder_id", "mode") },returning: %w[id])- service_call.result = ::Storages::LastProjectFolder.where(id: bulk_insertion.rows.flatten)+ service_call.result = mapping_model_class.where(id: bulk_insertion.rows.flatten)service_callend-- def incoming_projects- @incoming_projects ||= Project.where(id: @project_storages.pluck(:project_id))- end-- def set_attributes(params)- attributes_service_class- .new(user: @user,- model: instance(params),- contract_class: default_contract_class,- contract_options: {})- .call(params)- end-- def instance(params)- ::Storages::LastProjectFolder.new(params)- end-- def attributes_service_class- Storages::LastProjectFolders::SetAttributesService- end-- def default_contract_class- Storages::LastProjectFolders::CreateContract- endendenddiff --git a/modules/storages/app/services/storages/last_project_folders/project_storages_projects_mapper.rb b/modules/storages/app/services/storages/last_project_folders/project_storages_projects_mapper.rbnew file mode 100644index 00000000000..b2d944d3dcf--- /dev/null+++ b/modules/storages/app/services/storages/last_project_folders/project_storages_projects_mapper.rb@@ -0,0 +1,56 @@+# 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 Storages+ module LastProjectFolders+ class ProjectStoragesProjectsMapper < BulkServices::ProjectMappings::ProjectsMapperBase+ attr_reader :project_storages++ def initialize(project_storages)+ super()+ @project_storages = project_storages+ end++ def mapping_attributes_for_all_projects(_params)+ project_storages.map do |project_storage|+ {+ project_storage_id: project_storage.id,+ origin_folder_id: project_storage.project_folder_id,+ mode: project_storage.project_folder_mode+ }+ end+ end++ def incoming_projects+ @incoming_projects ||= Project.where(id: project_storages.pluck(:project_id))+ end+ end+ end+enddiff --git a/modules/storages/app/services/storages/last_project_folders/project_storages_projects_mapper.rb b/modules/storages/app/services/storages/last_project_folders/project_storages_projects_mapper.rbnew file mode 100644index 00000000000..8b137891791--- /dev/null+++ b/modules/storages/app/services/storages/last_project_folders/project_storages_projects_mapper.rb@@ -0,0 +1 @@+diff --git a/modules/storages/app/services/storages/project_storages/bulk_create_service.rb b/modules/storages/app/services/storages/project_storages/bulk_create_service.rbindex b6c9205c527..590fd54e2b4 100644--- a/modules/storages/app/services/storages/project_storages/bulk_create_service.rb+++ b/modules/storages/app/services/storages/project_storages/bulk_create_service.rb@@ -49,19 +49,17 @@ module Storages::ProjectStoragesdef default_contract_class = ::Storages::ProjectStorages::CreateContractdef validate_contract(service_call, project_ids, params)- project_folder_params = params.slice(:project_folder_mode, :project_folder_id)+ super_service_call = super- set_attributes_results = project_ids.map do |id|- set_attributes(project_id: id, storage_id: @model.id, **project_folder_params)+ super_service_call.on_failure do+ super_service_call.errors = super_service_call.errors.firstend- if (failures = set_attributes_results.select(&:failure?)).any?- service_call = failures.first- else- service_call.result = set_attributes_results.map(&:result)- end+ super_service_call+ end- service_call+ def attributes_from_params(params)+ params.slice(:project_folder_mode, :project_folder_id)enddef perform_bulk_create(service_call)@@ -91,7 +89,7 @@ module Storages::ProjectStoragesevent: :created,project_folder_mode: params[:project_folder_mode],project_folder_mode_previously_was: nil,- storage: @model+ storage: something.model)endend