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

Can reduce dates to work packages dates with Chart#compact_dates

Christophe Bliardbeb08d0fc06d
diff --git a/spec/support/schedule_helpers/chart.rb b/spec/support/schedule_helpers/chart.rb
index 61a325defb0..f6907f47c15 100644
--- a/spec/support/schedule_helpers/chart.rb
+++ b/spec/support/schedule_helpers/chart.rb
@@ -28,16 +28,24 @@
module ScheduleHelpers
# Contains work packages and relations information from a chart
- # representation.
+ # representation, and information to render it.
#
# The work package information are:
# * subject
+ # * parent
# * start_date
# * due_date
+ # * duration
+ # * ignore_non_working_days
#
# The relations information are limited to follows relations and are retrieved
# with +#predecessors_by_follower+
#
+ # The rendering information are:
+ # * chart origin (the monday displayed in the header line)
+ # * max and min date
+ # * first column size
+ #
# The chart uses different symbols in the timeline to represent a work package
# start and due dates:
# * +X+: a day of the work package duration. The first +X+ is the start date,
@@ -71,6 +79,7 @@ module ScheduleHelpers
# duplicates the chart with different representation properties
def with(order: work_package_names, id_column_size: self.id_column_size, first_day: self.first_day, last_day: self.last_day)
chart = Chart.new
+ order = order.map(&:to_sym)
extra_names = work_package_names - order
chart.work_packages_attributes = work_packages_attributes.index_by { _1[:name] }.values_at(*(order + extra_names)).compact
chart.monday = monday
@@ -125,6 +134,8 @@ module ScheduleHelpers
end
def add_work_package(attributes)
+ attributes[:start_date] ||= WorkPackage.column_defaults["start_date"]
+ attributes[:due_date] ||= WorkPackage.column_defaults["due_date"]
extend_calendar_range(*attributes.values_at(:start_date, :due_date))
extend_id_column_size(*attributes.values_at(:subject))
work_packages_attributes << attributes.merge(name: attributes[:subject].to_sym)
@@ -175,6 +186,13 @@ module ScheduleHelpers
representer.to_s
end
+ def compact_dates
+ @first_day, @last_day = work_packages_attributes.pluck(:start_date, :due_date).flatten.compact.minmax
+ @monday = ([@first_day, @last_day, @monday].compact.first - 1).next_occurring(:monday)
+ extend_calendar_range(@monday, @monday + 6)
+ self
+ end
+
protected
attr_writer :work_packages_attributes,
diff --git a/spec/support_spec/schedule_helpers/chart_spec.rb b/spec/support_spec/schedule_helpers/chart_spec.rb
index 3b66ca4b737..111be885939 100644
--- a/spec/support_spec/schedule_helpers/chart_spec.rb
+++ b/spec/support_spec/schedule_helpers/chart_spec.rb
@@ -47,10 +47,20 @@ describe ScheduleHelpers::Chart do
end
describe '#first_day' do
- context 'without work packages' do
- it 'returns the first day represented on the graph, which is next Monday' do
- expect(chart.first_day).to eq(monday)
- end
+ it 'returns the first day represented on the graph, which is next Monday' do
+ expect(chart.first_day).to eq(monday)
+ end
+
+ it 'can be set to an earlier date by setting the origin monday to an earlier date' do
+ expect(chart.first_day).to eq(monday)
+
+ # no change when origin is moved forward
+ expect { chart.monday = monday + 14.days }
+ .not_to change(chart, :first_day)
+
+ # change when origin is moved backward
+ expect { chart.monday = monday - 14.days }
+ .to change(chart, :first_day).to(monday - 14.days)
end
context 'with work packages' do
@@ -70,25 +80,23 @@ describe ScheduleHelpers::Chart do
expect(chart.first_day).to eq(monday - 6.days)
end
end
+ end
- it 'can be set to an earlier date by setting the origin monday to an earlier date' do
- expect(chart.first_day).to eq(monday)
+ describe '#last_day' do
+ it 'returns the last day represented on the graph, which is the Sunday following origin Monday' do
+ expect(chart.last_day).to eq(sunday)
+ end
- # no change when origin is moved forward
- expect { chart.monday = monday + 14.days }
- .not_to change(chart, :first_day)
+ it 'can be set to an later date by setting the origin Monday to a later date' do
+ expect(chart.last_day).to eq(sunday)
- # change when origin is moved backward
+ # no change when origin is moved backward
expect { chart.monday = monday - 14.days }
- .to change(chart, :first_day).to(monday - 14.days)
- end
- end
+ .not_to change(chart, :last_day)
- describe '#last_day' do
- context 'without work packages' do
- it 'returns the last day represented on the graph, which is the Sunday following origin Monday' do
- expect(chart.last_day).to eq(sunday)
- end
+ # change when origin is moved forward
+ expect { chart.monday = monday + 14.days }
+ .to change(chart, :last_day).to(sunday + 14.days)
end
context 'with work packages' do
@@ -105,17 +113,33 @@ describe ScheduleHelpers::Chart do
expect(chart.last_day).to eq(monday + 20.days)
end
end
+ end
- it 'can be set to an later date by setting the origin Monday to a later date' do
- expect(chart.last_day).to eq(sunday)
+ describe '#compact_dates' do
+ it 'makes the chart dates fit with the work packages dates' do
+ chart.add_work_package(subject: 'wp1', start_date: friday - 21.days, due_date: tuesday - 14.days)
+ chart.add_work_package(subject: 'wp2', start_date: wednesday - 14.days)
+ chart.add_work_package(subject: 'wp3', due_date: thursday - 14.days)
+ chart.add_work_package(subject: 'wp4', due_date: thursday - 14.days)
+
+ expect { chart.compact_dates }
+ .to change { [chart.monday, chart.first_day, chart.last_day] }
+ .from([monday, friday - 21.days, sunday])
+ .to([monday - 14.days, friday - 21.days, sunday - 14.days])
+ end
- # no change when origin is moved backward
- expect { chart.monday = monday - 14.days }
- .not_to change(chart, :last_day)
+ it 'does nothing if there are no work packages' do
+ expect { chart.compact_dates }
+ .not_to change { [chart.monday, chart.first_day, chart.last_day] }
+ end
- # change when origin is moved forward
- expect { chart.monday = monday + 14.days }
- .to change(chart, :last_day).to(sunday + 14.days)
+ it 'does nothing if none of the work packages have any dates' do
+ chart.add_work_package(subject: 'wp1')
+ chart.add_work_package(subject: 'wp2')
+ chart.add_work_package(subject: 'wp3')
+
+ expect { chart.compact_dates }
+ .not_to change { [chart.monday, chart.first_day, chart.last_day] }
end
end