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

method extraction

ulfertsbc94536cc158
diff --git a/app/services/projects/copy/dependency.rb b/app/services/projects/copy/dependency.rb
index 54740b2ffb8..be50c7b4beb 100644
--- a/app/services/projects/copy/dependency.rb
+++ b/app/services/projects/copy/dependency.rb
@@ -41,7 +41,7 @@ module Projects::Copy
# Check whether this dependency should be copied
# as it was selected
def self.should_copy?(params, check)
- return true unless params[:only].present?
+ return true if params[:only].blank?
params[:only].any? { |key| key.to_sym == check }
end
diff --git a/app/services/projects/copy/work_packages_dependent_service.rb b/app/services/projects/copy/work_packages_dependent_service.rb
index 8da626e3eba..3f78e750247 100644
--- a/app/services/projects/copy/work_packages_dependent_service.rb
+++ b/app/services/projects/copy/work_packages_dependent_service.rb
@@ -39,37 +39,39 @@ module Projects::Copy
protected
def copy_dependency(params:)
+ to_copy = source_work_packages
+
# Stores the source work_package id as a key and the copied work package ID as the
- # value. Used to map the two together for work_package relations.
- work_packages_map = {}
+ # value. Used to map the two together for work_package relations.
+ work_packages_map = copy_work_packages(to_copy)
+ # Relations after in case copied work_packages are related to each other.
+ copy_work_packages_relations(to_copy, work_packages_map)
+
+ state.work_package_id_lookup = work_packages_map
+ end
- # Get work_packages sorted by their depth in the hierarchy tree
- # so that parents get copied before their children.
- to_copy = source
+ # Get work_packages sorted by their depth in the hierarchy tree
+ # so that parents get copied before their children.
+ def source_work_packages
+ source
.work_packages
.includes(:custom_values, :version, :assigned_to, :responsible)
.order_by_ancestors('asc')
.order('id ASC')
+ end
+ def copy_work_packages(to_copy)
user_cf_ids = WorkPackageCustomField.where(field_format: 'user').pluck(:id)
- to_copy.each do |wp|
+ to_copy.inject({}) do |work_packages_map, wp|
parent_id = work_packages_map[wp.parent_id] || wp.parent_id
new_wp = copy_work_package(wp, parent_id, user_cf_ids)
work_packages_map[wp.id] = new_wp.id if new_wp
- end
-
- # Relations and attachments after in case work_packages related each other
- to_copy.each do |wp|
- new_wp_id = work_packages_map[wp.id]
- next unless new_wp_id
- copy_relations(wp, new_wp_id, work_packages_map)
+ work_packages_map
end
-
- state.work_package_id_lookup = work_packages_map
end
def copy_work_package(source_work_package, parent_id, user_cf_ids)
@@ -94,6 +96,15 @@ module Projects::Copy
end
end
+ def copy_work_packages_relations(to_copy, work_packages_map)
+ to_copy.each do |wp|
+ new_wp_id = work_packages_map[wp.id]
+ next unless new_wp_id
+
+ copy_relations(wp, new_wp_id, work_packages_map)
+ end
+ end
+
def copy_relations(source_wp, new_wp_id, work_packages_map)
Relation.of_work_package(source_wp).each do |source_relation|
from_id, to_id = relations_from_to(source_relation, source_wp, new_wp_id, work_packages_map)