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

[37124] Add formatter classes for estimated hours and generalize (#9252)

Oliver Güntheraf4922ebf6bc
diff --git a/app/models/work_package/exporter/csv.rb b/app/models/work_package/exporter/csv.rb
index a82bfd6b71e..b4471ecbe8b 100644
--- a/app/models/work_package/exporter/csv.rb
+++ b/app/models/work_package/exporter/csv.rb
@@ -107,33 +107,7 @@ class WorkPackage::Exporter::CSV < WorkPackage::Exporter::Base
end
def csv_format_value(work_package, column)
- if column.is_a?(Queries::WorkPackages::Columns::CustomFieldColumn)
- csv_format_custom_value(work_package, column)
- else
- value = work_package.send(column.name)
-
- case value
- when Date
- format_date(value)
- when Time
- format_time(value)
- when nil
- # ruby >=2.7.1 will return a frozen string for nil.to_s which will cause an error when e.g. trying to
- # force an encoding
- ''
- else
- value
- end
- end.to_s
- end
-
- def csv_format_custom_value(work_package, column)
- cv = work_package
- .custom_values
- .select { |v| v.custom_field_id == column.custom_field.id }
-
- cv
- .map { |v| show_value(v) }
- .join('; ')
+ formatter = ::WorkPackage::Exporter::Formatters.for_column(column)
+ formatter.format(work_package, column, array_separator: '; ')
end
end
diff --git a/app/models/work_package/exporter/formatters.rb b/app/models/work_package/exporter/formatters.rb
new file mode 100644
index 00000000000..0b6e86ac26a
--- /dev/null
+++ b/app/models/work_package/exporter/formatters.rb
@@ -0,0 +1,26 @@
+module WorkPackage::Exporter
+ module Formatters
+ def self.all
+ @formatters ||= begin
+ %i[default costs estimated_hours].map do |key|
+ Kernel.const_get("WorkPackage::Exporter::Formatters::#{key.to_s.camelize}")
+ end
+ end
+ end
+
+ def self.keys
+ all.map(&:key)
+ end
+
+ def self.register(clz)
+ @formatters << clz
+ end
+
+ ##
+ # Returns a matching formatter for the given query column
+ def self.for_column(column)
+ formatter = all.find { |formatter| formatter.apply? column } || Default
+ formatter.new
+ end
+ end
+end
diff --git a/app/models/work_package/exporter/formatters/costs.rb b/app/models/work_package/exporter/formatters/costs.rb
new file mode 100644
index 00000000000..ad2af83a14a
--- /dev/null
+++ b/app/models/work_package/exporter/formatters/costs.rb
@@ -0,0 +1,24 @@
+require_relative './default'
+
+module WorkPackage::Exporter
+ module Formatters
+ class Costs < Default
+ def self.apply?(column)
+ column.is_a? ::Costs::QueryCurrencyColumn
+ end
+
+ def format_options(_column)
+ { number_format: number_format_string }
+ end
+
+ def number_format_string
+ # [$CUR] makes sure we have an actually working currency format with arbitrary currencies
+ curr = "[$CUR]".gsub "CUR", ERB::Util.h(Setting.plugin_costs['costs_currency'])
+ format = ERB::Util.h Setting.plugin_costs['costs_currency_format']
+ number = '#,##0.00'
+
+ format.gsub("%n", number).gsub("%u", curr)
+ end
+ end
+ end
+end
\ No newline at end of file
diff --git a/app/models/work_package/exporter/formatters/default.rb b/app/models/work_package/exporter/formatters/default.rb
new file mode 100644
index 00000000000..789b4932033
--- /dev/null
+++ b/app/models/work_package/exporter/formatters/default.rb
@@ -0,0 +1,44 @@
+module WorkPackage::Exporter
+ module Formatters
+ class Default
+ include Redmine::I18n
+
+ ##
+ # Takes a QueryColumn and returns true if this formatter should be used to handle it.
+ def self.apply?(column)
+ false
+ end
+
+ def self.key
+ self.name.demodulize.underscore.to_sym
+ end
+
+ ##
+ # Takes a WorkPackage and a QueryColumn and returns the value to be exported.
+ def format(work_package, column, **options)
+ value = column.value work_package
+
+ case value
+ when Date
+ format_date value
+ when Time, DateTime, ActiveSupport::TimeWithZone
+ format_time value
+ when Array
+ value.join options.fetch(:array_separator, ', ')
+ when nil
+ # ruby >=2.7.1 will return a frozen string for nil.to_s which will cause an error when e.g. trying to
+ # force an encoding
+ ''
+ else
+ value.to_s
+ end
+ end
+
+ ##
+ # Takes a QueryColumn and returns format options for it.
+ def format_options(_column)
+ {}
+ end
+ end
+ end
+end
\ No newline at end of file
diff --git a/app/models/work_package/exporter/formatters/estimated_hours.rb b/app/models/work_package/exporter/formatters/estimated_hours.rb
new file mode 100644
index 00000000000..d63f232405d
--- /dev/null
+++ b/app/models/work_package/exporter/formatters/estimated_hours.rb
@@ -0,0 +1,24 @@
+require_relative './default'
+
+module WorkPackage::Exporter
+ module Formatters
+ class EstimatedHours < Default
+ def self.apply?(column)
+ column.name == :estimated_hours
+ end
+
+ ##
+ # Takes a WorkPackage and a QueryColumn and returns the value to be exported.
+ def format(work_package, column, **options)
+ value = work_package.estimated_hours
+ derived_estimated_value = work_package.derived_estimated_hours
+
+ if value.nil? && derived_estimated_value.present?
+ "(#{derived_estimated_value})"
+ else
+ value
+ end
+ end
+ end
+ end
+end
\ No newline at end of file
diff --git a/app/models/work_package/pdf_export/work_package_list_to_pdf.rb b/app/models/work_package/pdf_export/work_package_list_to_pdf.rb
index 14395576caf..06d5706bfed 100644
--- a/app/models/work_package/pdf_export/work_package_list_to_pdf.rb
+++ b/app/models/work_package/pdf_export/work_package_list_to_pdf.rb
@@ -290,15 +290,9 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exporter::Base
end
def make_column_value(work_package, column)
- if column.is_a?(Queries::WorkPackages::Columns::CustomFieldColumn)
- make_custom_field_value work_package, column
- else
- make_field_value work_package, column.name
- end
- end
+ formatter = ::WorkPackage::Exporter::Formatters.for_column(column)
- def make_field_value(work_package, column_name)
- pdf.make_cell field_value(work_package, column_name),
+ pdf.make_cell formatter.format(work_package, column),
padding: cell_padding
end
@@ -312,15 +306,6 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exporter::Base
end
end
- def make_custom_field_value(work_package, column)
- values = work_package
- .custom_values
- .select { |v| v.custom_field_id == column.custom_field.id }
-
- pdf.make_cell values.map(&:formatted_value).join(', '),
- padding: cell_padding
- end
-
def batch_supported?
return @batch_supported if defined?(@batch_supported)
diff --git a/app/models/work_package/pdf_export/work_package_to_pdf.rb b/app/models/work_package/pdf_export/work_package_to_pdf.rb
index d438beacc4f..aa43f56b1f9 100644
--- a/app/models/work_package/pdf_export/work_package_to_pdf.rb
+++ b/app/models/work_package/pdf_export/work_package_to_pdf.rb
@@ -33,12 +33,13 @@ class WorkPackage::PDFExport::WorkPackageToPdf < WorkPackage::Exporter::Base
include WorkPackage::PDFExport::Formattable
include WorkPackage::PDFExport::Attachments
- attr_accessor :pdf
+ attr_accessor :pdf, :columns
def initialize(work_package)
super
self.pdf = get_pdf(current_language)
+ self.columns = ::Query.available_columns(work_package.project)
configure_markup
end
@@ -83,7 +84,9 @@ class WorkPackage::PDFExport::WorkPackageToPdf < WorkPackage::Exporter::Base
label_options
)
- value_content = field_value work_package, attribute
+ column = columns.find { |col| col.name == attribute.to_sym }
+ formatter = ::WorkPackage::Exporter::Formatters.for_column(column)
+ value_content = formatter.format(work_package, column)
value = pdf.make_cell(value_content.to_s, value_options)
[label, value]
diff --git a/lib/redmine/codeset_util.rb b/lib/redmine/codeset_util.rb
index dc73aa0f8a3..d297c7436ad 100644
--- a/lib/redmine/codeset_util.rb
+++ b/lib/redmine/codeset_util.rb
@@ -68,6 +68,7 @@ module Redmine
def self.from_utf8(str, encoding)
str ||= ''
+ str = str.dup if str.frozen?
str.force_encoding('UTF-8')
if encoding.upcase != 'UTF-8'
str.encode(encoding,
diff --git a/modules/costs/lib/costs/query_currency_column.rb b/modules/costs/lib/costs/query_currency_column.rb
index e95257dd28f..1772a6aed18 100644
--- a/modules/costs/lib/costs/query_currency_column.rb
+++ b/modules/costs/lib/costs/query_currency_column.rb
@@ -43,14 +43,6 @@ module Costs
super_value work_package
end
- def xls_formatter
- :cost
- end
-
- def xls_value(work_package)
- super_value work_package
- end
-
class_attribute :currency_columns
self.currency_columns = {
diff --git a/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb b/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb
index e9238c20783..702825473bc 100644
--- a/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb
+++ b/modules/xls_export/app/models/xls_export/work_package/exporter/xls.rb
@@ -82,12 +82,16 @@ module XlsExport::WorkPackage::Exporter
end
def format_column_value(column, work_package)
- formatters[column].format work_package, column
+ ::WorkPackage::Exporter::Formatters
+ .for_column(column)
+ .format(work_package, column)
end
def set_column_format_options!(spreadsheet)
columns.each_with_index do |column, i|
- options = formatters[column].format_options column
+ options = ::WorkPackage::Exporter::Formatters
+ .for_column(column)
+ .format_options column
spreadsheet.add_format_option_to_column i, options
end
@@ -97,10 +101,6 @@ module XlsExport::WorkPackage::Exporter
@columns ||= valid_export_columns
end
- def formatters
- @formatters ||= OpenProject::XlsExport::Formatters.for_columns(columns)
- end
-
def spreadsheet_builder
OpenProject::XlsExport::SpreadsheetBuilder.new I18n.t(:label_work_package_plural)
end
@@ -112,7 +112,7 @@ module XlsExport::WorkPackage::Exporter
def xls_export_filename
sane_filename(
"#{Setting.app_title} #{I18n.t(:label_work_package_plural)} \
- #{format_time_as_date(Time.now, '%Y-%m-%d')}.xls"
+ #{format_time_as_date(Time.now, '%Y-%m-%d')}.xls"
)
end
end
@@ -142,8 +142,8 @@ module XlsExport::WorkPackage::Exporter
module WithRelations
def add_headers!(spreadsheet)
headers_0 = [I18n.t(:label_work_package_plural)] +
- columns.size.times.map { |_| "" } +
- [I18n.t("js.work_packages.tabs.relations")]
+ columns.size.times.map { |_| "" } +
+ [I18n.t("js.work_packages.tabs.relations")]
spreadsheet.add_headers headers_0, 0
spreadsheet.add_headers headers, 1
@@ -186,9 +186,9 @@ module XlsExport::WorkPackage::Exporter
# parent or a child.
def related_work_packages(work_package)
family = ([work_package.parent].compact +
- work_package.children.order(:subject))
- .select { |wp| wp.visible? current_user }
- .map { |wp| [wp, nil] }
+ work_package.children.order(:subject))
+ .select { |wp| wp.visible? current_user }
+ .map { |wp| [wp, nil] }
family + relation_work_packages(work_package)
end
diff --git a/modules/xls_export/lib/open_project/xls_export/engine.rb b/modules/xls_export/lib/open_project/xls_export/engine.rb
index 53723534af9..82e851437ce 100644
--- a/modules/xls_export/lib/open_project/xls_export/engine.rb
+++ b/modules/xls_export/lib/open_project/xls_export/engine.rb
@@ -8,7 +8,7 @@ module OpenProject::XlsExport
author_url: 'https://www.openproject.com',
bundled: true
- patches %i[Queries::WorkPackages::Columns::WorkPackageColumn CostReportsController]
+ patches %i[CostReportsController]
extend_api_response(:v3, :work_packages, :work_package_collection) do
require_relative 'patches/api/v3/export_formats'
diff --git a/modules/xls_export/lib/open_project/xls_export/formatters.rb b/modules/xls_export/lib/open_project/xls_export/formatters.rb
deleted file mode 100644
index 80ed5c292f3..00000000000
--- a/modules/xls_export/lib/open_project/xls_export/formatters.rb
+++ /dev/null
@@ -1,78 +0,0 @@
-module OpenProject::XlsExport
- module Formatters
- def self.all
- constants.map do |const|
- Kernel.const_get("OpenProject::XlsExport::Formatters::#{const}")
- end.select do |const|
- const.is_a?(Class) && const != DefaultFormatter
- end + [DefaultFormatter]
- end
-
- def self.keys
- all.map(&:key)
- end
-
- ##
- # Returns a Hash mapping columns to formatters to be used.
- def self.for_columns(columns)
- formatters = all
- entries = columns.map do |column|
- formatter = formatters.find { |formatter| formatter.apply? column }
- [column, (formatter || DefaultFormatter).new]
- end
- Hash[entries]
- end
-
- class DefaultFormatter
- ##
- # Takes a QueryColumn and returns true if this formatter should be used to handle it.
- def self.apply?(column)
- column.xls_formatter == key
- end
-
- def self.key
- name = self.name.demodulize.underscore
- name[0..(name.index("_") - 1)].to_sym
- end
-
- ##
- # Takes a WorkPackage and a QueryColumn and returns the value to be exported.
- def format(work_package, column)
- value = column.xls_value work_package
-
- if value.is_a? Array
- value.join(", ")
- else
- value
- end
- end
-
- ##
- # Takes a QueryColumn and returns format options for it.
- def format_options(_column)
- {}
- end
- end
-
- class TimeFormatter < DefaultFormatter
- def format_options(_column)
- { number_format: '0.0 "h"' }
- end
- end
-
- class CostFormatter < DefaultFormatter
- def format_options(_column)
- { number_format: number_format_string }
- end
-
- def number_format_string
- # [$CUR] makes sure we have an actually working currency format with arbitrary currencies
- curr = "[$CUR]".gsub "CUR", ERB::Util.h(Setting.plugin_costs['costs_currency'])
- format = ERB::Util.h Setting.plugin_costs['costs_currency_format']
- number = '#,##0.00'
-
- format.gsub("%n", number).gsub("%u", curr)
- end
- end
- end
-end
diff --git a/modules/xls_export/lib/open_project/xls_export/patches/queries/work_packages/columns/work_package_column_patch.rb b/modules/xls_export/lib/open_project/xls_export/patches/queries/work_packages/columns/work_package_column_patch.rb
deleted file mode 100644
index c9efcd8e6af..00000000000
--- a/modules/xls_export/lib/open_project/xls_export/patches/queries/work_packages/columns/work_package_column_patch.rb
+++ /dev/null
@@ -1,30 +0,0 @@
-module OpenProject::XlsExport::Patches
- module Queries
- module WorkPackages
- module Columns
- module WorkPackageColumnPatch
- def self.included(base) # :nodoc:
- base.class_eval do
- include InstanceMethods
- end
- end
-
- module InstanceMethods
- ##
- # Returns the key (symbol) of the formatter to be used.
- # E.g. :default, :time, :cost
- #
- # Available keys: OpenProject::XlsExport::Formatters.keys
- def xls_formatter
- nil
- end
-
- def xls_value(work_package)
- value work_package
- end
- end
- end
- end
- end
- end
-end
diff --git a/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb b/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb
index f160bd9ceb2..d16d44f6d56 100644
--- a/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb
+++ b/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb
@@ -115,7 +115,7 @@ describe XlsExport::WorkPackage::Exporter::XLS do
# duplicates rows for each relation
c2id = child_2.id
expect(sheet.column(2).drop(2))
- .to eq [parent.id, parent.id, child_1.id, c2id, c2id, c2id, single.id, followed.id, child_2_child.id]
+ .to eq [parent.id, parent.id, child_1.id, c2id, c2id, c2id, single.id, followed.id, child_2_child.id].map(&:to_s)
# marks Parent as parent of Child 1 and 2
expect(sheet.row(PARENT)[RELATION]).to eq 'parent of'
@@ -151,22 +151,22 @@ describe XlsExport::WorkPackage::Exporter::XLS do
# exports the correct data (examples)
expect(sheet.row(PARENT))
.to eq [
- nil, parent.type.name, parent.id, parent.subject, parent.status.name, parent.assigned_to, parent.priority.name,
+ nil, parent.type.name, parent.id.to_s, parent.subject, parent.status.name, parent.assigned_to, parent.priority.name,
nil, 'parent of', nil, nil,
- child_1.type.name, child_1.id, child_1.subject, child_1.status.name, child_1.assigned_to, child_1.priority.name
+ child_1.type.name, child_1.id.to_s, child_1.subject, child_1.status.name, child_1.assigned_to, child_1.priority.name
] # delay nil as this is a parent-child relation not represented by an actual Relation record
expect(sheet.row(SINGLE))
.to eq [
- nil, single.type.name, single.id, single.subject, single.status.name, single.assigned_to, single.priority.name
+ nil, single.type.name, single.id.to_s, single.subject, single.status.name, single.assigned_to, single.priority.name
]
expect(sheet.row(FOLLOWED))
.to eq [
nil,
- followed.type.name, followed.id, followed.subject, followed.status.name, followed.assigned_to, followed.priority.name,
+ followed.type.name, followed.id.to_s, followed.subject, followed.status.name, followed.assigned_to, followed.priority.name,
nil, 'Precedes', 0, relation.description,
- child_2.type.name, child_2.id, child_2.subject, child_2.status.name, child_2.assigned_to, child_2.priority.name
+ child_2.type.name, child_2.id.to_s, child_2.subject, child_2.status.name, child_2.assigned_to, child_2.priority.name
]
end
@@ -218,14 +218,6 @@ describe XlsExport::WorkPackage::Exporter::XLS do
let(:column_names) { ['subject', 'status', 'estimated_hours', "cf_#{custom_field.id}"] }
before do
- allow(OpenProject::XlsExport::Formatters::TimeFormatter).to receive(:apply?) do |column|
- column.caption =~ /time/i
- end
-
- allow(OpenProject::XlsExport::Formatters::CostFormatter).to receive(:apply?) do |column|
- column.caption =~ /cost/i
- end
-
allow(Setting)
.to receive(:plugin_costs)
.and_return('costs_currency' => 'EUR', 'costs_currency_format' => '%n %u')
@@ -322,18 +314,38 @@ describe XlsExport::WorkPackage::Exporter::XLS do
let(:column_names) { %w[subject status updated_at] }
+ let(:i18n_helper) do
+ Class.new do
+ include Redmine::I18n
+ end
+ end
+
before do
allow(current_user).to receive(:time_zone).and_return(zone)
-
- allow(OpenProject::XlsExport::Formatters::TimeFormatter).to receive(:apply?) do |column|
- column.caption =~ /time/i
- end
end
it 'should adapt the datetime fields to the user time zone' do
work_package.reload
updated_at_cell = sheet.rows.last.to_a.last
- expect(updated_at_cell.to_s(:number)).to eq(work_package.updated_at.in_time_zone(zone).to_s(:number))
+ expect(updated_at_cell).to eq(i18n_helper.format_time(work_package.updated_at).to_s)
+ end
+ end
+
+ describe 'with derived estimated hours' do
+ let(:work_package) do
+ FactoryBot.create(:work_package,
+ project: project,
+ derived_estimated_hours: 15.0,
+ type: project.types.first)
+ end
+ let(:work_packages) { [work_package] }
+
+ let(:column_names) { %w[subject status updated_at estimated_hours] }
+
+ it 'should adapt the datetime fields to the user time zone' do
+ work_package.reload
+ estimated_cell = sheet.rows.last.to_a.last
+ expect(estimated_cell).to eq '(15.0)'
end
end
end
diff --git a/spec/models/work_package/exporter/csv_integration_spec.rb b/spec/models/work_package/exporter/csv_integration_spec.rb
index 239ee65aab2..7ae5410e800 100644
--- a/spec/models/work_package/exporter/csv_integration_spec.rb
+++ b/spec/models/work_package/exporter/csv_integration_spec.rb
@@ -44,7 +44,7 @@ describe WorkPackage::Exporter::CSV, 'integration', type: :model do
end
let(:query) do
Query.new(name: '_').tap do |query|
- query.column_names = %i(subject assigned_to updated_at)
+ query.column_names = %i(subject assigned_to updated_at estimated_hours)
end
end
let(:instance) do
@@ -64,6 +64,7 @@ describe WorkPackage::Exporter::CSV, 'integration', type: :model do
subject: "Ruby encodes ß as '\\xDF' in ISO-8859-1.",
description: "\u2022 requires unicode.",
assigned_to: current_user,
+ derived_estimated_hours: 15.0,
project: project
)
end
@@ -83,6 +84,7 @@ describe WorkPackage::Exporter::CSV, 'integration', type: :model do
expect(data.last).to include(work_package.description)
expect(data.last).to include(current_user.name)
expect(data.last).to include(work_package.updated_at.localtime.strftime("%m/%d/%Y %I:%M %p"))
+ expect(data.last).to include('(15.0)')
end
end
end