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
Remove instance variable, adds some docs, wraps some return values.
Marcello Rocha9670a166e7ef
diff --git a/app/contracts/custom_fields/hierarchy/generate_root_contract.rb b/app/contracts/custom_fields/hierarchy/generate_root_contract.rbindex 6dafd9cdf3e..137d13ab0c2 100644--- a/app/contracts/custom_fields/hierarchy/generate_root_contract.rb+++ b/app/contracts/custom_fields/hierarchy/generate_root_contract.rb@@ -32,7 +32,12 @@ module CustomFieldsmodule Hierarchyclass GenerateRootContract < Dry::Validation::Contractparams do- required(:hierarchy_root).value(:nil?)+ required(:custom_field).filled(type?: CustomField)+ end++ rule(:custom_field) do+ key.failure("must have field format 'hierarchy'") if value.field_format != "hierarchy"+ key.failure("cannot be defined") if value.hierarchy_root.present?endendenddiff --git a/app/contracts/custom_fields/hierarchy/service_initialization_contract.rb b/app/contracts/custom_fields/hierarchy/service_initialization_contract.rbdeleted file mode 100644index 8b72a534ba2..00000000000--- a/app/contracts/custom_fields/hierarchy/service_initialization_contract.rb+++ /dev/null@@ -1,43 +0,0 @@-# frozen_string_literal: true--#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) 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 CustomFields- module Hierarchy- class ServiceInitializationContract < Dry::Validation::Contract- params do- required(:field_format).filled(:string)- end-- rule(:field_format) do- key.failure("Custom field must have field format 'hierarchy'") if value != "hierarchy"- end- end- end-enddiff --git a/app/controllers/admin/custom_fields/hierarchy/items_controller.rb b/app/controllers/admin/custom_fields/hierarchy/items_controller.rbindex 416e71f6e9f..c8fa3440e33 100644--- a/app/controllers/admin/custom_fields/hierarchy/items_controller.rb+++ b/app/controllers/admin/custom_fields/hierarchy/items_controller.rb@@ -53,7 +53,7 @@ module Admindef create::CustomFields::Hierarchy::HierarchicalItemService- .new(@custom_field)+ .new.insert_item(**item_input).either(->(_) { update_via_turbo_stream(component: ItemsComponent.new(custom_field: @custom_field)) },diff --git a/app/services/custom_fields/create_service.rb b/app/services/custom_fields/create_service.rbindex 6919f05b247..314ea55e46e 100644--- a/app/services/custom_fields/create_service.rb+++ b/app/services/custom_fields/create_service.rb@@ -57,7 +57,7 @@ module CustomFieldsif cf.is_a?(ProjectCustomField)add_cf_to_visible_columns(cf)elsif cf.field_format_hierarchy?- CustomFields::Hierarchy::HierarchicalItemService.new(cf).generate_root+ CustomFields::Hierarchy::HierarchicalItemService.new.generate_root(cf)endcalldiff --git a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb b/app/services/custom_fields/hierarchy/hierarchical_item_service.rbindex 8625107355c..0e595505abb 100644--- a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb+++ b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb@@ -33,22 +33,22 @@ module CustomFieldsclass HierarchicalItemServiceinclude 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?-- @custom_field = custom_field- end-- def generate_root+ # Generate the root item for the CustomField of type hierarchy+ # @param custom_field [CustomField] custom field of type hierarchy+ # @return [Success(CustomField::Hierarchy::Item), Failure(Dry::Validation::Result), Failure(ActiveModel::Errors)]+ def generate_root(custom_field)CustomFields::Hierarchy::GenerateRootContract.new- .call(hierarchy_root: @custom_field.hierarchy_root)+ .call(custom_field:).to_monad- .bind { create_root_item }+ .bind { |validation| create_root_item(validation[:custom_field]) }end+ # Insert a new node on the hierarchy tree.+ # @param parent [CustomField::Hierarchy::Item] the parent of the node+ # @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)CustomFields::Hierarchy::InsertItemContract.new@@ -57,6 +57,11 @@ module CustomFields.bind { |validation| create_child_item(validation:) }end+ # Updates an item/node+ # @param item [CustomField::Hierarchy::Item] the item to be updated+ # @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 update_item(item:, label: nil, short: nil)CustomFields::Hierarchy::UpdateItemContract.new@@ -65,23 +70,38 @@ module CustomFields.bind { |attributes| update_item_attributes(item:, attributes:) }end+ # Delete an entire branch of the hierarchy/tree+ # @param item [CustomField::Hierarchy::Item] the parent of the node+ # @return [Success(CustomField::Hierarchy::Item), Failure(Symbol), Failure(ActiveModel::Errors)]def delete_branch(item:)return Failure(:item_is_root) if item.root?item.destroy ? Success() : Failure(item.errors)end+ # Gets all nodes in a tree from the item/node back to the root.+ # Ordered from root to leaf+ # @param item [CustomField::Hierarchy::Item] the parent of the node+ # @return [Success(Array<CustomField::Hierarchy::Item>)]def get_branch(item:)Success(item.ancestors.reverse)end+ # Move an item/node to a new parent item/node+ # @param item [CustomField::Hierarchy::Item] the parent of the node+ # @param new_parent [CustomField::Hierarchy::Item] the new parent of the node+ # @return [Success(CustomField::Hierarchy::Item)]def move_item(item:, new_parent:)Success(new_parent.append_child(item))end+ # Reorder the item along its siblings.+ # @param item [CustomField::Hierarchy::Item] the parent of the node+ # @param new_sort_order [Integer] the new parent of the node+ # @return [Success(CustomField::Hierarchy::Item)]def reorder_item(item:, new_sort_order:)old_item = item.siblings.where(sort_order: new_sort_order).first- old_item.prepend_sibling(item)+ Success(old_item.prepend_sibling(item))enddef soft_delete_item(item)@@ -91,8 +111,8 @@ module CustomFieldsprivate- def create_root_item- item = CustomField::Hierarchy::Item.create(custom_field: @custom_field)+ def create_root_item(custom_field)+ item = CustomField::Hierarchy::Item.create(custom_field.hierarchy_root)return Failure(item.errors) if item.new_record?Success(item)diff --git a/spec/contracts/custom_fields/hierarchy/generate_root_contract_spec.rb b/spec/contracts/custom_fields/hierarchy/generate_root_contract_spec.rbindex 6b484c10100..6089fe29742 100644--- a/spec/contracts/custom_fields/hierarchy/generate_root_contract_spec.rb+++ b/spec/contracts/custom_fields/hierarchy/generate_root_contract_spec.rb@@ -38,7 +38,7 @@ RSpec.describe CustomFields::Hierarchy::GenerateRootContract dolet(:custom_field) { create(:custom_field, field_format: "hierarchy", hierarchy_root: nil) }it "is valid" do- result = subject.call(hierarchy_root: custom_field.hierarchy_root)+ result = subject.call(custom_field:)expect(result).to be_successendend@@ -48,19 +48,17 @@ RSpec.describe CustomFields::Hierarchy::GenerateRootContract dolet(:custom_field) { create(:custom_field, field_format: "hierarchy", hierarchy_root:) }it "is invalid" do- result = subject.call(hierarchy_root: custom_field.hierarchy_root)+ result = subject.call(custom_field:)expect(result).to be_failure- # rubocop:disable Rails/DeprecatedActiveModelErrorsMethods- expect(result.errors.to_h).to include(hierarchy_root: ["cannot be defined"])- # rubocop:enable Rails/DeprecatedActiveModelErrorsMethods+ expect(result.errors[:custom_field]).to match_array("cannot be defined")endendcontext "when inputs are valid" do+ let(:custom_field) { create(:custom_field, field_format: "hierarchy", hierarchy_root: nil) }+it "creates a success result" do- [- { hierarchy_root: nil }- ].each { |params| expect(subject.call(params)).to be_success }+ expect(subject.call(custom_field:)).to be_successendenddiff --git a/spec/contracts/custom_fields/hierarchy/service_initialization_contract_spec.rb b/spec/contracts/custom_fields/hierarchy/service_initialization_contract_spec.rbdeleted file mode 100644index 2cf1979cc78..00000000000--- a/spec/contracts/custom_fields/hierarchy/service_initialization_contract_spec.rb+++ /dev/null@@ -1,97 +0,0 @@-# frozen_string_literal: true--#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) 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 "rails_helper"--RSpec.describe CustomFields::Hierarchy::ServiceInitializationContract do- subject { described_class.new }-- # rubocop:disable Rails/DeprecatedActiveModelErrorsMethods- describe "#call" do- context "when field_format is 'hierarchy'" do- let(:params) { { field_format: "hierarchy" } }-- it "is valid" do- result = subject.call(params)- expect(result).to be_success- end- end-- context "when field_format is not 'hierarchy'" do- let(:params) { { field_format: "text" } }-- it "is invalid" do- result = subject.call(params)- expect(result).to be_failure- expect(result.errors.to_h).to include(field_format: ["Custom field must have field format 'hierarchy'"])- end- end-- context "when field_format is missing" do- let(:params) { {} }-- it "is invalid" do- result = subject.call(params)- expect(result).to be_failure- expect(result.errors.to_h).to include(field_format: ["is missing"])- end- end-- context "when field_format is nil" do- let(:params) { { field_format: nil } }-- it "is invalid" do- result = subject.call(params)- expect(result).to be_failure- expect(result.errors.to_h).to include(field_format: ["must be filled"])- end- end-- context "when inputs are valid" do- it "creates a success result" do- [- { field_format: "hierarchy" }- ].each { |params| expect(subject.call(params)).to be_success }- end- end-- context "when inputs are invalid" do- it "creates a failure result" do- [- {},- { field_format: "text" },- { field_format: nil },- { field_format: 42 }- ].each { |params| expect(subject.call(params)).to be_failure }- end- end- end- # rubocop:enable Rails/DeprecatedActiveModelErrorsMethods-enddiff --git a/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb b/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rbindex 7bac82f3247..73e6d019d00 100644--- a/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb+++ b/spec/services/custom_fields/hierarchy/hierarchical_item_service_spec.rb@@ -34,40 +34,32 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService dolet(:custom_field) { create(:custom_field, field_format: "hierarchy", hierarchy_root: nil) }let(:invalid_custom_field) { create(:custom_field, field_format: "text", hierarchy_root: nil) }- let!(:root) { service.generate_root.value! }+ 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! }- subject(:service) { described_class.new(custom_field) }-- describe "#initialize" do- context "with valid custom field" do- it "initializes successfully" do- expect { service }.not_to raise_error- end- end-- context "with invalid custom field" do- it "raises an ArgumentError" do- expect { described_class.new(invalid_custom_field) }.to raise_error(ArgumentError, /Invalid custom field/)- end- end- end+ subject(:service) { described_class.new }describe "#generate_root" docontext "with valid hierarchy root" doit "creates a root item successfully" do- expect(service.generate_root).to be_success+ expect(service.generate_root(custom_field)).to be_successendend+ it "requires a custom field of type hierarchy" do+ result = service.generate_root(invalid_custom_field).failure++ expect(result.errors[:custom_field]).to eq(["must have field format 'hierarchy'"])+ end+context "with persistence of hierarchy root fails" doit "fails to create a root item" doallow(CustomField::Hierarchy::Item).to receive(:create).and_return(instance_double(CustomField::Hierarchy::Item, new_record?: true, errors: "some errors"))- result = service.generate_root+ result = service.generate_root(custom_field)expect(result).to be_failureendend@@ -79,8 +71,9 @@ RSpec.describe CustomFields::Hierarchy::HierarchicalItemService docontext "with valid parameters" doit "inserts an item successfully without short" do- result = service.insert_item(parent: root, label:)+ result = service.insert_item(parent: luke, label:)expect(result).to be_success+ expect(luke.reload.children.count).to eq(2)endit "inserts an item successfully with short" do