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

chore[Op#57579]: elevate bulk creation to custom fields DRY up project attributes

Kabiru Mwenjaf9e8e8feaff1
diff --git a/app/services/custom_fields/custom_field_projects/bulk_create_service.rb b/app/services/custom_fields/custom_field_projects/bulk_create_service.rb
index ecbee879828..3b452006ce6 100644
--- a/app/services/custom_fields/custom_field_projects/bulk_create_service.rb
+++ b/app/services/custom_fields/custom_field_projects/bulk_create_service.rb
@@ -49,10 +49,10 @@ module CustomFields
private
- def validate_permissions
+ def validate_permissions(permission: :select_custom_fields)
return ServiceResult.failure(errors: I18n.t(:label_not_found)) if incoming_projects.empty?
- if @user.allowed_in_project?(:select_custom_fields, incoming_projects)
+ if @user.allowed_in_project?(permission, incoming_projects)
ServiceResult.success
else
ServiceResult.failure(errors: I18n.t("activerecord.errors.messages.error_unauthorized"))
@@ -117,15 +117,8 @@ module CustomFields
custom_field_project_mapping_class.new(params)
end
- def attributes_service_class
- custom_field_project_mapping_module::SetAttributesService
- end
-
- def default_contract_class
- custom_field_project_mapping_module::UpdateContract
- end
-
- def custom_field_project_mapping_module = CustomFields::CustomFieldProjects
+ def attributes_service_class = CustomFields::CustomFieldProjects::SetAttributesService
+ def default_contract_class = CustomFields::CustomFieldProjects::UpdateContract
def custom_field_project_mapping_class = CustomFieldsProject
end
end
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 982a27a03e6..432112326ac 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
@@ -29,99 +29,19 @@
#++
module ProjectCustomFieldProjectMappings
- class BulkCreateService < ::BaseServices::BaseCallable
+ class BulkCreateService < ::CustomFields::CustomFieldProjects::BulkCreateService
def initialize(user:, projects:, project_custom_field:, include_sub_projects: false)
- super()
- @user = user
- @projects = projects
- @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
+ super(user:, projects:, custom_field: project_custom_field, include_sub_projects:)
end
private
- def validate_permissions
- return ServiceResult.failure(errors: I18n.t(:label_not_found)) if incoming_projects.empty?
-
- if @user.allowed_in_project?(:select_project_custom_fields, incoming_projects)
- ServiceResult.success
- else
- ServiceResult.failure(errors: I18n.t("activerecord.errors.messages.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)
- ProjectCustomFieldProjectMapping.insert_all(
- service_call.result.map { |model| model.attributes.slice("project_id", "custom_field_id") },
- unique_by: %i[project_id custom_field_id]
- )
-
- service_call
- end
-
- def incoming_mapping_ids
- project_ids = incoming_projects.pluck(:id)
- project_ids - existing_project_mappings(project_ids)
+ def validate_permissions(permission: :select_project_custom_fields)
+ super
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)
- ProjectCustomFieldProjectMapping.where(
- custom_field_id: @project_custom_field.id,
- project_id: project_ids
- ).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)
- ProjectCustomFieldProjectMapping.new(params)
- end
-
- def attributes_service_class
- ProjectCustomFieldProjectMappings::SetAttributesService
- end
-
- def default_contract_class
- ProjectCustomFieldProjectMappings::UpdateContract
- end
+ def attributes_service_class = ProjectCustomFieldProjectMappings::SetAttributesService
+ def default_contract_class = ProjectCustomFieldProjectMappings::UpdateContract
+ def custom_field_project_mapping_class = ProjectCustomFieldProjectMapping
end
end