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

Update the insert_item to care for sort_order

Marcello Rocha777c9c34f837
diff --git a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
index 72e682a2332..c903ef6044b 100644
--- a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
+++ b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
@@ -49,12 +49,12 @@ module CustomFields
# @param label [String] the node label/name that must be unique at the same tree level
# @param short [String] an alias for the node
# @return [Success(CustomField::Hierarchy::Item), Failure(Dry::Validation::Result), Failure(ActiveModel::Errors)]
- def insert_item(parent:, label:, short: nil)
+ def insert_item(parent:, label:, short: nil, sort_order: nil)
CustomFields::Hierarchy::InsertItemContract
.new
.call({ parent:, label:, short: }.compact)
.to_monad
- .bind { |validation| create_child_item(validation:) }
+ .bind { |validation| create_child_item(validation:, sort_order:) }
end
# Updates an item/node
@@ -118,11 +118,11 @@ module CustomFields
Success(item)
end
- def create_child_item(validation:)
- item = validation[:parent].children.create(label: validation[:label], short: validation[:short])
+ def create_child_item(validation:, sort_order:)
+ item = validation[:parent].children.create(label: validation[:label], short: validation[:short], sort_order:)
return Failure(item.errors) if item.new_record?
- Success(item)
+ Success(item.reload)
end
def update_item_attributes(item:, attributes:)
diff --git a/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb b/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb
index 7a197909d85..797f66f36c7 100644
--- a/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb
+++ b/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb
@@ -36,7 +36,7 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService do
let!(:root) { service.generate_root(custom_field).value! }
let!(:luke) { service.insert_item(parent: root, label: "luke").value! }
- let!(:leia) { service.insert_item(parent: luke, label: "leia").value! }
+ let!(:mara) { service.insert_item(parent: luke, label: "mara").value! }
subject(:service) { described_class.new }
@@ -80,6 +80,14 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService do
result = service.insert_item(parent: root, label:, short:)
expect(result).to be_success
end
+
+ it "insert an item into a specific sort_order" do
+ leia = service.insert_item(parent: root, label: "leia").value!
+ expect(root.reload.children).to contain_exactly(luke, leia)
+
+ bob = service.insert_item(parent: root, label: "Bob", sort_order: 1).value!
+ expect(root.reload.children).to contain_exactly(luke, bob, leia)
+ end
end
context "with invalid item" do
@@ -136,13 +144,13 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService do
describe "#get_branch" do
context "with a non-root node" do
it "returns all the ancestors to that item" do
- result = service.get_branch(item: leia)
+ result = service.get_branch(item: mara)
expect(result).to be_success
ancestors = result.value!
expect(ancestors.size).to eq(3)
- expect(ancestors).to contain_exactly(root, luke, leia)
- expect(ancestors.last).to eq(leia)
+ expect(ancestors).to contain_exactly(root, luke, mara)
+ expect(ancestors.last).to eq(mara)
end
end
@@ -159,14 +167,14 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService do
let(:lando) { service.insert_item(parent: root, label: "lando").value! }
it "move the node to the new parent" do
- expect { service.move_item(item: leia, new_parent: lando) }.to change { leia.reload.ancestors.first }.to(lando)
+ expect { service.move_item(item: mara, new_parent: lando) }.to change { mara.reload.ancestors.first }.to(lando)
end
it "all child nodes follow" do
service.move_item(item: luke, new_parent: lando)
expect(luke.reload.ancestors).to contain_exactly(root, lando)
- expect(leia.reload.ancestors).to contain_exactly(root, lando, luke)
+ expect(mara.reload.ancestors).to contain_exactly(root, lando, luke)
end
end