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

[#66412] amend persistence layer to handle item scores

Eric Schubert20d427954da7
diff --git a/app/contracts/custom_fields/hierarchy/insert_item_contract.rb b/app/contracts/custom_fields/hierarchy/insert_list_item_contract.rb
similarity index 96%
rename from app/contracts/custom_fields/hierarchy/insert_item_contract.rb
rename to app/contracts/custom_fields/hierarchy/insert_list_item_contract.rb
index 043e1d90225..0a4cbb8a46f 100644
--- a/app/contracts/custom_fields/hierarchy/insert_item_contract.rb
+++ b/app/contracts/custom_fields/hierarchy/insert_list_item_contract.rb
@@ -23,14 +23,14 @@
#
# 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.
+# 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 InsertItemContract < Dry::Validation::Contract
+ class InsertListItemContract < Dry::Validation::Contract
config.messages.backend = :i18n
params do
diff --git a/app/contracts/custom_fields/hierarchy/insert_scored_item_contract.rb b/app/contracts/custom_fields/hierarchy/insert_scored_item_contract.rb
new file mode 100644
index 00000000000..14ec96fefd2
--- /dev/null
+++ b/app/contracts/custom_fields/hierarchy/insert_scored_item_contract.rb
@@ -0,0 +1,55 @@
+# 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 InsertScoredItemContract < Dry::Validation::Contract
+ config.messages.backend = :i18n
+
+ params do
+ required(:parent).filled(type?: CustomField::Hierarchy::Item)
+ required(:label).filled(:string)
+ required(:score).filled(:decimal)
+ end
+
+ rule(:parent) do
+ next if schema_error?(:parent)
+
+ key.failure("must exist") unless value.persisted?
+ end
+
+ rule(:label) do
+ next if schema_error?(:parent)
+
+ key.failure(:not_unique) if values[:parent].children.exists?(label: value)
+ end
+ end
+ end
+end
diff --git a/app/contracts/custom_fields/hierarchy/update_item_contract.rb b/app/contracts/custom_fields/hierarchy/update_list_item_contract.rb
similarity index 96%
rename from app/contracts/custom_fields/hierarchy/update_item_contract.rb
rename to app/contracts/custom_fields/hierarchy/update_list_item_contract.rb
index 614cd121ab7..7bfbecd13f6 100644
--- a/app/contracts/custom_fields/hierarchy/update_item_contract.rb
+++ b/app/contracts/custom_fields/hierarchy/update_list_item_contract.rb
@@ -23,14 +23,14 @@
#
# 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.
+# 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 UpdateItemContract < Dry::Validation::Contract
+ class UpdateListItemContract < Dry::Validation::Contract
config.messages.backend = :i18n
params do
diff --git a/app/contracts/custom_fields/hierarchy/update_scored_item_contract.rb b/app/contracts/custom_fields/hierarchy/update_scored_item_contract.rb
new file mode 100644
index 00000000000..cddb5b282e7
--- /dev/null
+++ b/app/contracts/custom_fields/hierarchy/update_scored_item_contract.rb
@@ -0,0 +1,54 @@
+# 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 UpdateScoredItemContract < Dry::Validation::Contract
+ config.messages.backend = :i18n
+
+ params do
+ required(:item).filled(type?: CustomField::Hierarchy::Item)
+ optional(:label).filled(:string)
+ optional(:score).filled(:decimal)
+ end
+
+ rule(:item) do
+ key.failure(:not_persisted) if value.new_record?
+ key.failure(:root_item) if value.root?
+ end
+
+ rule(:label) do
+ next if schema_error?(:item)
+
+ key.failure(:not_unique) if values[:item].siblings.exists?(label: value)
+ end
+ end
+ end
+end
diff --git a/app/controllers/admin/custom_fields/hierarchy/items_controller.rb b/app/controllers/admin/custom_fields/hierarchy/items_controller.rb
index 3af126a8ca1..6b53f291e2c 100644
--- a/app/controllers/admin/custom_fields/hierarchy/items_controller.rb
+++ b/app/controllers/admin/custom_fields/hierarchy/items_controller.rb
@@ -23,7 +23,7 @@
#
# 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.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
@@ -62,7 +62,7 @@ module Admin
def create
item_service
- .insert_item(**item_input)
+ .insert_item(contract: create_contract, **item_input)
.either(
lambda do |item|
redirect_to(
@@ -78,8 +78,13 @@ module Admin
end
def update
+ input = item_input
item_service
- .update_item(item: @active_item, label: item_input[:label], short: item_input[:short])
+ .update_item(contract: update_contract,
+ item: @active_item,
+ label: input[:label],
+ short: input[:short],
+ score: input[:score])
.either(
lambda do |_|
redirect_to(custom_field_item_path(@custom_field, @active_item.parent), status: :see_other)
@@ -119,14 +124,37 @@ module Admin
::CustomFields::Hierarchy::HierarchicalItemService.new
end
- def item_input
+ def item_input # rubocop:disable Metrics/AbcSize
input = { parent: @active_item, label: params[:label] }
input[:short] = params[:short] if params[:short].present?
+ input[:score] = params[:score] if params[:score].present?
input[:sort_order] = params[:sort_order].to_i if params[:sort_order].present?
input
end
+ def create_contract
+ case @custom_field.field_format
+ when "hierarchy"
+ ::CustomFields::Hierarchy::InsertListItemContract
+ when "scored_list"
+ ::CustomFields::Hierarchy::InsertScoredItemContract
+ else
+ raise ArgumentError, "unsupported custom field format '#{@custom_field.field_format}'"
+ end
+ end
+
+ def update_contract
+ case @custom_field.field_format
+ when "hierarchy"
+ ::CustomFields::Hierarchy::UpdateListItemContract
+ when "scored_list"
+ ::CustomFields::Hierarchy::UpdateScoredItemContract
+ else
+ raise ArgumentError, "unsupported custom field format '#{@custom_field.field_format}'"
+ end
+ end
+
def add_errors_to_form(validation_result)
@new_item = ::CustomField::Hierarchy::Item.new(**item_input)
validation_result.errors(full: true).to_h.each do |attribute, errors|
diff --git a/app/forms/custom_fields/hierarchy/item_form.rb b/app/forms/custom_fields/hierarchy/item_form.rb
index fa05ae5c9a4..e75c8ecc81d 100644
--- a/app/forms/custom_fields/hierarchy/item_form.rb
+++ b/app/forms/custom_fields/hierarchy/item_form.rb
@@ -23,7 +23,7 @@
#
# 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.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
@@ -98,6 +98,7 @@ module CustomFields
name: :score,
label: I18n.t("custom_fields.admin.items.placeholder.score"),
type: :number,
+ step: :any,
value: @target_item.score,
visually_hide_label: true,
full_width: false,
diff --git a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
index 2123063571b..8d53a964bc6 100644
--- a/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
+++ b/app/services/custom_fields/hierarchy/hierarchical_item_service.rb
@@ -23,7 +23,7 @@
#
# 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.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
@@ -45,28 +45,34 @@ module CustomFields
end
# Insert a new node on the hierarchy tree at a desired position or at the end if no sort_order is passed.
+ # @param contract [Class<CustomFields::Hierarchy::InsertListItemContract>, Class<CustomFields::Hierarchy::InsertScoredItemContract>]
+ # the params validation contract class
# @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
+ # @param score [Decimal] a numeric value for the node
# @param sort_order [Integer] the position into which insert the item.
# @return [Success(CustomField::Hierarchy::Item), Failure(Dry::Validation::Result), Failure(ActiveModel::Errors)]
- def insert_item(parent:, label:, short: nil, sort_order: nil)
- CustomFields::Hierarchy::InsertItemContract
+ def insert_item(contract:, parent:, label:, short: nil, score: nil, sort_order: nil)
+ contract
.new
- .call({ parent:, label:, short: }.compact)
+ .call({ parent:, label:, short:, score: }.compact)
.to_monad
.bind { |validation| create_child_item(validation:, sort_order:) }
end
# Updates an item/node
+ # @param contract [Class<CustomFields::Hierarchy::UpdateListItemContract>, Class<CustomFields::Hierarchy::UpdateScoredItemContract>]
+ # the params validation contract class
# @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
+ # @param score [Decimal] a numeric value 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
+ def update_item(contract:, item:, label: nil, short: nil, score: nil)
+ contract
.new
- .call({ item:, label:, short: }.compact)
+ .call({ item:, label:, short:, score: }.compact)
.to_monad
.bind { |attributes| update_item_attributes(item:, attributes:) }
end
@@ -173,7 +179,7 @@ module CustomFields
end
def update_item_attributes(item:, attributes:)
- if item.update(label: attributes[:label], short: attributes[:short])
+ if item.update(label: attributes[:label], short: attributes[:short], score: attributes[:score])
Success(item)
else
Failure(item.errors)
diff --git a/config/locales/en.yml b/config/locales/en.yml
index b1bf372a57b..158525bc795 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -364,6 +364,7 @@ en:
errors:
defined: "must not be defined."
int?: "must be an integer."
+ decimal?: "must be a decimal."
filled?: "must be filled."
greater_or_equal_zero: "must be greater or equal to 0."
not_found: "not found."
diff --git a/spec/contracts/custom_fields/hierarchy/insert_item_contract_spec.rb b/spec/contracts/custom_fields/hierarchy/insert_list_item_contract_spec.rb
similarity index 98%
rename from spec/contracts/custom_fields/hierarchy/insert_item_contract_spec.rb
rename to spec/contracts/custom_fields/hierarchy/insert_list_item_contract_spec.rb
index 36050663dd7..bc7aab367b2 100644
--- a/spec/contracts/custom_fields/hierarchy/insert_item_contract_spec.rb
+++ b/spec/contracts/custom_fields/hierarchy/insert_list_item_contract_spec.rb
@@ -23,14 +23,14 @@
#
# 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.
+# 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::InsertItemContract do
+RSpec.describe CustomFields::Hierarchy::InsertListItemContract do
subject { described_class.new }
# rubocop:disable Rails/DeprecatedActiveModelErrorsMethods
diff --git a/spec/contracts/custom_fields/hierarchy/insert_scored_item_contract_spec.rb b/spec/contracts/custom_fields/hierarchy/insert_scored_item_contract_spec.rb
new file mode 100644
index 00000000000..4d36d28a1bc
--- /dev/null
+++ b/spec/contracts/custom_fields/hierarchy/insert_scored_item_contract_spec.rb
@@ -0,0 +1,134 @@
+# 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::InsertScoredItemContract do
+ subject { described_class.new }
+
+ # rubocop:disable Rails/DeprecatedActiveModelErrorsMethods
+ describe "#call" do
+ let(:parent) { create(:hierarchy_item) }
+
+ context "when all required fields are valid" do
+ let(:params) { { parent:, label: "Valid Label", score: 0.1337 } }
+
+ it "is valid" do
+ result = subject.call(params)
+ expect(result).to be_success
+ end
+ end
+
+ context "when parent is not of type 'Item'" do
+ let(:invalid_parent) { create(:custom_field) }
+ let(:params) { { parent: invalid_parent, label: "Valid Label", score: 0.1337 } }
+
+ it "is invalid" do
+ result = subject.call(params)
+ expect(result).to be_failure
+ expect(result.errors.to_h).to include(parent: ["must be CustomField::Hierarchy::Item"])
+ end
+ end
+
+ context "when label is not unique within the same hierarchy level" do
+ let(:params) { { parent:, label: "Duplicate Label", score: 0.1337 } }
+
+ before { create(:hierarchy_item, parent:, label: "Duplicate Label", score: 0.1337) }
+
+ it "is invalid" do
+ result = subject.call(params)
+ expect(result).to be_failure
+ expect(result.errors.to_h).to include(label: [I18n.t("dry_validation.errors.rules.label.not_unique")])
+ end
+
+ context "if another locale is set" do
+ let(:mordor) { "agh burzum-ishi krimpatul" }
+
+ before do
+ I18n.config.enforce_available_locales = false
+ I18n.backend.store_translations(
+ :mo,
+ { dry_validation: {
+ errors: { rules: { label: { not_unique: mordor } } }
+ } }
+ )
+ end
+
+ after do
+ I18n.config.enforce_available_locales = true
+ end
+
+ it "is invalid with localized validation errors" do
+ I18n.with_locale(:mo) do
+ result = subject.call(params)
+ expect(result).to be_failure
+ expect(result.errors.to_h).to include(label: [mordor])
+ end
+ end
+ end
+ end
+
+ context "when score is not a decimal value" do
+ let(:params) { { parent:, label: "Valid Label", score: "pi" } }
+
+ it "is invalid with localized validation errors" do
+ result = subject.call(params)
+ expect(result).to be_failure
+ expect(result.errors.to_h).to include(score: [I18n.t("dry_validation.errors.decimal?")])
+ end
+ end
+
+ context "when inputs are valid" do
+ it "creates a success result" do
+ [
+ { parent:, label: "A label", score: 0.1337 },
+ { parent:, label: "Another label", score: 1.47e12 }
+ ].each { |params| expect(subject.call(params)).to be_success }
+ end
+ end
+
+ context "when inputs are invalid" do
+ it "creates a failure result" do
+ [
+ { parent:, label: "A label", score: "" },
+ { parent:, label: "A label", score: nil },
+ { parent:, label: "", score: 1.47e12 },
+ { parent:, label: nil, score: 1.47e12 },
+ { parent: },
+ { parent: nil },
+ { parent: nil, label: "A label", score: 1.47e12 },
+ { parent: "parent", label: "A label", score: 1.47e12 },
+ { parent: 42, label: "A label", score: 1.47e12 }
+ ].each { |params| expect(subject.call(params)).to be_failure }
+ end
+ end
+ end
+ # rubocop:enable Rails/DeprecatedActiveModelErrorsMethods
+end
diff --git a/spec/contracts/custom_fields/hierarchy/update_item_contract_spec.rb b/spec/contracts/custom_fields/hierarchy/update_list_item_contract_spec.rb
similarity index 98%
rename from spec/contracts/custom_fields/hierarchy/update_item_contract_spec.rb
rename to spec/contracts/custom_fields/hierarchy/update_list_item_contract_spec.rb
index decb03ed751..5f09cd57479 100644
--- a/spec/contracts/custom_fields/hierarchy/update_item_contract_spec.rb
+++ b/spec/contracts/custom_fields/hierarchy/update_list_item_contract_spec.rb
@@ -23,14 +23,14 @@
#
# 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.
+# 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::UpdateItemContract do
+RSpec.describe CustomFields::Hierarchy::UpdateListItemContract do
subject { described_class.new }
# rubocop:disable Rails/DeprecatedActiveModelErrorsMethods
diff --git a/spec/contracts/custom_fields/hierarchy/update_scored_item_contract_spec.rb b/spec/contracts/custom_fields/hierarchy/update_scored_item_contract_spec.rb
new file mode 100644
index 00000000000..dcd6d471eb7
--- /dev/null
+++ b/spec/contracts/custom_fields/hierarchy/update_scored_item_contract_spec.rb
@@ -0,0 +1,112 @@
+# 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::UpdateScoredItemContract do
+ subject { described_class.new }
+
+ # rubocop:disable Rails/DeprecatedActiveModelErrorsMethods
+ describe "#call" do
+ let!(:impact) { create(:hierarchy_item) }
+ let!(:high) { create(:hierarchy_item, label: "HIGH", score: 1.17e-12, parent: impact) }
+ let!(:middle) { create(:hierarchy_item, label: "Middle", score: 1, parent: impact) }
+ let!(:low) { create(:hierarchy_item, label: "low", score: 9.81e6, parent: impact) }
+
+ context "when all required fields are valid" do
+ it "is valid" do
+ [
+ { item: high, label: "VERY HIGH", score: 1.17e-12 },
+ { item: high, label: "HIGH", score: 1.17e-11 },
+ { item: high, label: "VERY HIGH" },
+ { item: high, score: 1.17e-10 },
+ { item: high }
+ ].each { |params| expect(subject.call(params)).to be_success }
+ end
+ end
+
+ context "when item is a root item" do
+ let(:params) { { item: impact } }
+
+ it("is invalid") do
+ result = subject.call(params)
+ expect(result).to be_failure
+ expect(result.errors.to_h).to include(item: ["cannot be a root item."])
+ end
+ end
+
+ context "when item is not of type 'Item'" do
+ let(:invalid_item) { create(:custom_field) }
+ let(:params) { { item: invalid_item } }
+
+ it("is invalid") do
+ result = subject.call(params)
+ expect(result).to be_failure
+ expect(result.errors.to_h).to include(item: ["must be CustomField::Hierarchy::Item"])
+ end
+ end
+
+ context "when item is not persisted" do
+ let(:item) { build(:hierarchy_item, parent: impact) }
+ let(:params) { { item: } }
+
+ it "is invalid" do
+ result = subject.call(params)
+ expect(result).to be_failure
+ expect(result.errors[:item]).to match_array("must be an already existing item.")
+ end
+ end
+
+ context "when the label already exist in the same hierarchy level" do
+ let(:params) { { item: high, label: "Middle" } }
+
+ it "is invalid" do
+ result = subject.call(params)
+ expect(result).to be_failure
+
+ expect(result.errors[:label]).to match_array("must be unique within the same hierarchy level.")
+ end
+ end
+
+ context "when fields are invalid" do
+ it "is invalid" do
+ [
+ {},
+ { item: nil },
+ { item: high, label: nil },
+ { item: high, label: 42 },
+ { item: high, score: nil },
+ { item: high, score: "pi" }
+ ].each { |params| expect(subject.call(params)).to be_failure }
+ end
+ end
+ end
+ # rubocop:enable Rails/DeprecatedActiveModelErrorsMethods
+end