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 specs, contracts, draft inplementation of the missing actions

Marcello Rocha80edc9e04b92
diff --git a/app/contracts/custom_fields/hierarchy/generate_root_contract.rb b/app/contracts/custom_fields/hierarchy/generate_root_contract.rb
index 03ebff45ff6..6dafd9cdf3e 100644
--- a/app/contracts/custom_fields/hierarchy/generate_root_contract.rb
+++ b/app/contracts/custom_fields/hierarchy/generate_root_contract.rb
@@ -34,10 +34,6 @@ module CustomFields
params do
required(:hierarchy_root).value(:nil?)
end
-
- rule(:hierarchy_root) do
- key.failure("Hierarchical root already set") unless value.nil?
- end
end
end
end
diff --git a/app/contracts/custom_fields/hierarchy/update_item_contract.rb b/app/contracts/custom_fields/hierarchy/update_item_contract.rb
index e61289df264..b78f45369eb 100644
--- a/app/contracts/custom_fields/hierarchy/update_item_contract.rb
+++ b/app/contracts/custom_fields/hierarchy/update_item_contract.rb
@@ -38,14 +38,14 @@ module CustomFields
end
rule(:item) do
- key.failure("must exist") unless value.persisted?
+ key.failure("must exist") if value.new_record?
key.failure("must not be a root item") if value.root?
end
rule(:label) do
next unless key?
- if CustomField::Hierarchy::Item.exists?(parent_id: values[:item].parent, label: value)
+ if CustomField::Hierarchy::Item.exists?(parent_id: values[:item].parent_id, label: value)
key.failure("must be unique at the same hierarchical level")
end
end
diff --git a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
index a0602c285dd..8625107355c 100644
--- a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
+++ b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
@@ -34,6 +34,7 @@ module CustomFields
include Dry::Monads[:result]
def initialize(custom_field)
+ # Is this overkill? We are only checking `custom_field.field_format`
validation = ServiceInitializationContract.new.call(field_format: custom_field.field_format)
raise ArgumentError, "Invalid custom field: #{validation.errors(full: true).to_h}" if validation.failure?
@@ -61,7 +62,7 @@ module CustomFields
.new
.call({ item:, label:, short: }.compact)
.to_monad
- .fmap { |attributes| update_item_attributes(item:, attributes:) }
+ .bind { |attributes| update_item_attributes(item:, attributes:) }
end
def delete_branch(item:)
@@ -74,18 +75,17 @@ module CustomFields
Success(item.ancestors.reverse)
end
- def move_item(item:, new_parent:, sort_order:)
- # Move with all the children
- raise NotImplementedError
+ def move_item(item:, new_parent:)
+ Success(new_parent.append_child(item))
end
def reorder_item(item:, new_sort_order:)
- # move it around. Check closure_tree
- raise NotImplementedError
+ old_item = item.siblings.where(sort_order: new_sort_order).first
+ old_item.prepend_sibling(item)
end
def soft_delete_item(item)
- # Soft delete the item and children?
+ # Soft delete the item and children
raise NotImplementedError
end
@@ -93,22 +93,24 @@ module CustomFields
def create_root_item
item = CustomField::Hierarchy::Item.create(custom_field: @custom_field)
- return Failure(item.errors) unless item.persisted?
+ return Failure(item.errors) if item.new_record?
Success(item)
end
def create_child_item(validation:)
item = validation[:parent].children.create(label: validation[:label], short: validation[:short])
- return Failure(item.errors) unless item.persisted?
+ return Failure(item.errors) if item.new_record?
Success(item)
end
def update_item_attributes(item:, attributes:)
- item.update(label: attributes[:label], short: attributes[:short])
-
- item.errors.empty? ? Success(item) : Failure(item.errors)
+ if item.update(label: attributes[:label], short: attributes[:short])
+ Success(item)
+ else
+ Failure(item.errors)
+ end
end
end
end
diff --git a/spec/contracts/custom_fields/hierarchy/generate_root_contract_spec.rb b/spec/contracts/custom_fields/hierarchy/generate_root_contract_spec.rb
index 78429458a2d..6b484c10100 100644
--- a/spec/contracts/custom_fields/hierarchy/generate_root_contract_spec.rb
+++ b/spec/contracts/custom_fields/hierarchy/generate_root_contract_spec.rb
@@ -51,7 +51,7 @@ RSpec.describe CustomFields::Hierarchy::GenerateRootContract do
result = subject.call(hierarchy_root: custom_field.hierarchy_root)
expect(result).to be_failure
# rubocop:disable Rails/DeprecatedActiveModelErrorsMethods
- expect(result.errors.to_h).to include(hierarchy_root: ["Hierarchical root already set"])
+ expect(result.errors.to_h).to include(hierarchy_root: ["cannot be defined"])
# rubocop:enable Rails/DeprecatedActiveModelErrorsMethods
end
end
diff --git a/spec/contracts/custom_fields/hierarchy/insert_item_contract_spec.rb b/spec/contracts/custom_fields/hierarchy/insert_item_contract_spec.rb
index 58e5d9c0cfc..900e519481c 100644
--- a/spec/contracts/custom_fields/hierarchy/insert_item_contract_spec.rb
+++ b/spec/contracts/custom_fields/hierarchy/insert_item_contract_spec.rb
@@ -67,7 +67,6 @@ RSpec.describe CustomFields::Hierarchy::InsertItemContract do
it "is invalid" do
result = subject.call(params)
expect(result).to be_failure
- expect(result.errors.to_h).to include(label: ["must be unique at the same hierarchical level"])
expect(result.errors.to_h).to include(label: ["must be unique within the same hierarchy level"])
end
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 4b070f19cde..7bac82f3247 100644
--- a/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb
+++ b/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb
@@ -65,7 +65,7 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService do
it "fails to create a root item" do
allow(CustomField::Hierarchy::Item)
.to receive(:create)
- .and_return(instance_double(CustomField::Hierarchy::Item, persisted?: false, errors: "some errors"))
+ .and_return(instance_double(CustomField::Hierarchy::Item, new_record?: true, errors: "some errors"))
result = service.generate_root
expect(result).to be_failure
@@ -91,7 +91,7 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService do
context "with invalid item" do
it "fails to insert an item" do
- child = instance_double(CustomField::Hierarchy::Item, persisted?: false, errors: "some errors")
+ child = instance_double(CustomField::Hierarchy::Item, new_record?: true, errors: "some errors")
allow(root.children).to receive(:create).and_return(child)
result = service.insert_item(parent: root, label:, short:)
@@ -161,4 +161,32 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService do
end
end
end
+
+ describe "#move_item" 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)
+ 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)
+ end
+ end
+
+ describe "reorder_item" do
+ let!(:lando) { service.insert_item(parent: root, label: "lando").value! }
+ let!(:chewbacca) { service.insert_item(parent: root, label: "AWOOO").value! }
+
+ it "moves the note to the requested position" do
+ service.reorder_item(item: chewbacca, new_sort_order: 1)
+
+ expect(luke.reload.sort_order).to eq(0)
+ expect(chewbacca.reload.sort_order).to eq(1)
+ expect(lando.reload.sort_order).to eq(2)
+ end
+ end
end