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 contracts to handle unique shorts and also I18n error messages

Marcello Rochacd9b5cd14d36
diff --git a/app/contracts/custom_fields/hierarchy/insert_item_contract.rb b/app/contracts/custom_fields/hierarchy/insert_item_contract.rb
index d633761738c..67c171a753a 100644
--- a/app/contracts/custom_fields/hierarchy/insert_item_contract.rb
+++ b/app/contracts/custom_fields/hierarchy/insert_item_contract.rb
@@ -40,13 +40,21 @@ module CustomFields
end
rule(:parent) do
+ next if schema_error?(:parent)
+
key.failure("must exist") unless value.persisted?
end
rule(:label) do
- if CustomField::Hierarchy::Item.exists?(parent_id: values[:parent], label: value)
- key.failure(:not_unique)
- end
+ next if schema_error?(:parent)
+
+ key.failure(:not_unique) if values[:parent].children.exists?(label: value)
+ end
+
+ rule(:short) do
+ next if schema_error?(:parent)
+
+ key.failure(:not_unique) if values[:parent].children.exists?(short: value)
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 8899cfb8a44..01c12c87340 100644
--- a/app/contracts/custom_fields/hierarchy/update_item_contract.rb
+++ b/app/contracts/custom_fields/hierarchy/update_item_contract.rb
@@ -31,6 +31,8 @@
module CustomFields
module Hierarchy
class UpdateItemContract < Dry::Validation::Contract
+ config.messages.backend = :i18n
+
params do
required(:item).filled(type?: CustomField::Hierarchy::Item)
optional(:label).filled(:string)
@@ -38,16 +40,20 @@ module CustomFields
end
rule(:item) do
- key.failure("must exist") if value.new_record?
- key.failure("must not be a root item") if value.root?
+ key.failure(:not_persisted) if value.new_record?
+ key.failure(:root_item) if value.root?
end
rule(:label) do
- next unless key?
+ next if schema_error?(:item)
+
+ key.failure(:not_unique) if values[:item].siblings.exists?(label: value)
+ end
+
+ rule(:short) do
+ next if schema_error?(:item)
- if values[:item].siblings.where(label: value).any?
- key.failure("must be unique at the same hierarchical level")
- end
+ key.failure(:not_unique) if values[:item].siblings.exists?(short: value)
end
end
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index d59da529359..53ee8683538 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -299,8 +299,16 @@ en:
label:
filled?: "must be filled"
not_unique: "must be unique within the same hierarchy level"
+ short:
+ not_unique: "must be unique within the same hierarchy level"
+ item:
+ root_item: "cannot be a root item"
+ not_persisted: "must be an already existing item"
rules:
label: "Label"
+ short: "Short"
+ item: "Item"
+ parent: "Item"
global_search:
placeholder: "Search in %{app_title}"
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 900e519481c..363a36d6f41 100644
--- a/spec/contracts/custom_fields/hierarchy/insert_item_contract_spec.rb
+++ b/spec/contracts/custom_fields/hierarchy/insert_item_contract_spec.rb
@@ -81,6 +81,18 @@ RSpec.describe CustomFields::Hierarchy::InsertItemContract do
end
end
+ context "when short is not unique in the same hierarchy level" do
+ let(:params) { { parent:, label: "Valid Label", short: "Repeated Short" } }
+
+ before { create(:hierarchy_item, parent:, label: "Unique Label", short: "Repeated Short") }
+
+ it "is invalid with localized validation errors" do
+ result = subject.call(params)
+ expect(result).to be_failure
+ expect(result.errors.to_h).to include(short: ["must be unique within the same hierarchy level"])
+ end
+ end
+
context "when short is set and is a string" do
let(:params) { { parent:, label: "Valid Label", short: "Valid Short" } }
diff --git a/spec/contracts/custom_fields/hierarchy/update_item_contract_spec.rb b/spec/contracts/custom_fields/hierarchy/update_item_contract_spec.rb
index 53011a43046..49a40b28674 100644
--- a/spec/contracts/custom_fields/hierarchy/update_item_contract_spec.rb
+++ b/spec/contracts/custom_fields/hierarchy/update_item_contract_spec.rb
@@ -35,14 +35,9 @@ RSpec.describe CustomFields::Hierarchy::UpdateItemContract do
# rubocop:disable Rails/DeprecatedActiveModelErrorsMethods
describe "#call" do
- let(:vader) { create(:hierarchy_item) }
- let(:luke) { create(:hierarchy_item, label: "luke", short: "ls", parent: vader) }
- let(:leia) { create(:hierarchy_item, label: "leia", short: "lo", parent: vader) }
-
- before do
- luke
- leia
- end
+ let!(:vader) { create(:hierarchy_item) }
+ let!(:luke) { create(:hierarchy_item, label: "luke", short: "ls", parent: vader) }
+ let!(:leia) { create(:hierarchy_item, label: "leia", short: "lo", parent: vader) }
context "when all required fields are valid" do
it "is valid" do
@@ -51,7 +46,7 @@ RSpec.describe CustomFields::Hierarchy::UpdateItemContract do
{ item: luke, label: "Luke Skywalker" },
{ item: luke, label: "luke", short: "lu" },
{ item: luke, short: "LS" },
- { item: luke, short: "lo" },
+ { item: luke, short: "ls" },
{ item: luke }
].each { |params| expect(subject.call(params)).to be_success }
end
@@ -63,7 +58,7 @@ RSpec.describe CustomFields::Hierarchy::UpdateItemContract do
it("is invalid") do
result = subject.call(params)
expect(result).to be_failure
- expect(result.errors.to_h).to include(item: ["must not be a root item"])
+ expect(result.errors.to_h).to include(item: ["cannot be a root item"])
end
end
@@ -85,7 +80,7 @@ RSpec.describe CustomFields::Hierarchy::UpdateItemContract do
it "is invalid" do
result = subject.call(params)
expect(result).to be_failure
- expect(result.errors.to_h).to include(item: ["must exist"])
+ expect(result.errors[:item]).to match_array("must be an already existing item")
end
end
@@ -95,7 +90,22 @@ RSpec.describe CustomFields::Hierarchy::UpdateItemContract 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[:label]).to match_array("must be unique within the same hierarchy level")
+ end
+ end
+
+ context "when the short already exist in the same hierarchy level" do
+ let(:params) { { item: luke, short: "lo" } }
+
+ it "is invalid" do
+ result = subject.call(params)
+
+ expect(result).to be_failure
+
+ p result
+
+ expect(result.errors[:short]).to match_array("must be unique within the same hierarchy level")
end
end