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

Implement RelationGroup#closest_relation

Alexander Brandon Coles79ed18a35d47
diff --git a/app/components/work_package_relations_tab/closest_relation.rb b/app/components/work_package_relations_tab/closest_relation.rb
new file mode 100644
index 00000000000..18d176dcc8f
--- /dev/null
+++ b/app/components/work_package_relations_tab/closest_relation.rb
@@ -0,0 +1,68 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+module WorkPackageRelationsTab
+ class ClosestRelation
+ include Comparable
+
+ attr :relation
+
+ delegate :predecessor, to: :relation
+
+ 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?
+ comparison
+ end
+
+ def initialize(relation)
+ @relation = relation
+ end
+
+ def soonest_start
+ return @soonest_start if defined?(@soonest_start)
+
+ @soonest_start = relation.successor_soonest_start(gap: 0.days)
+ end
+
+ def inspect
+ "#<#{self.class.name} soonest_start: #{soonest_start} relation: #{relation.inspect}>"
+ end
+
+ private
+
+ def compare_nilable_dates(date1, date2)
+ return -1 if date1.nil? && !date2.nil?
+ return 1 if !date1.nil? && date2.nil?
+ return 0 if date1.nil? && date2.nil?
+
+ date1 <=> date2
+ end
+ end
+end
diff --git a/app/components/work_package_relations_tab/relations_mediator.rb b/app/components/work_package_relations_tab/relations_mediator.rb
index a66906f0f10..dcbab66faf8 100644
--- a/app/components/work_package_relations_tab/relations_mediator.rb
+++ b/app/components/work_package_relations_tab/relations_mediator.rb
@@ -38,9 +38,29 @@ class WorkPackageRelationsTab::RelationsMediator
visible_relations.any? || ghost_relations.any?
end
+ def all_relations
+ visible_relations + ghost_relations
+ end
+
+ def closest_relation?(relation) = closest_relation == relation
+
+ def closest_relation = closest_relations.first
+
+ def closest_relations
+ return [] unless follows_type?
+
+ all_relations
+ .sort_by { WorkPackageRelationsTab::ClosestRelation.new(it) }
+ .reverse
+ end
+
def children_type?
type == "children"
end
+
+ def follows_type?
+ type == "follows"
+ end
end
attr_reader :work_package
diff --git a/app/models/relation.rb b/app/models/relation.rb
index af637a0d8de..91671e857ec 100644
--- a/app/models/relation.rb
+++ b/app/models/relation.rb
@@ -165,10 +165,10 @@ class Relation < ApplicationRecord
successor.start_date || successor.due_date
end
- def successor_soonest_start
+ def successor_soonest_start(gap: 1.day)
if follows? && predecessor_date
days = WorkPackages::Shared::Days.for(from)
- relation_start_date = predecessor_date + 1.day
+ relation_start_date = predecessor_date + gap
days.soonest_working_day(relation_start_date, lag:)
end
end
diff --git a/spec/components/work_package_relations_tab/closest_relation_spec.rb b/spec/components/work_package_relations_tab/closest_relation_spec.rb
new file mode 100644
index 00000000000..6a838f80b3b
--- /dev/null
+++ b/spec/components/work_package_relations_tab/closest_relation_spec.rb
@@ -0,0 +1,204 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "rails_helper"
+
+RSpec.describe WorkPackageRelationsTab::ClosestRelation do
+ let(:work_package) { build_stubbed(:work_package) }
+ let(:today) { Time.zone.today }
+
+ def closest_relation(lag: 0, **wp_attributes)
+ predecessor = build_stubbed(:work_package, **wp_attributes)
+ relation = build_stubbed(:follows_relation, from: work_package, to: predecessor, lag:)
+ described_class.new(relation)
+ end
+
+ describe "#<=>" do
+ context "without a lag" do
+ context "when comparing two instances with different due dates" do
+ it "compares with the respective due dates" do
+ expect(closest_relation(due_date: 1.day.from_now)).to be < closest_relation(due_date: 2.days.from_now)
+ expect(closest_relation(due_date: today)).to be > closest_relation(due_date: 2.days.ago)
+ expect(closest_relation(due_date: today)).to be > closest_relation(due_date: nil)
+ expect(closest_relation(due_date: nil)).to be < closest_relation(due_date: 1.day.ago)
+ end
+ end
+
+ context "when comparing two instances with different start dates" do
+ it "compares with the respective start dates" do
+ expect(closest_relation(start_date: 1.day.from_now)).to be < closest_relation(start_date: 2.days.from_now)
+ expect(closest_relation(start_date: today)).to be > closest_relation(start_date: 2.days.ago)
+ expect(closest_relation(start_date: today)).to be > closest_relation(start_date: nil)
+ expect(closest_relation(start_date: nil)).to be < closest_relation(start_date: 1.day.ago)
+ end
+ end
+
+ context "when comparing two instances with same due dates" do
+ it "compares with the respective created at dates, oldest > newest" do
+ expect(closest_relation(due_date: 1.day.from_now)).to be > closest_relation(due_date: 1.day.from_now)
+ expect(closest_relation(due_date: 2.days.ago)).to be > closest_relation(due_date: 2.days.ago)
+ expect(closest_relation(due_date: nil)).to be > closest_relation(due_date: nil)
+ end
+ end
+
+ context "when comparing two instances with same start dates" do
+ it "compares with the respective created at dates, oldest > newest" do
+ expect(closest_relation(start_date: 1.day.from_now)).to be > closest_relation(start_date: 1.day.from_now)
+ expect(closest_relation(start_date: 2.days.ago)).to be > closest_relation(start_date: 2.days.ago)
+ expect(closest_relation(start_date: nil)).to be > closest_relation(start_date: nil)
+ end
+ end
+
+ context "when comparing two instances with both due and start dates set" do
+ it "compares with the respective due dates, ignoring start dates" do
+ expect(closest_relation(due_date: 10.days.from_now, start_date: today))
+ .to be < closest_relation(due_date: 12.days.from_now, start_date: 2.days.ago)
+ expect(closest_relation(due_date: 14.days.from_now, start_date: today))
+ .to be > closest_relation(due_date: 12.days.from_now, start_date: 2.days.ago)
+ end
+ end
+ end
+
+ context "with a lag" do
+ context "when comparing two instances with same soonest starts (due date and lag)" do
+ it "compares with the respective created at dates, oldest > newest" do
+ expect(closest_relation(lag: 3, due_date: 3.days.from_now))
+ .to be > closest_relation(lag: 4, due_date: 2.days.from_now)
+ expect(closest_relation(lag: 2, due_date: 3.days.ago))
+ .to be > closest_relation(lag: 1, due_date: 4.days.ago)
+ expect(closest_relation(lag: 2, due_date: nil))
+ .to be > closest_relation(lag: 2, due_date: nil)
+ end
+ end
+
+ context "when comparing two instances with same soonest starts (start date and lag)" do
+ it "compares with the respective created at dates, oldest > newest" do
+ expect(closest_relation(lag: 3, start_date: 3.days.from_now))
+ .to be > closest_relation(lag: 4, start_date: 2.days.from_now)
+ expect(closest_relation(lag: 2, start_date: 3.days.ago))
+ .to be > closest_relation(lag: 1, start_date: 4.days.ago)
+ expect(closest_relation(lag: 2, start_date: nil))
+ .to be > closest_relation(lag: 2, start_date: nil)
+ end
+ end
+
+ context "when comparing two instances with different soonest starts (due date and lag)" do
+ it "compares with the combined due dates and lag" do
+ expect(closest_relation(lag: 4, due_date: 3.days.from_now))
+ .to be > closest_relation(lag: 4, due_date: 2.days.from_now)
+ expect(closest_relation(lag: 1, due_date: 3.days.ago))
+ .to be < closest_relation(lag: 4, due_date: 3.days.ago)
+ expect(closest_relation(lag: 4, due_date: nil))
+ .to be < closest_relation(lag: 4, due_date: 2.days.from_now)
+ expect(closest_relation(lag: 1, due_date: 3.days.ago))
+ .to be > closest_relation(lag: 4, due_date: nil)
+ end
+ end
+
+ context "when comparing two instances with different soonest starts (start date and lag)" do
+ it "compares with the combined start dates and lag" do
+ expect(closest_relation(lag: 4, start_date: 3.days.from_now))
+ .to be > closest_relation(lag: 4, start_date: 2.days.from_now)
+ expect(closest_relation(lag: 1, start_date: 3.days.ago))
+ .to be < closest_relation(lag: 4, start_date: 3.days.ago)
+ expect(closest_relation(lag: 4, start_date: nil))
+ .to be < closest_relation(lag: 4, start_date: 2.days.from_now)
+ expect(closest_relation(lag: 1, start_date: 3.days.ago))
+ .to be > closest_relation(lag: 4, start_date: nil)
+ end
+ end
+ end
+ end
+
+ describe "#soonest_start" do
+ context "with a nil due and start date" do
+ it "returns nil" do
+ expect(closest_relation(due_date: nil, start_date: nil).soonest_start).to be_nil
+ expect(closest_relation(lag: 1, due_date: nil, start_date: nil).soonest_start).to be_nil
+ end
+ end
+
+ context "with a due date set" do
+ let(:due_date) { Date.new(2020, 7, 14) }
+
+ context "without a lag" do
+ it "returns the due date" do
+ expect(closest_relation(due_date:).soonest_start).to eq(Date.new(2020, 7, 14))
+ end
+ end
+
+ context "with a positive lag" do
+ it "returns the combined due date and lag" do
+ expect(closest_relation(lag: 11, due_date:).soonest_start).to eq(Date.new(2020, 7, 25))
+ end
+ end
+
+ context "with a negative lag" do
+ it "returns the combined due date and lag" do
+ pending "negative lags are not yet implemented"
+ expect(closest_relation(lag: -2, due_date:).soonest_start).to eq(Date.new(2020, 7, 12))
+ end
+ end
+ end
+
+ context "with a start date set" do
+ let(:start_date) { Date.new(2020, 7, 14) }
+
+ context "with a zero lag" do
+ it "returns the start date" do
+ expect(closest_relation(start_date:).soonest_start).to eq(Date.new(2020, 7, 14))
+ end
+ end
+
+ context "with a positive lag" do
+ it "returns the combined start date and lag" do
+ expect(closest_relation(lag: 11, start_date:).soonest_start).to eq(Date.new(2020, 7, 25))
+ end
+ end
+
+ context "with a negative lag" do
+ it "returns the combined start date and lag" do
+ pending "negative lags are not yet implemented (Feature OP#38606)"
+ expect(closest_relation(lag: -2, start_date:).soonest_start).to eq(Date.new(2020, 7, 12))
+ end
+ end
+ end
+ end
+
+ describe "#inspect" do
+ subject { closest_relation(lag: 3, due_date: Date.new(2022, 7, 4)) }
+
+ it "outputs object for debugging" do
+ expect(subject.inspect)
+ .to start_with("#<WorkPackageRelationsTab::ClosestRelation soonest_start: 2022-07-07")
+ .and include(subject.relation.inspect)
+ end
+ end
+end
diff --git a/spec/components/work_package_relations_tab/relations_mediator_spec.rb b/spec/components/work_package_relations_tab/relations_mediator_spec.rb
new file mode 100644
index 00000000000..e18351bdc01
--- /dev/null
+++ b/spec/components/work_package_relations_tab/relations_mediator_spec.rb
@@ -0,0 +1,77 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "rails_helper"
+
+RSpec.describe WorkPackageRelationsTab::RelationsMediator do
+ shared_let(:user) { create(:admin) }
+ shared_let(:work_package) { create(:work_package) }
+
+ current_user { user }
+
+ subject(:mediator) { described_class.new(work_package:) }
+
+ shared_let_work_packages(<<~TABLE)
+ hierarchy | MTWTFSS | scheduling mode | predecessors
+ predecessor1 | XXX | manual |
+ predecessor2 | XX | manual |
+ predecessor3 | XX | manual |
+ predecessor4 | | manual |
+ work_package | X | automatic | predecessor1 with lag 2, predecessor2 with lag 7, predecessor3 with lag 7, predecessor4 with lag 10
+ TABLE
+
+ describe "RelationGroup" do
+ let(:group) { mediator.relation_group("follows") }
+
+ describe "#closest_relation" do
+ it "returns the closest follows relation" do
+ expect(group.closest_relation).to eq _table.relation(predecessor: predecessor2)
+ end
+ end
+
+ describe "#closest_relation?(relation)" do
+ it "returns true if the given relation is the closest one, false otherwise" do
+ expect(group.closest_relation?(_table.relation(predecessor: predecessor2))).to be true
+ expect(group.closest_relation?(_table.relation(predecessor: predecessor1))).to be false
+ end
+ end
+
+ describe "#closest_relations" do
+ it "returns all follows relations with the closest first" do
+ expect(group.closest_relations).to eq [
+ _table.relation(predecessor: predecessor2),
+ _table.relation(predecessor: predecessor3),
+ _table.relation(predecessor: predecessor1),
+ _table.relation(predecessor: predecessor4)
+ ]
+ end
+ end
+ end
+end