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

Schedule helpers can display a self representation

Christophe Bliard54e2a4302b6b
diff --git a/app/services/work_packages/shared/all_days.rb b/app/services/work_packages/shared/all_days.rb
index 662848168b2..d8089b796ea 100644
--- a/app/services/work_packages/shared/all_days.rb
+++ b/app/services/work_packages/shared/all_days.rb
@@ -48,6 +48,10 @@ module WorkPackages
current - previous
end
+ def working?(_date)
+ true
+ end
+
private
def no_duration
diff --git a/app/services/work_packages/shared/working_days.rb b/app/services/work_packages/shared/working_days.rb
index fd2f80f8494..cec65c00cec 100644
--- a/app/services/work_packages/shared/working_days.rb
+++ b/app/services/work_packages/shared/working_days.rb
@@ -70,16 +70,16 @@ module WorkPackages
delta
end
+ def working?(date)
+ working_week_day?(date) && working_specific_date?(date)
+ end
+
private
def no_duration
OpenProject::FeatureDecisions.work_packages_duration_field_active? ? nil : 1
end
- def working?(date)
- working_week_day?(date) && working_specific_date?(date)
- end
-
def working_week_day?(date)
working_week_days[date.wday]
end
diff --git a/spec/support/schedule_helpers.rb b/spec/support/schedule_helpers.rb
index 6dfc959b91a..3e6053dfae9 100644
--- a/spec/support/schedule_helpers.rb
+++ b/spec/support/schedule_helpers.rb
@@ -49,19 +49,28 @@ module ScheduleHelpers
# * +_+: ignored but useful as a placeholder to highlight particular days, for
# instance to highlight the previous dates of a work package.
class Chart
- attr_writer :first_day
+ attr_reader :first_day, :last_day, :monday
def self.for(representation)
builder = ChartBuilder.new
builder.parse(representation)
end
+ def self.from_work_packages(work_packages)
+ ChartBuilder.new.use_work_packages(Array(work_packages))
+ end
+
def initialize
- @monday = next_monday
+ self.monday = next_monday
end
- def first_day
- @first_day ||= monday
+ # Sets the origin of the calendar, represented by +M+ on the first line (M as
+ # in Monday).
+ def monday=(monday)
+ raise ArgumentError, "#{monday} is not a Monday" unless monday.wday == 1
+
+ extend_calendar_range(monday, monday + 6.days)
+ @monday = monday
end
def validate
@@ -95,6 +104,7 @@ module ScheduleHelpers
def add_work_package(attributes)
name = attributes[:subject].to_sym
+ extend_calendar_range(*attributes.values_at(:start_date, :due_date))
work_packages[name] = attributes
end
@@ -103,43 +113,48 @@ module ScheduleHelpers
delays_between[[predecessor, follower]] = delay
end
- # Returns the date for the Monday represented on the chart.
- def monday
- @monday
- end
-
- # Returns the date for the Tuesday represented on the chart.
- def tuesday
- monday + 1.day
- end
-
- # Returns the date for the Wednesday represented on the chart.
- def wednesday
- monday + 2.days
+ def to_s
+ representer = ChartRepresenter.new
+ representer.add_row
+ representer.add_cell('days')
+ representer.add_cell(spaced_at(monday, 'MTWTFSS'))
+ work_packages.each do |name, attributes|
+ representer.add_row
+ representer.add_cell(name.to_s)
+ representer.add_cell(span(attributes))
+ end
+ representer.to_s
end
- # Returns the date for the Thursday represented on the chart.
- def thursday
- monday + 3.days
- end
+ private
- # Returns the date for the Friday represented on the chart.
- def friday
- monday + 4.days
+ def extend_calendar_range(*dates)
+ @first_day = [@first_day, *dates].compact.min
+ @last_day = [@last_day, *dates].compact.max
end
- # Returns the date for the Saturday represented on the chart.
- def saturday
- monday + 5.days
+ def spaced_at(date, text)
+ nb_days = date - first_day
+ (" " * nb_days) + text
end
- # Returns the date for the Sunday represented on the chart.
- def sunday
- monday + 6.days
+ def span(attributes)
+ case attributes
+ in { start_date: nil, due_date: nil }
+ ''
+ in { start_date:, due_date: nil }
+ spaced_at(start_date, '[')
+ in { start_date: nil, due_date: }
+ spaced_at(due_date, ']')
+ in { start_date:, due_date: }
+ days = WorkPackages::Shared::WorkingDays.new
+ span = (start_date..due_date).map do |date|
+ days.working?(date) ? 'X' : '.'
+ end.join
+ spaced_at(start_date, span)
+ end
end
- private
-
def next_monday
date = Time.zone.today
date += 1.day while date.wday != 1
@@ -151,6 +166,37 @@ module ScheduleHelpers
end
end
+ class ChartRepresenter
+ LINE = "%<id>s | %<days>s |".freeze
+
+ def add_row
+ rows << []
+ end
+
+ def add_cell(text)
+ rows.last << text
+ end
+
+ def rows
+ @rows ||= []
+ end
+
+ def to_s
+ line_template = "%<id>-#{columns_size[0]}s | %<days>-#{columns_size[1]}s |"
+ rows.map do |row|
+ line_template % { id: row[0], days: row[1] }
+ end.join("\n")
+ end
+
+ def columns
+ rows.transpose
+ end
+
+ def columns_size
+ columns.map { |column| column.map(&:length).max }
+ end
+ end
+
# Builds a +Chart+ instance from a visual chart representation.
#
# Example:
@@ -181,6 +227,13 @@ module ScheduleHelpers
chart
end
+ def use_work_packages(work_packages)
+ work_packages.each do |work_package|
+ chart.add_work_package(work_package.slice(:subject, :start_date, :due_date))
+ end
+ chart
+ end
+
private
def parse_header(header)
@@ -189,8 +242,7 @@ module ScheduleHelpers
raise ArgumentError, "First header line of schedule chart must contain MTWTFSS to indicate day names and have an origin"
end
- nb_days_from_monday = week_days.index('M')
- chart.first_day = chart.monday - nb_days_from_monday.days
+ @nb_days_from_origin_monday = week_days.index('M')
end
def parse_line(line)
@@ -233,8 +285,8 @@ module ScheduleHelpers
start_pos = timespan.index('[') || timespan.index('X')
due_pos = timespan.rindex(']') || timespan.rindex('X')
{
- start_date: start_pos && (chart.first_day + start_pos),
- due_date: due_pos && (chart.first_day + due_pos)
+ start_date: start_pos && (chart.monday - @nb_days_from_origin_monday + start_pos),
+ due_date: due_pos && (chart.monday - @nb_days_from_origin_monday + due_pos)
}
end
@@ -399,4 +451,20 @@ end
RSpec.configure do |config|
config.extend ScheduleHelpers::LetSchedule
config.include ScheduleHelpers::ExampleMethods
+
+ RSpec::Matchers.define :match_schedule do |expected|
+ match do |actual_work_packages|
+ expected_chart = ScheduleHelpers::Chart.for(expected)
+ @expected = expected_chart.to_s # normalize expected
+
+ actual_chart = ScheduleHelpers::Chart.from_work_packages(actual_work_packages)
+ actual_chart.monday = expected_chart.monday
+ @actual = actual_chart.to_s
+
+ values_match? @expected, @actual
+ end
+
+ diffable
+ attr_reader :expected, :actual
+ end
end
diff --git a/spec/support_spec/schedule_helpers_spec.rb b/spec/support_spec/schedule_helpers_spec.rb
index 66673f8a5c6..953631d78e2 100644
--- a/spec/support_spec/schedule_helpers_spec.rb
+++ b/spec/support_spec/schedule_helpers_spec.rb
@@ -50,21 +50,134 @@ describe ScheduleHelpers do
end
describe '#first_day' do
- it 'returns the first day represented on the graph, which is next monday by default' 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
+ end
+
+ context 'with work packages' do
+ it 'returns the minimum between work packages dates and origin Monday' do
+ expect(chart.first_day).to eq(monday)
+
+ chart.add_work_package(subject: 'wp1', start_date: tuesday)
+ expect(chart.first_day).to eq(monday)
+
+ chart.add_work_package(subject: 'wp2', start_date: monday - 3.days)
+ expect(chart.first_day).to eq(monday - 3.days)
+
+ chart.add_work_package(subject: 'wp3', start_date: sunday)
+ expect(chart.first_day).to eq(monday - 3.days)
+
+ chart.add_work_package(subject: 'wp4', due_date: monday - 6.days)
+ expect(chart.first_day).to eq(monday - 6.days)
+ 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)
+
+ # 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
+ end
- it 'can be set' do
- chart.first_day = fake_today
- expect(chart.first_day).to eq(fake_today)
+ 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
+ end
+
+ context 'with work packages' do
+ it 'returns the maximum between work packages dates and the Sunday following origin Monday' do
+ expect(chart.last_day).to eq(sunday)
+
+ chart.add_work_package(subject: 'wp1', due_date: tuesday + 7.days)
+ expect(chart.last_day).to eq(tuesday + 7.days)
+
+ chart.add_work_package(subject: 'wp2', start_date: monday - 3.days)
+ expect(chart.last_day).to eq(tuesday + 7.days)
+
+ chart.add_work_package(subject: 'wp3', start_date: monday + 20.days)
+ expect(chart.last_day).to eq(monday + 20.days)
+ 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)
+
+ # no change when origin is moved backward
+ expect { chart.monday = monday - 14.days }
+ .not_to change(chart, :last_day)
+
+ # change when origin is moved forward
+ expect { chart.monday = monday + 14.days }
+ .to change(chart, :last_day).to(sunday + 14.days)
end
end
- %i[monday tuesday wednesday thursday friday saturday sunday].each do |day_name|
- describe "##{day_name}" do
- it "returns the #{day_name} of the week represented on the chart" do
- expected_date = send(day_name)
- expect(chart.send(day_name)).to eq(expected_date)
+ describe '#to_s' do
+ let!(:week_days) { create(:week_days) }
+
+ context 'with a chart built from ascii representation' do
+ let(:chart) do
+ ScheduleHelpers::ChartBuilder.new.parse(<<~CHART)
+ days | MTWTFSS |
+ main | X..X |
+ other | XXX..X |
+ follower | XXX | follows main
+ start_only | [ |
+ due_only | ] |
+ no_dates | |
+ CHART
+ end
+
+ it 'returns the same ascii representation without properties information' do
+ expect(chart.to_s).to eq(<<~CHART.chomp)
+ days | MTWTFSS |
+ main | X..X |
+ other | XXX..X |
+ follower | XXX |
+ start_only | [ |
+ due_only | ] |
+ no_dates | |
+ CHART
+ end
+ end
+
+ context 'with a chart built from real work packages' do
+ let(:work_package1) { build_stubbed(:work_package, subject: 'main', start_date: monday, due_date: tuesday) }
+ let(:work_package2) { build_stubbed(:work_package, subject: 'other', start_date: tuesday, due_date: monday + 7.days) }
+ let(:work_package3) { build_stubbed(:work_package, subject: 'start_only', start_date: monday - 3.days) }
+ let(:work_package4) { build_stubbed(:work_package, subject: 'due_only', due_date: wednesday) }
+ let(:work_package5) { build_stubbed(:work_package, subject: 'no_dates') }
+ let(:chart) do
+ ScheduleHelpers::ChartBuilder.new.use_work_packages(
+ [
+ work_package1,
+ work_package2,
+ work_package3,
+ work_package4,
+ work_package5
+ ]
+ )
+ end
+
+ it 'returns the same ascii representation without properties information' do
+ expect(chart.to_s).to eq(<<~CHART.chomp)
+ days | MTWTFSS |
+ main | XX |
+ other | XXXX..X |
+ start_only | [ |
+ due_only | ] |
+ no_dates | |
+ CHART
end
end
end
@@ -119,26 +232,13 @@ describe ScheduleHelpers do
expect(chart.monday).to eq(chart.first_day)
end
- it 'defines the first day by the number of characters separating the M from the cell left border' do
- chart = builder.parse(<<~CHART)
- days | 12MTWTFSS |
- CHART
- expect(chart.monday).to eq(monday)
- expect(chart.first_day).to eq(chart.monday - 2.days)
-
- chart = builder.parse(<<~CHART)
- days | 123456789MTWTFSS |
- CHART
- expect(chart.monday).to eq(monday)
- expect(chart.first_day).to eq(chart.monday - 9.days)
- end
-
it 'is not identified by mtwtfss which can be used as documentation instead' do
chart = builder.parse(<<~CHART)
- days | mtwtfssMTWTFSSmtwtfss |
+ days | mtwtfssMTWTFSSmtwtfss |
+ wp | X |
CHART
expect(chart.monday).to eq(monday)
- expect(chart.monday).to eq(chart.first_day + 7.days)
+ expect(chart.first_day).to eq(chart.work_packages.dig(:wp, :start_date))
end
end
@@ -208,12 +308,12 @@ describe ScheduleHelpers do
expect(main).to have_attributes(
subject: 'main',
start_date: schedule_chart.monday,
- due_date: schedule_chart.tuesday
+ due_date: schedule_chart.monday + 1.day
)
expect(follower).to have_attributes(
subject: 'follower',
- start_date: schedule_chart.wednesday,
- due_date: schedule_chart.friday
+ start_date: schedule_chart.monday + 2.days,
+ due_date: schedule_chart.monday + 4.days
)
end