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

Resort projects when creating or updating their name

Oliver Günther78e404412ad6
diff --git a/app/models/project.rb b/app/models/project.rb
index e92be04bca2..75125c1b71d 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -34,6 +34,7 @@ class Project < ApplicationRecord
include Projects::Storage
include Projects::Activity
+ include Projects::Hierarchy
include Projects::AncestorsFromRoot
include ::Scopes::Scoped
@@ -89,8 +90,6 @@ class Project < ApplicationRecord
has_many :budgets, dependent: :destroy
has_many :notification_settings, dependent: :destroy
- acts_as_nested_set order_column: :name, dependent: :destroy
-
acts_as_customizable
acts_as_searchable columns: %W(#{table_name}.name #{table_name}.identifier #{table_name}.description),
date_column: "#{table_name}.created_at",
diff --git a/app/models/projects/hierarchy.rb b/app/models/projects/hierarchy.rb
new file mode 100644
index 00000000000..bbca5571064
--- /dev/null
+++ b/app/models/projects/hierarchy.rb
@@ -0,0 +1,61 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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 Projects::Hierarchy
+ extend ActiveSupport::Concern
+
+ included do
+ acts_as_nested_set order_column: :name, dependent: :destroy
+
+ # Keep the siblings sorted after naming changes to ensure lft sort includes name sorting
+ before_save :remember_reorder
+ after_save :reorder_by_name, if: -> { @reorder_nested_set }
+
+ def reorder_by_name
+ @reorder_nested_set = nil
+ return unless siblings.any?
+
+ left_neighbor = find_left_neighbor(parent, :name, true)
+
+ if left_neighbor
+ move_to_right_of(left_neighbor)
+ elsif self != parent.children.first
+ move_to_left_of(parent.children[0])
+ end
+ end
+
+ # We need to remember if we want to reorder as nested_set
+ # will perform another save directly in +after_save+ if a parent was set
+ # and that clear new_record? as well as previous_new_record?
+ def remember_reorder
+ @reorder_nested_set = parent_id.present? && (new_record? || name_changed?)
+ end
+ end
+end
diff --git a/app/workers/projects/reorder_children_job.rb b/app/workers/projects/reorder_children_job.rb
new file mode 100644
index 00000000000..e19397bdc58
--- /dev/null
+++ b/app/workers/projects/reorder_children_job.rb
@@ -0,0 +1,71 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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 Projects
+ class ReorderChildrenJob < ApplicationJob
+ def perform
+ Rails.logger.info { "Resorting siblings by name in the project's nested set." }
+ Project.transaction { reorder! }
+ end
+
+ private
+
+ def reorder!
+ Project
+ .where(id: unique_parent_ids)
+ .find_each(&method(:reorder_children))
+ end
+
+ def unique_parent_ids
+ Project
+ .where.not(parent_id: nil)
+ .select(:parent_id)
+ .distinct
+ end
+
+ def reorder_children(parent)
+ return unless parent.children.many?
+
+ # Resort children manually
+ sorted = parent.children.sort_by(&:name)
+
+ # Get the current first child
+ first = parent.children.first
+
+ sorted.each_with_index do |child, i|
+ if i == 0
+ child.move_to_left_of(first) unless child == first
+ else
+ child.move_to_right_of(sorted[i - 1])
+ end
+ end
+ end
+ end
+end
diff --git a/db/migrate/20220202140507_reorder_project_children.rb b/db/migrate/20220202140507_reorder_project_children.rb
new file mode 100644
index 00000000000..e78fefc2ae5
--- /dev/null
+++ b/db/migrate/20220202140507_reorder_project_children.rb
@@ -0,0 +1,5 @@
+class ReorderProjectChildren < ActiveRecord::Migration[6.1]
+ def up
+ ::Projects::ReorderChildrenJob.perform_later
+ end
+end
diff --git a/spec/models/projects/reorder_nested_set_spec.rb b/spec/models/projects/reorder_nested_set_spec.rb
new file mode 100644
index 00000000000..64e2f871116
--- /dev/null
+++ b/spec/models/projects/reorder_nested_set_spec.rb
@@ -0,0 +1,56 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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.
+#++
+
+require 'spec_helper'
+
+describe Project, 'reordering of nested set', type: :model do
+ shared_let(:parent_project) { create :project, name: 'Parent' }
+
+ # Create some children in non-alphabetical order
+ shared_let(:child_a) { create :project, name: 'A', parent: parent_project }
+ shared_let(:child_f) { create :project, name: 'F', parent: parent_project }
+ shared_let(:child_b) { create :project, name: 'B', parent: parent_project }
+
+ subject { parent_project.children.reorder(:lft) }
+
+ it 'has the correct sort' do
+ expect(subject.reload.pluck(:name)).to eq %w[A B F]
+ end
+
+ context 'when renaming a child' do
+ before do
+ child_a.update! name: 'Z'
+ end
+
+ it 'updates that order' do
+ expect(subject.reload.pluck(:name)).to eq %w[B F Z]
+ end
+ end
+end
diff --git a/spec/workers/projects/reorder_children_job_integration_spec.rb b/spec/workers/projects/reorder_children_job_integration_spec.rb
new file mode 100644
index 00000000000..d124db7ef73
--- /dev/null
+++ b/spec/workers/projects/reorder_children_job_integration_spec.rb
@@ -0,0 +1,56 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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.
+#++
+
+require 'spec_helper'
+
+describe Projects::ReorderChildrenJob, type: :model do
+ subject(:job) { described_class.perform_now }
+
+ shared_let(:parent_project) { create(:project, name: 'Parent') }
+
+ shared_let(:child_a) { create :project, name: 'A', parent: parent_project }
+ shared_let(:child_b) { create :project, name: 'B', parent: parent_project }
+ shared_let(:child_c) { create :project, name: 'C', parent: parent_project }
+
+ let(:ordered) { parent_project.children.reorder(:lft) }
+
+ before do
+ # Update the names
+ child_a.update_column(:name, 'Second')
+ child_b.update_column(:name, 'Third')
+ child_c.update_column(:name, 'First')
+ end
+
+ it 'corrects the order' do
+ expect(ordered.pluck(:name)).to eq %w[Second Third First]
+
+ subject
+
+ expect(ordered.reload.pluck(:name)).to eq %w[First Second Third]
+ end
+end