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

Show error messages for both `from_id` and `to_id` fields in the relations dialog

Henriette Darge7a243a54cf31
diff --git a/app/components/work_package_relations_tab/work_package_relation_form_component.html.erb b/app/components/work_package_relations_tab/work_package_relation_form_component.html.erb
index a9bb8a96c45..bfd9090c7e0 100644
--- a/app/components/work_package_relations_tab/work_package_relation_form_component.html.erb
+++ b/app/components/work_package_relations_tab/work_package_relation_form_component.html.erb
@@ -20,15 +20,16 @@
# so we need to re-define them here. Figure out solution for this.
relation = @relation
lag_shown = show_lag?
- to_id_field_value = relation.to.present? ? "#{related_work_package.type.name.upcase} ##{related_work_package.id} - #{related_work_package.subject}" : nil
+ field_value = displayable_field_value
+ relation_direction = direction
url = ::API::V3::Utilities::PathHelper::ApiV3Path.work_package_available_relation_candidates(@work_package.id, type: relation.relation_type_for(@work_package))
render_inline_form(f) do |my_form|
if relation.persisted?
my_form.text_field(
- name: :to_id,
+ name: relation_direction,
label: WorkPackage.model_name.human,
visually_hide_label: false,
- value: to_id_field_value,
+ value: field_value,
readonly: true
)
else
@@ -38,7 +39,7 @@
)
my_form.autocompleter(
- name: :to_id,
+ name: relation_direction,
label: WorkPackage.model_name.human,
required: true,
visually_hide_label: false,
diff --git a/app/components/work_package_relations_tab/work_package_relation_form_component.rb b/app/components/work_package_relations_tab/work_package_relation_form_component.rb
index 264d08770fa..14ad7e21fcc 100644
--- a/app/components/work_package_relations_tab/work_package_relation_form_component.rb
+++ b/app/components/work_package_relations_tab/work_package_relation_form_component.rb
@@ -51,10 +51,22 @@ class WorkPackageRelationsTab::WorkPackageRelationFormComponent < ApplicationCom
related = @relation.to
# We cannot rely on the related WorkPackage being the "to",
# depending on the relation it can also be "from"
- related.id == @work_package.id ? @relation.from : related
+ related.present? && related.id == @work_package.id ? @relation.from : related
end
end
+ def displayable_field_value
+ return nil if related_work_package.nil?
+
+ if @relation.to.present?
+ "#{related_work_package.type.name.upcase} ##{related_work_package.id} - #{related_work_package.subject}"
+ end
+ end
+
+ def direction
+ @relation.to.present? ? :from_id : :to_id
+ end
+
def submit_url_options
if @relation.persisted?
{ method: :patch,
@@ -66,6 +78,6 @@ class WorkPackageRelationsTab::WorkPackageRelationFormComponent < ApplicationCom
end
def show_lag?
- @relation.relation_type == Relation::TYPE_PRECEDES || @relation.relation_type == Relation::TYPE_FOLLOWS
+ [Relation::TYPE_PRECEDES, Relation::TYPE_FOLLOWS].include?(@relation.relation_type)
end
end
diff --git a/app/contracts/relations/base_contract.rb b/app/contracts/relations/base_contract.rb
index 9970593a8eb..c334dc2263a 100644
--- a/app/contracts/relations/base_contract.rb
+++ b/app/contracts/relations/base_contract.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -47,11 +49,11 @@ module Relations
private
def validate_from_exists
- errors.add :from, :error_not_found unless visible_work_packages.exists? model.from_id
+ errors.add :from_id, :error_not_found unless visible_work_packages.exists? model.from_id
end
def validate_to_exists
- errors.add :to, :error_not_found unless visible_work_packages.exists? model.to_id
+ errors.add :to_id, :error_not_found unless visible_work_packages.exists? model.to_id
end
def validate_nodes_relatable
@@ -60,7 +62,7 @@ module Relations
# in this case we only want to show the "WorkPackage can't be blank" error instead of a
# misleading circular dependencies error
# the error is added by the models presence validation
- return if model.to_id.nil?
+ return if model.to_id.nil? || model.from_id.nil?
if (model.from_id_changed? || model.to_id_changed?) &&
WorkPackage.relatable(model.from, model.relation_type, ignored_relation: model).where(id: model.to_id).empty?
@@ -85,6 +87,8 @@ module Relations
end
def manage_relations?
+ return true if model.from.nil?
+
user.allowed_in_work_package?(:manage_work_package_relations, model.from)
end
end
diff --git a/app/controllers/work_package_relations_controller.rb b/app/controllers/work_package_relations_controller.rb
index dbdef5d38a3..2af79aac70c 100644
--- a/app/controllers/work_package_relations_controller.rb
+++ b/app/controllers/work_package_relations_controller.rb
@@ -59,7 +59,7 @@ class WorkPackageRelationsController < ApplicationController
service_result = Relations::CreateService.new(user: current_user)
.call(create_relation_params)
- unless service_result.success?
+ if service_result.failure?
update_via_turbo_stream(
component: WorkPackageRelationsTab::WorkPackageRelationFormComponent.new(work_package: @work_package,
relation: service_result.result,
diff --git a/app/models/relation.rb b/app/models/relation.rb
index 5b9875e920c..1cad5d0bc23 100644
--- a/app/models/relation.rb
+++ b/app/models/relation.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -30,22 +32,22 @@ class Relation < ApplicationRecord
belongs_to :from, class_name: "WorkPackage"
belongs_to :to, class_name: "WorkPackage"
- TYPE_RELATES = "relates".freeze
- TYPE_PRECEDES = "precedes".freeze
- TYPE_FOLLOWS = "follows".freeze
- TYPE_BLOCKS = "blocks".freeze
- TYPE_BLOCKED = "blocked".freeze
- TYPE_DUPLICATES = "duplicates".freeze
- TYPE_DUPLICATED = "duplicated".freeze
- TYPE_INCLUDES = "includes".freeze
- TYPE_PARTOF = "partof".freeze
- TYPE_REQUIRES = "requires".freeze
- TYPE_REQUIRED = "required".freeze
+ TYPE_RELATES = "relates"
+ TYPE_PRECEDES = "precedes"
+ TYPE_FOLLOWS = "follows"
+ TYPE_BLOCKS = "blocks"
+ TYPE_BLOCKED = "blocked"
+ TYPE_DUPLICATES = "duplicates"
+ TYPE_DUPLICATED = "duplicated"
+ TYPE_INCLUDES = "includes"
+ TYPE_PARTOF = "partof"
+ TYPE_REQUIRES = "requires"
+ TYPE_REQUIRED = "required"
# The parent/child relation is maintained separately
# (in WorkPackage and WorkPackageHierarchy) and a relation cannot
# have the type 'parent' but this is abstracted to simplify the code.
- TYPE_PARENT = "parent".freeze
- TYPE_CHILD = "child".freeze
+ TYPE_PARENT = "parent"
+ TYPE_CHILD = "child"
TYPES = {
TYPE_RELATES => {
@@ -109,8 +111,7 @@ class Relation < ApplicationRecord
greater_than_or_equal_to: 0
}
- # The primer form depends on to_id being validated
- validates :to_id, uniqueness: { scope: :from }, presence: true
+ validates :to, uniqueness: { scope: :from }
before_validation :reverse_if_needed
diff --git a/config/locales/en.yml b/config/locales/en.yml
index e075a6954ea..2bd3404b2f3 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1301,11 +1301,11 @@ en:
typed_dag:
circular_dependency: "The relationship creates a circle of relationships."
attributes:
- to:
- error_not_found: "work package in `to` position not found or not visible"
+ to_id:
+ error_not_found: "work package not found or not visible"
error_readonly: "an existing relation's `to` link is immutable"
- from:
- error_not_found: "work package in `from` position not found or not visible"
+ from_id:
+ error_not_found: "work package not found or not visible"
error_readonly: "an existing relation's `from` link is immutable"
lag:
greater_than_or_equal_to: