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

add validation to relations to check if to and from are selected

Behrokh Satarnejad3e96e4e46032
diff --git a/app/contracts/relations/base_contract.rb b/app/contracts/relations/base_contract.rb
index 82c4d5edc2e..1caaaa0ca6f 100644
--- a/app/contracts/relations/base_contract.rb
+++ b/app/contracts/relations/base_contract.rb
@@ -55,7 +55,7 @@ module Relations
end
def validate_nodes_relatable
- if (model.from_id_changed? || model.to_id_changed?) &&
+ if (model.from_id_changed? || model.to_id_changed?) && (model.from_id.present? && model.to_id.present?) &&
WorkPackage.relatable(model.from, model.relation_type, ignored_relation: model).where(id: model.to_id).empty?
errors.add :base, I18n.t(:"activerecord.errors.messages.circular_dependency")
end
diff --git a/app/models/relation.rb b/app/models/relation.rb
index aede485a7d8..0e195286d7c 100644
--- a/app/models/relation.rb
+++ b/app/models/relation.rb
@@ -105,7 +105,9 @@ class Relation < ApplicationRecord
validates :lag, numericality: { allow_nil: true }
- validates :to, uniqueness: { scope: :from }
+ validates :to, presence: true, uniqueness: { scope: :from }
+
+ validates :from, presence: true
before_validation :reverse_if_needed
diff --git a/spec/contracts/relations/shared_contract_examples.rb b/spec/contracts/relations/shared_contract_examples.rb
index 8511d4a368b..a880c3eb949 100644
--- a/spec/contracts/relations/shared_contract_examples.rb
+++ b/spec/contracts/relations/shared_contract_examples.rb
@@ -115,6 +115,18 @@ RSpec.shared_examples_for "relation contract" do
it_behaves_like "contract is invalid", from: :error_not_found
end
+ context "when no from value is set" do
+ let(:relation_from) { nil }
+
+ it_behaves_like "contract is invalid", from: :error_not_found
+ end
+
+ context "when no to value is set" do
+ let(:relation_to) { nil }
+
+ it_behaves_like "contract is invalid", to: :error_not_found
+ end
+
context "when the work package for to is not visible" do
let(:relation_to_visible) { false }