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

feat[Op#54749]: refactor bulk create to include redundant contract validations

Kabiru Mwenjaf748c0bdb873
diff --git a/app/controllers/admin/settings/project_custom_fields_controller.rb b/app/controllers/admin/settings/project_custom_fields_controller.rb
index 3f8a3451d98..8844d1b4f72 100644
--- a/app/controllers/admin/settings/project_custom_fields_controller.rb
+++ b/app/controllers/admin/settings/project_custom_fields_controller.rb
@@ -74,16 +74,10 @@ module Admin::Settings
end
def link
- 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"])
- end
+ create_service = ProjectCustomFieldProjectMappings::BulkCreateService
+ .new(user: current_user, project: @project, project_custom_field: @custom_field,
+ include_sub_projects: include_sub_projects?)
+ .call
create_service.on_success { render_project_list }
diff --git a/app/services/project_custom_field_project_mappings/bulk_create_service.rb b/app/services/project_custom_field_project_mappings/bulk_create_service.rb
index 177616e0f71..8bc811d85ba 100644
--- a/app/services/project_custom_field_project_mappings/bulk_create_service.rb
+++ b/app/services/project_custom_field_project_mappings/bulk_create_service.rb
@@ -30,33 +30,47 @@
module ProjectCustomFieldProjectMappings
class BulkCreateService < ::BaseServices::BaseCallable
- def initialize(user:, project:, project_custom_field:)
+ def initialize(user:, project:, project_custom_field:, include_sub_projects: false)
super()
@user = user
@project = project
@project_custom_field = project_custom_field
+ @include_sub_projects = include_sub_projects
end
def perform
service_call = validate_permissions
+ service_call = validate_contract(service_call, incoming_mapping_ids) if service_call.success?
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])
+ if @user.allowed_in_project?(:select_project_custom_fields, projects)
ServiceResult.success
else
ServiceResult.failure(errors: { base: :error_unauthorized })
end
end
+ def validate_contract(service_call, project_ids)
+ set_attributes_results = project_ids.map do |id|
+ set_attributes(project_id: id, custom_field_id: @project_custom_field.id)
+ 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)
- 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?
+ ProjectCustomFieldProjectMapping.import(service_call.result, validate: false)
service_call
rescue StandardError => e
@@ -64,6 +78,17 @@ module ProjectCustomFieldProjectMappings
service_call.errors = e.message
end
+ def incoming_mapping_ids
+ project_ids = projects.pluck(:id)
+ project_ids - existing_project_mappings(project_ids)
+ end
+
+ def projects
+ [@project].tap do |projects_array|
+ projects_array.concat(@project.children.to_a) if @include_sub_projects
+ end
+ end
+
def existing_project_mappings(project_ids)
ProjectCustomFieldProjectMapping.where(
custom_field_id: @project_custom_field.id,
@@ -71,11 +96,25 @@ module ProjectCustomFieldProjectMappings
).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)
+ 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)
+ ProjectCustomFieldProjectMapping.new(params)
+ end
+
+ def attributes_service_class
+ ProjectCustomFieldProjectMappings::SetAttributesService
+ end
+
+ def default_contract_class
+ ProjectCustomFieldProjectMappings::UpdateContract
end
end
end
diff --git a/app/services/project_custom_field_project_mappings/create_service.rb b/app/services/project_custom_field_project_mappings/create_service.rb
deleted file mode 100644
index 16d8d20e0a0..00000000000
--- a/app/services/project_custom_field_project_mappings/create_service.rb
+++ /dev/null
@@ -1,37 +0,0 @@
-# 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 CreateService < ::BaseServices::Create
- def default_contract_class
- ProjectCustomFieldProjectMappings::UpdateContract
- end
- end
-end