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

rebuild positions for tasks

ulferts3e042356390c
diff --git a/modules/backlogs/lib/open_project/backlogs/list.rb b/modules/backlogs/lib/open_project/backlogs/list.rb
index 249ebff6bf4..7c173ebebf0 100644
--- a/modules/backlogs/lib/open_project/backlogs/list.rb
+++ b/modules/backlogs/lib/open_project/backlogs/list.rb
@@ -58,7 +58,7 @@ module OpenProject::Backlogs::List
remove_from_list
reload
- prev = self.class.find_by_id(prev_id.to_i)
+ prev = self.class.find_by(id: prev_id.to_i)
# If it should be the first story, move it to the 1st position
if prev.blank?
@@ -146,7 +146,12 @@ module OpenProject::Backlogs::List
end
def set_default_prev_positions_silently(prev)
- prev.version.rebuild_positions(prev.project)
+ if prev.is_task?
+ prev.version.rebuild_task_positions(prev)
+ else
+ prev.version.rebuild_story_positions(prev.project)
+ end
+
prev.reload.position
end
end
diff --git a/modules/backlogs/lib/open_project/backlogs/patches/version_patch.rb b/modules/backlogs/lib/open_project/backlogs/patches/version_patch.rb
index 1bda57eba37..26ebfade0f1 100644
--- a/modules/backlogs/lib/open_project/backlogs/patches/version_patch.rb
+++ b/modules/backlogs/lib/open_project/backlogs/patches/version_patch.rb
@@ -39,7 +39,7 @@ module OpenProject::Backlogs::Patches::VersionPatch
end
module InstanceMethods
- def rebuild_positions(project = self.project)
+ def rebuild_story_positions(project = self.project)
return unless project.backlogs_enabled?
WorkPackage.transaction do
@@ -47,17 +47,19 @@ module OpenProject::Backlogs::Patches::VersionPatch
WorkPackage.where(['project_id = ? AND type_id NOT IN (?) AND position IS NOT NULL', project, Story.types])
.update_all(position: nil)
- # Add work_packages w/o position to the top of the list and add
- # work_packages, that have a position, at the end
- stories_wo_position = work_packages.where(project_id: project, type_id: Story.types, position: nil).order(Arel.sql('id'))
+ rebuild_positions(work_packages.where(project_id: project), Story.types)
+ end
+
+ nil
+ end
- stories_w_position = work_packages.where(project_id: project, type_id: Story.types)
- .where('position IS NOT NULL')
- .order(Arel.sql('COALESCE(position, 0), id'))
+ def rebuild_task_positions(task)
+ return unless task.project.backlogs_enabled?
- (stories_w_position + stories_wo_position).each_with_index do |story, index|
- story.update_column(:position, index + 1)
- end
+ WorkPackage.transaction do
+ # Add work_packages w/o position to the top of the list and add
+ # work_packages, that have a position, at the end
+ rebuild_positions(task.story.children.where(project_id: task.project), Task.type)
end
nil
@@ -77,6 +79,24 @@ module OpenProject::Backlogs::Patches::VersionPatch
def hash
id.hash
end
+
+ private
+
+ def rebuild_positions(scope, type_ids)
+ wo_position = scope
+ .where(type_id: type_ids,
+ position: nil)
+ .order(Arel.sql('id'))
+
+ w_position = scope
+ .where(type_id: type_ids)
+ .where.not(position: nil)
+ .order(Arel.sql('COALESCE(position, 0), id'))
+
+ (w_position + wo_position).each_with_index do |work_package, index|
+ work_package.update_column(:position, index + 1)
+ end
+ end
end
end
diff --git a/modules/backlogs/lib/open_project/backlogs/patches/work_package_patch.rb b/modules/backlogs/lib/open_project/backlogs/patches/work_package_patch.rb
index 15d2f120ed1..49f1f3266d7 100644
--- a/modules/backlogs/lib/open_project/backlogs/patches/work_package_patch.rb
+++ b/modules/backlogs/lib/open_project/backlogs/patches/work_package_patch.rb
@@ -113,15 +113,16 @@ module OpenProject::Backlogs::Patches::WorkPackagePatch
def story
if is_story?
- return Story.find(id)
+ Story.find(id)
elsif is_task?
- # Make sure to get the closest ancestor that is a Story, i.e. the one with the highest lft
- # otherwise, the highest parent that is a Story is returned
- story_work_package = ancestors.find_by(type_id: Story.types).order(Arel.sql('lft DESC'))
- return Story.find(story_work_package.id) if story_work_package
+ # Make sure to get the closest ancestor that is a Story
+ ancestors_relations
+ .includes(:from)
+ .where(from: { type_id: Story.types })
+ .order(hierarchy: :asc)
+ .first
+ .from
end
-
- nil
end
def blocks
diff --git a/modules/backlogs/spec/models/version_spec.rb b/modules/backlogs/spec/models/version_spec.rb
index 36fc0261b26..5a67a211afe 100644
--- a/modules/backlogs/spec/models/version_spec.rb
+++ b/modules/backlogs/spec/models/version_spec.rb
@@ -127,7 +127,7 @@ describe Version, type: :model do
expect(work_package1.version_id).to eq(version.id)
end
- it 'rebuilds postions' do
+ it 'rebuilds positions' do
e1 = create_work_package(type_id: epic_type.id)
s2 = create_work_package(type_id: story_type.id)
s3 = create_work_package(type_id: story_type.id)
@@ -145,7 +145,7 @@ describe Version, type: :model do
t3.update_column(:position, 3)
o9.update_column(:position, 9)
- version.rebuild_positions(project)
+ version.rebuild_story_positions(project)
work_packages = version
.work_packages