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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
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.rbindex 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 CustomFieldsparams dorequired(:hierarchy_root).value(:nil?)end-- rule(:hierarchy_root) do- key.failure("Hierarchical root already set") unless value.nil?- endendendenddiff --git a/app/contracts/custom_fields/hierarchy/update_item_contract.rb b/app/contracts/custom_fields/hierarchy/update_item_contract.rbindex 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 CustomFieldsendrule(: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?endrule(:label) donext 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")endenddiff --git a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb b/app/services/custom_fields/hierarchy/hierarchical_item_service.rbindex 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 CustomFieldsinclude 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:) }enddef delete_branch(item:)@@ -74,18 +75,17 @@ module CustomFieldsSuccess(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))enddef 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)enddef soft_delete_item(item)- # Soft delete the item and children?+ # Soft delete the item and childrenraise NotImplementedErrorend@@ -93,22 +93,24 @@ module CustomFieldsdef create_root_itemitem = 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)enddef 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)enddef 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)+ endendendenddiff --git a/spec/contracts/custom_fields/hierarchy/generate_root_contract_spec.rb b/spec/contracts/custom_fields/hierarchy/generate_root_contract_spec.rbindex 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 doresult = 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/DeprecatedActiveModelErrorsMethodsendenddiff --git a/spec/contracts/custom_fields/hierarchy/insert_item_contract_spec.rb b/spec/contracts/custom_fields/hierarchy/insert_item_contract_spec.rbindex 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 doit "is invalid" doresult = 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"])enddiff --git a/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb b/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rbindex 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 doit "fails to create a root item" doallow(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_rootexpect(result).to be_failure@@ -91,7 +91,7 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService docontext "with invalid item" doit "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 doendendend++ 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+ endend