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

[#67973] update insert contract to behave like update

Eric Schubert7845b35cb31a
diff --git a/app/contracts/custom_fields/hierarchy/insert_list_item_contract.rb b/app/contracts/custom_fields/hierarchy/insert_list_item_contract.rb
index 2efc10d6603..4e0fc3ebb7d 100644
--- a/app/contracts/custom_fields/hierarchy/insert_list_item_contract.rb
+++ b/app/contracts/custom_fields/hierarchy/insert_list_item_contract.rb
@@ -31,11 +31,10 @@
module CustomFields
module Hierarchy
class InsertListItemContract < DryApplicationContract
-
params do
required(:parent).filled(type?: CustomField::Hierarchy::Item)
required(:label).filled(:string)
- optional(:short).filled(:string)
+ required(:short).maybe(:string)
end
rule(:parent) do
@@ -52,7 +51,7 @@ module CustomFields
rule(:short) do
next if schema_error?(:parent)
- next unless key?
+ next if value.nil?
key.failure(:not_unique) if values[:parent].children.exists?(short: value)
end
diff --git a/spec/contracts/custom_fields/hierarchy/insert_list_item_contract_spec.rb b/spec/contracts/custom_fields/hierarchy/insert_list_item_contract_spec.rb
index fad946b3584..0f72935454b 100644
--- a/spec/contracts/custom_fields/hierarchy/insert_list_item_contract_spec.rb
+++ b/spec/contracts/custom_fields/hierarchy/insert_list_item_contract_spec.rb
@@ -38,7 +38,7 @@ RSpec.describe CustomFields::Hierarchy::InsertListItemContract do
let(:parent) { create(:hierarchy_item) }
context "when all required fields are valid" do
- let(:params) { { parent:, label: "Valid Label" } }
+ let(:params) { { parent:, label: "Valid Label", short: nil } }
it "is valid" do
result = subject.call(params)
@@ -48,7 +48,7 @@ RSpec.describe CustomFields::Hierarchy::InsertListItemContract do
context "when parent is not of type 'Item'" do
let(:invalid_parent) { create(:custom_field) }
- let(:params) { { parent: invalid_parent, label: "Valid Label" } }
+ let(:params) { { parent: invalid_parent, label: "Valid Label", short: nil } }
it "is invalid" do
result = subject.call(params)
@@ -62,7 +62,7 @@ RSpec.describe CustomFields::Hierarchy::InsertListItemContract do
create(:hierarchy_item, parent:, label: "Duplicate Label")
end
- let(:params) { { parent:, label: "Duplicate Label" } }
+ let(:params) { { parent:, label: "Duplicate Label", short: nil } }
it "is invalid" do
result = subject.call(params)
@@ -132,7 +132,7 @@ RSpec.describe CustomFields::Hierarchy::InsertListItemContract do
it "creates a success result" do
[
{ parent:, label: "A label", short: "A shorthand" },
- { parent:, label: "A label" }
+ { parent:, label: "A label", short: nil }
].each { |params| expect(subject.call(params)).to be_success }
end
end
@@ -140,15 +140,14 @@ RSpec.describe CustomFields::Hierarchy::InsertListItemContract do
context "when inputs are invalid" do
it "creates a failure result" do
[
- { parent:, label: "A label", short: "" },
- { parent:, label: "A label", short: nil },
- { parent:, label: "" },
- { parent:, label: nil },
{ parent: },
- { parent: nil },
- { parent: nil, label: "A label" },
- { parent: "parent", label: "A label" },
- { parent: 42, label: "A label" }
+ { parent:, label: "A label" },
+ { parent:, short: "AL" },
+ { parent: nil, label: "A label", short: nil },
+ { parent: 42, label: "A label", short: nil },
+ { parent:, label: nil, short: nil },
+ { parent:, label: 42, short: nil },
+ { parent:, label: "A label", short: 42 }
].each { |params| expect(subject.call(params)).to be_failure }
end
end