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

Code improvements

Henriette Dargebe18ff082a16
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 14ad7e21fcc..0f4509cc23a 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
@@ -48,23 +48,26 @@ class WorkPackageRelationsTab::WorkPackageRelationFormComponent < ApplicationCom
def related_work_package
@related_work_package ||= begin
- related = @relation.to
# We cannot rely on the related WorkPackage being the "to",
# depending on the relation it can also be "from"
- related.present? && related.id == @work_package.id ? @relation.from : related
+ relation_to_matches_wp? ? @relation.from : @relation.to
end
end
def displayable_field_value
return nil if related_work_package.nil?
- if @relation.to.present?
+ if relation_to_matches_wp?
"#{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
+ relation_to_matches_wp? ? :from_id : :to_id
+ end
+
+ def relation_to_matches_wp?
+ @relation.to == @work_package
end
def submit_url_options
diff --git a/app/contracts/relations/base_contract.rb b/app/contracts/relations/base_contract.rb
index c334dc2263a..cf6d293eb68 100644
--- a/app/contracts/relations/base_contract.rb
+++ b/app/contracts/relations/base_contract.rb
@@ -36,7 +36,7 @@ module Relations
attribute :from
attribute :to
- validate :manage_relations_permission?
+ validate :validate_user_allowed
validate :validate_from_exists
validate :validate_to_exists
validate :validate_nodes_relatable
@@ -61,11 +61,10 @@ module Relations
# the to_id is not set
# 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? || model.from_id.nil?
+ # the error is added by the `validate_from_exists` and `validate_to_exists` methods
+ return if to_id_or_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?
+ if relation_changed? && circular_dependency?
errors.add :base, I18n.t(:"activerecord.errors.messages.circular_dependency")
end
end
@@ -76,19 +75,31 @@ module Relations
errors.add :relation_type, :inclusion
end
- def manage_relations_permission?
+ def validate_user_allowed
+ return if model.to_id.nil? || model.from_id.nil?
+
unless manage_relations?
errors.add :base, :error_unauthorized
end
end
+ def to_id_or_from_id_nil?
+ model.to_id.nil? || model.from_id.nil?
+ end
+
+ def relation_changed?
+ model.from_id_changed? || model.to_id_changed?
+ end
+
+ def circular_dependency?
+ WorkPackage.relatable(model.from, model.relation_type, ignored_relation: model).where(id: model.to_id).empty?
+ end
+
def visible_work_packages
::WorkPackage.visible(user)
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_children_relations_controller.rb b/app/controllers/work_package_children_relations_controller.rb
index 0d722196676..4feff06746b 100644
--- a/app/controllers/work_package_children_relations_controller.rb
+++ b/app/controllers/work_package_children_relations_controller.rb
@@ -43,14 +43,7 @@ class WorkPackageChildrenRelationsController < ApplicationController
end
def create
- if params[:work_package][:id].present?
- child = WorkPackage.find(params[:work_package][:id])
- service_result = set_relation(child:, parent: @work_package)
- else
- child = WorkPackage.new
- child.errors.add(:id, :blank)
- service_result = ServiceResult.failure(result: child)
- end
+ service_result = create_service_result
if service_result.failure?
update_via_turbo_stream(
@@ -73,6 +66,17 @@ class WorkPackageChildrenRelationsController < ApplicationController
private
+ def create_service_result
+ if params[:work_package][:id].present?
+ child = WorkPackage.find(params[:work_package][:id])
+ set_relation(child:, parent: @work_package)
+ else
+ child = WorkPackage.new
+ child.errors.add(:id, :blank)
+ ServiceResult.failure(result: child)
+ end
+ end
+
def set_relation(child:, parent:)
WorkPackages::UpdateService.new(user: current_user, model: child)
.call(parent:)
diff --git a/config/locales/en.yml b/config/locales/en.yml
index f30e20e7e3c..5e490f1b8ae 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1302,11 +1302,11 @@ en:
circular_dependency: "The relationship creates a circle of relationships."
attributes:
to_id:
- error_not_found: "not found or not visible"
- error_readonly: "an existing relation's `to` link is immutable"
+ error_not_found: "not found or not visible."
+ error_readonly: "cannot be changed for existing relations."
from_id:
- error_not_found: "not found or not visible"
- error_readonly: "an existing relation's `from` link is immutable"
+ error_not_found: "not found or not visible."
+ error_readonly: "cannot be changed for existing relations."
lag:
greater_than_or_equal_to:
zero: "cannot be negative."
diff --git a/spec/requests/api/v3/relations/relations_api_spec.rb b/spec/requests/api/v3/relations/relations_api_spec.rb
index bfb09356e47..a302c3d0618 100644
--- a/spec/requests/api/v3/relations/relations_api_spec.rb
+++ b/spec/requests/api/v3/relations/relations_api_spec.rb
@@ -303,7 +303,7 @@ RSpec.describe "API v3 Relation resource", content_type: :json do
it "lets the user know the attribute is read-only" do
msg = JSON.parse(last_response.body)["message"]
- expect(msg).to include "Related work package an existing relation's `from` link is immutable"
+ expect(msg).to include "Related work package cannot be changed for existing relations."
end
end
end