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

Show non-working days in the rendered table of table helpers

Christophe Bliardf72692d3fee9
diff --git a/app/services/work_packages/shared/working_days.rb b/app/services/work_packages/shared/working_days.rb
index ecaf0a88754..5bc2179e01b 100644
--- a/app/services/work_packages/shared/working_days.rb
+++ b/app/services/work_packages/shared/working_days.rb
@@ -29,6 +29,12 @@
module WorkPackages
module Shared
class WorkingDays
+ class << self
+ def clear_cache
+ RequestStore.delete(:work_package_non_working_dates)
+ end
+ end
+
# Returns number of working days between two dates, excluding weekend days
# and non working days.
def duration(start_date, due_date)
diff --git a/spec/support/table_helpers/column_type/schedule.rb b/spec/support/table_helpers/column_type/schedule.rb
index 5abe95ce5b2..19c325f36d5 100644
--- a/spec/support/table_helpers/column_type/schedule.rb
+++ b/spec/support/table_helpers/column_type/schedule.rb
@@ -58,7 +58,8 @@ module TableHelpers
def attributes_for_work_package(_attribute, work_package)
{
start_date: work_package.start_date,
- due_date: work_package.due_date
+ due_date: work_package.due_date,
+ ignore_non_working_days: work_package.ignore_non_working_days
}
end
diff --git a/spec/support/table_helpers/table_representer.rb b/spec/support/table_helpers/table_representer.rb
index 31b1123c9d4..aa6171ecbe8 100644
--- a/spec/support/table_helpers/table_representer.rb
+++ b/spec/support/table_helpers/table_representer.rb
@@ -36,6 +36,7 @@ module TableHelpers
end
def render(table_data)
+ WorkPackages::Shared::WorkingDays.clear_cache
columns
.map { |column| formatted_cells_for_column(column, table_data) }
.transpose
@@ -53,8 +54,9 @@ module TableHelpers
header = schedule_column_representer.column_title
start_dates = table_data.values_for_attribute(:start_date)
due_dates = table_data.values_for_attribute(:due_date)
- values = start_dates.zip(due_dates).map do |start_date, due_date|
- schedule_column_representer.span(start_date, due_date)
+ ignore_non_working_days_values = ignore_non_working_days_values(table_data)
+ values = start_dates.zip(due_dates, ignore_non_working_days_values).map do |start_date, due_date, ignore_non_working_days|
+ schedule_column_representer.span(start_date, due_date, ignore_non_working_days)
end
else
header = column.title
@@ -65,6 +67,18 @@ module TableHelpers
[header, *values]
end
+ # look into the other tables to find the ignore_non_working_days values of
+ # work packages for the given table data
+ def ignore_non_working_days_values(table_data)
+ master_values = table_data.work_packages_data.pluck(:attributes).pluck(:subject, :ignore_non_working_days).to_h
+ other_values = tables_data.reject { _1 == table_data }
+ .map { _1.work_packages_data.pluck(:attributes).pluck(:subject, :ignore_non_working_days).to_h.compact }
+ .reduce({}) { |acc, element| acc.reverse_merge(element) }
+ master_values.map do |subject, ignore_non_working_days|
+ ignore_non_working_days.nil? ? other_values[subject] : ignore_non_working_days
+ end
+ end
+
def normalize_width(cells, column)
header, *values = cells
width = column_width(column)
@@ -118,7 +132,7 @@ module TableHelpers
spaced_at(monday, "MTWTFSS")
end
- def span(start_date, due_date)
+ def span(start_date, due_date, ignore_non_working_days)
if start_date.nil? && due_date.nil?
" " * column_size
elsif due_date.nil?
@@ -126,7 +140,10 @@ module TableHelpers
elsif start_date.nil?
spaced_at(due_date, "]")
else
- span = "X" * (start_date..due_date).count
+ days = days_for(ignore_non_working_days)
+ span = (start_date..due_date).map do |date|
+ days.working?(date) ? "X" : "."
+ end.join
spaced_at(start_date, span)
end
end
@@ -136,6 +153,14 @@ module TableHelpers
spaced = (" " * nb_days) + text
spaced.ljust(column_size)
end
+
+ def days_for(ignore_non_working_days)
+ if ignore_non_working_days
+ WorkPackages::Shared::AllDays.new
+ else
+ WorkPackages::Shared::WorkingDays.new
+ end
+ end
end
end
end
diff --git a/spec/support_spec/table_helpers/table_representer_spec.rb b/spec/support_spec/table_helpers/table_representer_spec.rb
index 575b4c91b51..26fe75ba594 100644
--- a/spec/support_spec/table_helpers/table_representer_spec.rb
+++ b/spec/support_spec/table_helpers/table_representer_spec.rb
@@ -131,6 +131,53 @@ module TableHelpers
TABLE
end
+ context "when non working days are defined" do
+ let(:table) do
+ <<~TABLE
+ | subject | MTWTFSS | days counting
+ | wp1 | XXXXXXX | working days only
+ | wp2 | XXXXXXX | all days
+ TABLE
+ end
+
+ before do
+ set_non_working_week_days("wednesday", "thursday")
+ end
+
+ it "renders the non working days as dots `.` for work packages taking non-working days into account" do
+ expect(representer.render(table_data)).to eq <<~TABLE
+ | MTWTFSS |
+ | XX..XXX |
+ | XXXXXXX |
+ TABLE
+ end
+
+ context "when using a second table which does not have the ignore_non_working_days attribute knowledge" do
+ let(:twin_table) do
+ <<~TABLE
+ | subject | MTWTFSS |
+ | wp1 | XXXXXXX |
+ | wp2 | XXXXXXX |
+ TABLE
+ end
+ let(:twin_table_data) { TableData.for(twin_table) }
+ let(:tables_data) { [table_data, twin_table_data] }
+
+ it "renders the non working days as dots `.` for both tables" do
+ expect(representer.render(table_data)).to eq <<~TABLE
+ | MTWTFSS |
+ | XX..XXX |
+ | XXXXXXX |
+ TABLE
+ expect(representer.render(twin_table_data)).to eq <<~TABLE
+ | MTWTFSS |
+ | XX..XXX |
+ | XXXXXXX |
+ TABLE
+ end
+ end
+ end
+
context "when using a second table for the size" do
let(:twin_table) do
<<~TABLE