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

refactor: Integrate PR review comments

Christophe Bliardba96b46f81a2
diff --git a/app/components/work_package_relations_tab/closest_relation.rb b/app/components/work_package_relations_tab/closest_relation.rb
index 18d176dcc8f..aca6408807b 100644
--- a/app/components/work_package_relations_tab/closest_relation.rb
+++ b/app/components/work_package_relations_tab/closest_relation.rb
@@ -35,6 +35,15 @@ module WorkPackageRelationsTab
delegate :predecessor, to: :relation
+ def self.of(work_package, relations)
+ relations
+ .filter { |relation| relation.try(:relation_type_for, work_package) == Relation::TYPE_FOLLOWS }
+ .map { WorkPackageRelationsTab::ClosestRelation.new(it) }
+ .select(&:soonest_start)
+ .max
+ &.relation
+ end
+
def <=>(other)
comparison = compare_nilable_dates(soonest_start, other.soonest_start)
comparison = -compare_nilable_dates(predecessor.created_at, other.predecessor.created_at) if comparison.zero?
diff --git a/app/components/work_package_relations_tab/relation_component.rb b/app/components/work_package_relations_tab/relation_component.rb
index 9c06c92f9d6..912b11f7d77 100644
--- a/app/components/work_package_relations_tab/relation_component.rb
+++ b/app/components/work_package_relations_tab/relation_component.rb
@@ -34,7 +34,7 @@ class WorkPackageRelationsTab::RelationComponent < ApplicationComponent
attr_reader :relation_item, :editable
- delegate :relation, :work_package, to: :relation_item
+ delegate :closest?, :relation, :visible?, :work_package, to: :relation_item
# Checks if the relation or child work package is visible to the current user
#
@@ -52,11 +52,11 @@ class WorkPackageRelationsTab::RelationComponent < ApplicationComponent
end
def child
- if parent_child_relationship?
- relation_item.related
- end
+ relation_item.related if parent_child_relationship?
end
+ def editable? = editable
+
private
def parent_child_relationship? = relation.nil?
@@ -67,7 +67,7 @@ class WorkPackageRelationsTab::RelationComponent < ApplicationComponent
end
def should_render_action_menu?
- return false unless editable
+ return false unless editable?
if parent_child_relationship?
allowed_to_manage_subtasks?
@@ -85,10 +85,6 @@ class WorkPackageRelationsTab::RelationComponent < ApplicationComponent
helpers.current_user.allowed_in_project?(:manage_work_package_relations, work_package.project)
end
- def visible?
- relation_item.visible?
- end
-
def should_display_description?
return false if parent_child_relationship?
@@ -115,10 +111,6 @@ class WorkPackageRelationsTab::RelationComponent < ApplicationComponent
relation.relation_type_for(work_package) == Relation::TYPE_PRECEDES
end
- def closest?
- relation_item.closest?
- end
-
def edit_path
if parent_child_relationship?
raise NotImplementedError, "Children relationships are not editable"
diff --git a/app/components/work_package_relations_tab/relations_mediator.rb b/app/components/work_package_relations_tab/relations_mediator.rb
index 50ccced51a4..59b2195e060 100644
--- a/app/components/work_package_relations_tab/relations_mediator.rb
+++ b/app/components/work_package_relations_tab/relations_mediator.rb
@@ -32,20 +32,10 @@ class WorkPackageRelationsTab::RelationsMediator
RelationGroup = Data.define(:type, :work_package, :visible_relations, :ghost_relations, :closest_relation) do
def initialize(type:, work_package:, visible_relations:, ghost_relations:)
type = ActiveSupport::StringInquirer.new(type.to_s)
- closest_relation = self.class.closest_relation(type, visible_relations + ghost_relations)
+ closest_relation = WorkPackageRelationsTab::ClosestRelation.of(work_package, visible_relations + ghost_relations)
super(type:, work_package:, visible_relations:, ghost_relations:, closest_relation:)
end
- def self.closest_relation(type, relations)
- return nil unless type.follows?
-
- relations
- .map { WorkPackageRelationsTab::ClosestRelation.new(it) }
- .select(&:soonest_start)
- .max
- &.relation
- end
-
def count
visible_relations.count + ghost_relations.count
end
@@ -87,7 +77,7 @@ class WorkPackageRelationsTab::RelationsMediator
# @param work_package [WorkPackage] The work package for which the relation
# is displayed
# @param related [WorkPackage] The related work package: the other work
- # package of therelation, or the child for child relations.
+ # package of the relation, or the child for child relations.
# @param relation [Relation, nil] The relation, `nil` for child relations
# @param visibility [Symbol] The visibility of the relation, `:visible` to
# show related work package information or `:ghost` to show a placeholder
diff --git a/app/models/relation.rb b/app/models/relation.rb
index 9d0bc7e40b9..015e0be9ec7 100644
--- a/app/models/relation.rb
+++ b/app/models/relation.rb
@@ -54,7 +54,8 @@ class Relation < ApplicationRecord
# menu or the relations tab.
TYPES = {
TYPE_RELATES => {
- name: :label_relates_to, sym_name: :label_relates_to, order: 1, sym: TYPE_RELATES
+ name: :label_relates_to, sym_name: :label_relates_to, order: 1,
+ sym: TYPE_RELATES
},
TYPE_FOLLOWS => {
name: :label_follows, sym_name: :label_precedes, order: 7,
@@ -65,14 +66,16 @@ class Relation < ApplicationRecord
sym: TYPE_FOLLOWS, reverse: TYPE_FOLLOWS
},
TYPE_DUPLICATES => {
- name: :label_duplicates, sym_name: :label_duplicated_by, order: 6, sym: TYPE_DUPLICATED
+ name: :label_duplicates, sym_name: :label_duplicated_by, order: 6,
+ sym: TYPE_DUPLICATED
},
TYPE_DUPLICATED => {
name: :label_duplicated_by, sym_name: :label_duplicates, order: 7,
sym: TYPE_DUPLICATES, reverse: TYPE_DUPLICATES
},
TYPE_BLOCKS => {
- name: :label_blocks, sym_name: :label_blocked_by, order: 4, sym: TYPE_BLOCKED
+ name: :label_blocks, sym_name: :label_blocked_by, order: 4,
+ sym: TYPE_BLOCKED
},
TYPE_BLOCKED => {
name: :label_blocked_by, sym_name: :label_blocks, order: 5,
diff --git a/spec/components/work_package_relations_tab/closest_relation_spec.rb b/spec/components/work_package_relations_tab/closest_relation_spec.rb
index 6a838f80b3b..795514e5ac5 100644
--- a/spec/components/work_package_relations_tab/closest_relation_spec.rb
+++ b/spec/components/work_package_relations_tab/closest_relation_spec.rb
@@ -34,12 +34,78 @@ RSpec.describe WorkPackageRelationsTab::ClosestRelation do
let(:work_package) { build_stubbed(:work_package) }
let(:today) { Time.zone.today }
- def closest_relation(lag: 0, **wp_attributes)
+ def follows_relation(lag: 0, **wp_attributes)
predecessor = build_stubbed(:work_package, **wp_attributes)
- relation = build_stubbed(:follows_relation, from: work_package, to: predecessor, lag:)
+ build_stubbed(:follows_relation, from: work_package, to: predecessor, lag:)
+ end
+
+ def precedes_relation(lag: 0, **wp_attributes)
+ successor = build_stubbed(:work_package, **wp_attributes)
+ build_stubbed(:precedes_relation, from: work_package, to: successor, lag:)
+ end
+
+ def relates_relation(**wp_attributes)
+ related = build_stubbed(:work_package, **wp_attributes)
+ build_stubbed(:relates_relation, from: work_package, to: related)
+ end
+
+ def closest_relation(lag: 0, **wp_attributes)
+ relation = follows_relation(lag:, **wp_attributes)
described_class.new(relation)
end
+ describe ".of" do
+ it "returns the closest relation of a list of follows relations" do
+ relations = [
+ follows_relation(due_date: 4.days.ago),
+ follows_relation(due_date: 4.days.ago, lag: 2)
+ ]
+ expect(described_class.of(work_package, relations)).to eq(relations.last)
+ end
+
+ it "ignores relations not being 'follows' relations" do
+ relations = [
+ follows_relation(due_date: 4.days.ago),
+ follows_relation(due_date: 4.days.ago, lag: 2),
+ relates_relation(due_date: today)
+ ]
+ expect(described_class.of(work_package, relations)).to eq(relations.second)
+ end
+
+ it "ignores 'follows' relations not having any dates" do
+ relations = [
+ follows_relation(start_date: nil, due_date: nil)
+ ]
+ expect(described_class.of(work_package, relations)).to be_nil
+ end
+
+ it "ignores child relations" do
+ child_relation = build_stubbed(:work_package, parent: work_package)
+ relations = [
+ child_relation
+ ]
+ expect(described_class.of(work_package, relations)).to be_nil
+ end
+
+ it "distinguishes between 'follows' and 'precedes' relations" do
+ relations = [
+ follows_relation(due_date: 4.days.ago),
+ follows_relation(due_date: 4.days.ago, lag: 2),
+ precedes_relation(due_date: today)
+ ]
+ expect(described_class.of(work_package, relations)).to eq(relations.second)
+ end
+
+ it "returns nil when there no 'follows' relations are given" do
+ relations = [
+ relates_relation(due_date: today),
+ precedes_relation(due_date: today)
+ ]
+ expect(described_class.of(work_package, relations)).to be_nil
+ expect(described_class.of(work_package, [])).to be_nil
+ end
+ end
+
describe "#<=>" do
context "without a lag" do
context "when comparing two instances with different due dates" do
diff --git a/spec/components/work_package_relations_tab/relations_mediator_spec.rb b/spec/components/work_package_relations_tab/relations_mediator_spec.rb
index e583ae853d4..b8e62d9b2ee 100644
--- a/spec/components/work_package_relations_tab/relations_mediator_spec.rb
+++ b/spec/components/work_package_relations_tab/relations_mediator_spec.rb
@@ -136,7 +136,7 @@ RSpec.describe WorkPackageRelationsTab::RelationsMediator do
it "returns all relations of the group as RelationItem instances, " \
"ordered by oldest first (lowest id first), mixing visible and ghost relations" do
relation_group = described_class::RelationGroup.new(
- type: "relates",
+ type: Relation::TYPE_RELATES,
work_package:,
visible_relations: [
build_stubbed(:relates_relation, id: 4, from: work_package),
diff --git a/spec/factories/relation_factory.rb b/spec/factories/relation_factory.rb
index 2e4d9cc852e..88b1fcae8ba 100644
--- a/spec/factories/relation_factory.rb
+++ b/spec/factories/relation_factory.rb
@@ -51,5 +51,19 @@ FactoryBot.define do
relation_type { "follows" }
lag { 0 }
end
+
+ factory :precedes_relation do
+ # Use these transient attributes if you always mix up `from` and `to`
+ # attributes in a precedes relation
+ transient do
+ predecessor { nil }
+ successor { nil }
+ end
+
+ from { predecessor || super() }
+ to { successor || super() }
+ relation_type { "precedes" }
+ lag { 0 }
+ end
end
end