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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Use days and hours formatter in work package exports
Christophe Bliard72751b332006
diff --git a/app/models/work_package/exports/formatters/compound_hours.rb b/app/models/work_package/exports/formatters/compound_hours.rbnew file mode 100644index 00000000000..83fc4940327--- /dev/null+++ b/app/models/work_package/exports/formatters/compound_hours.rb@@ -0,0 +1,57 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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 WorkPackage::Exports+ module Formatters+ class CompoundHours < ::Exports::Formatters::Default+ def self.apply?(name, _export_format)+ name.to_sym == key+ end++ def format(work_package, **)+ hours = format_value(work_package.public_send(attribute))+ derived_hours = total_prefix(format_value(work_package.public_send(:"derived_#{attribute}")))++ [hours, derived_hours].compact.join(" ").presence+ end++ def format_value(value, _options = nil)+ DurationConverter.output(value)+ end++ private++ def attribute+ self.class.key+ end++ def total_prefix(value)+ value && "· Σ #{value}"+ end+ end+ end+enddiff --git a/app/models/work_package/exports/formatters/estimated_hours.rb b/app/models/work_package/exports/formatters/estimated_hours.rbindex 99a1b4d1f8a..48c61e7584f 100644--- a/app/models/work_package/exports/formatters/estimated_hours.rb+++ b/app/models/work_package/exports/formatters/estimated_hours.rb@@ -27,37 +27,7 @@#++module WorkPackage::Exportsmodule Formatters- class EstimatedHours < ::Exports::Formatters::Default- def self.apply?(name, _export_format)- name.to_sym == :estimated_hours- end-- def format(work_package, **)- estimated_hours = formatted_hours(work_package.estimated_hours)- derived_hours = formatted_derived_hours(work_package)-- if estimated_hours.nil? || derived_hours.nil?- return estimated_hours || derived_hours- end-- "#{estimated_hours} #{derived_hours}"- end-- def format_value(value, _options)- formatted_hours(value)- end-- private-- def formatted_hours(value)- value.nil? ? nil : "#{value} #{I18n.t('export.units.hours')}"- end-- def formatted_derived_hours(work_package)- if (derived_estimated_value = work_package.derived_estimated_hours)- "· Σ #{formatted_hours(derived_estimated_value)}"- end- end+ class EstimatedHours < ::WorkPackage::Exports::Formatters::CompoundHoursendendenddiff --git a/app/models/work_package/exports/formatters/hours.rb b/app/models/work_package/exports/formatters/hours.rbindex 5b5b415517e..29419f559bd 100644--- a/app/models/work_package/exports/formatters/hours.rb+++ b/app/models/work_package/exports/formatters/hours.rb@@ -29,17 +29,11 @@ module WorkPackage::Exportsmodule Formattersclass Hours < ::Exports::Formatters::Defaultdef self.apply?(name, export_format)- %i[remaining_hours spent_hours].include?(name.to_sym) && export_format == :pdf+ name.to_sym == :spent_hours && export_format == :pdfenddef format_value(value, _options)- formatted_hours(value)- end-- private-- def formatted_hours(value)- value.nil? ? "" : "#{value} #{I18n.t('export.units.hours')}"+ DurationConverter.output(value)endendenddiff --git a/app/models/work_package/exports/formatters/remaining_hours.rb b/app/models/work_package/exports/formatters/remaining_hours.rbnew file mode 100644index 00000000000..c1e2e880f4b--- /dev/null+++ b/app/models/work_package/exports/formatters/remaining_hours.rb@@ -0,0 +1,33 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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 WorkPackage::Exports+ module Formatters+ class RemainingHours < ::WorkPackage::Exports::Formatters::CompoundHours+ end+ end+enddiff --git a/config/initializers/export_formats.rb b/config/initializers/export_formats.rbindex ce0dc9e68db..ae50c624cb3 100644--- a/config/initializers/export_formats.rb+++ b/config/initializers/export_formats.rb@@ -7,6 +7,7 @@ Rails.application.configure do |application|single WorkPackage, WorkPackage::PDFExport::WorkPackageToPdfformatter WorkPackage, WorkPackage::Exports::Formatters::EstimatedHours+ formatter WorkPackage, WorkPackage::Exports::Formatters::RemainingHoursformatter WorkPackage, WorkPackage::Exports::Formatters::SpentUnitsformatter WorkPackage, WorkPackage::Exports::Formatters::Hoursformatter WorkPackage, WorkPackage::Exports::Formatters::Daysdiff --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.rbindex 7b35482de9a..e72be4e5c6f 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@@ -2,9 +2,10 @@ require "spec_helper"require "spreadsheet"RSpec.describe XlsExport::WorkPackage::Exporter::XLS do- let(:project) { create(:project) }+ shared_let(:project) { create(:project) }+ shared_let(:admin) { create(:admin) }- let(:current_user) { create(:admin) }+ let(:current_user) { admin }let(:column_names) { %w[type id subject status assigned_to priority] }let(:query) do@@ -203,7 +204,7 @@ RSpec.describe XlsExport::WorkPackage::Exporter::XLS do.and_return("costs_currency" => "EUR", "costs_currency_format" => "%n %u")end- it "successfullies export the work packages with a cost column" do+ it "exports successfully the work packages with a cost column" doexpect(sheet.rows.size).to eq(4 + 1)cost_column = sheet.columns.last.to_a@@ -215,7 +216,7 @@ RSpec.describe XlsExport::WorkPackage::Exporter::XLS docontext "with german locale" dolet(:current_user) { create(:admin, language: :de) }- it "successfullies export the work packages with a cost column localized" do+ it "exports successfully the work packages with a cost column localized" doI18n.with_locale :de dosheetend@@ -233,7 +234,7 @@ RSpec.describe XlsExport::WorkPackage::Exporter::XLS do# Check row after header rowhours = sheet.rows[1].values_at(2)- expect(hours).to include("27.5 h")+ expect(hours).to include("3d 3.5h")endend@@ -325,26 +326,29 @@ RSpec.describe XlsExport::WorkPackage::Exporter::XLS doit "adapts the datetime fields to the user time zone" dowork_package.reloadestimated_cell = sheet.rows.last.to_a.last- expect(estimated_cell).to eq "· Σ 15.0 h"+ expect(estimated_cell).to eq "· Σ 1d 7h"endend- describe "with derived estimated hours and estimated_hours set to zero" do+ describe "with estimated and remaining hours set to zero and their derived value set" dolet(:work_package) docreate(:work_package,project:,- derived_estimated_hours: 15.0,estimated_hours: 0.0,+ derived_estimated_hours: 15.0,+ remaining_hours: 0.0,+ derived_remaining_hours: 1225.89,type: project.types.first)endlet(:work_packages) { [work_package] }- let(:column_names) { %w[subject status updated_at estimated_hours] }+ let(:column_names) { %w[subject status updated_at estimated_hours remaining_hours] }it "outputs both values" dowork_package.reload- estimated_cell = sheet.rows.last.to_a.last- expect(estimated_cell).to eq "0.0 h · Σ 15.0 h"+ estimated_cell, remaining_cell = sheet.rows.last.to_a.last(2)+ expect(estimated_cell).to eq "0h · Σ 1d 7h"+ expect(remaining_cell).to eq "0h · Σ 1d 0h"endendenddiff --git a/spec/models/work_package/exporter/csv_integration_spec.rb b/spec/models/work_package/exporter/csv_integration_spec.rbindex 337bca713f7..f0565c4ce13 100644--- a/spec/models/work_package/exporter/csv_integration_spec.rb+++ b/spec/models/work_package/exporter/csv_integration_spec.rb@@ -75,6 +75,6 @@ RSpec.describe WorkPackage::Exports::CSV, "integration" doexpect(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 h")+ expect(data.last).to include("· Σ 1d 7h")endenddiff --git a/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rb b/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rbindex 6078c2c6378..a1b4b5e8e8c 100644--- a/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rb+++ b/spec/models/work_packages/pdf_export/work_package_to_pdf_spec.rb@@ -39,19 +39,20 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf dolet(:parent_project) docreate(:project, name: "Parent project")end- let(:project_custom_field_bool) { create(:project_custom_field, :boolean,- name: "Boolean project custom field") }- let(:project_custom_field_string) {+ let(:project_custom_field_bool) do+ create(:project_custom_field, :boolean,+ name: "Boolean project custom field")+ end+ let(:project_custom_field_string) docreate(:project_custom_field, :string,name: "Secret string", default_value: "admin eyes only",visible: false)- }- let(:project_custom_field_long_text) {+ end+ let(:project_custom_field_long_text) docreate(:project_custom_field, :text,name: "Rich text project custom field",- default_value: "rich text field value"- )- }+ default_value: "rich text field value")+ endlet(:project) docreate(:project,name: "Foo Bla. Report No. 4/2021 with/for Case 42",@@ -62,7 +63,7 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf doparent: parent_project,custom_field_values: {project_custom_field_bool.id => true,- project_custom_field_long_text.id => "foo",+ project_custom_field_long_text.id => "foo"},work_package_custom_fields: [cf_long_text, cf_disabled_in_project, cf_global_bool],work_package_custom_field_ids: [cf_long_text.id, cf_global_bool.id]) # cf_disabled_in_project.id is disabled@@ -96,8 +97,10 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf dolet(:image_attachment) { Attachment.new author: user, file: File.open(image_path) }let(:attachments) { [image_attachment] }let(:cf_long_text_description) { "" }- let(:cf_long_text) { create(:issue_custom_field, :text,- name: "Work Package Custom Field Long Text") }+ let(:cf_long_text) do+ create(:issue_custom_field, :text,+ name: "Work Package Custom Field Long Text")+ endlet!(:cf_disabled_in_project) do# NOT enabled by project.work_package_custom_field_ids => NOT in PDFcreate(:float_wp_custom_field, name: "DisabledCustomField")@@ -173,12 +176,12 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf docreated_at: export_time,updated_at: export_time,author: another_user,- assigned_to: another_user- ).tap do |wp|- allow(wp)- .to receive(:attachments)- .and_return attachments- end+ assigned_to: another_user)+ .tap do |wp|+ allow(wp)+ .to receive(:attachments)+ .and_return attachments+ endendlet(:options) { {} }let(:exporter) do@@ -221,6 +224,7 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf dodescribe "with a request for a PDF" dodescribe "with rich text and images" dolet(:cf_long_text_description) { "foo" }+it "contains correct data" doresult = pdf[:strings]expected_result = [@@ -248,12 +252,12 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do["category", category.name],["createdAt", export_time_formatted],["updatedAt", export_time_formatted],- ["estimatedTime", "10.0 h"],- ["remainingTime", "9.0 h"],+ ["estimatedTime", "1d 2h"],+ ["remainingTime", "1d 1h"],["version", version.name],["responsible", user.name],["dueDate", "05/30/2024"],- ["spentTime", "0.0 h"],+ ["spentTime", "0h"],["startDate", "05/30/2024"],["parent", "#{type.name} ##{parent_work_package.id}: #{parent_work_package.name}"],["priority", priority.name],@@ -298,6 +302,7 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf doworkPackageValue:#{forbidden_work_package.id}:assigneeDESCRIPTIONend+it "contains resolved attributes and labels" doresult = pdf[:strings]expected_result = [@@ -317,7 +322,7 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do"workPackageValue:3:assignee", "workPackageLabel:assignee","Work package not found: ","[#{I18n.t('export.macro.error', message:- I18n.t('export.macro.resource_not_found', resource: "WorkPackage 1234567890"))}] ",+ I18n.t('export.macro.resource_not_found', resource: 'WorkPackage 1234567890'))}] ","Access denied: ","[#{I18n.t('export.macro.error', message:I18n.t('export.macro.resource_not_found', resource: "WorkPackage #{forbidden_work_package.id}"))}]",@@ -382,6 +387,7 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf doprojectValue:"#{forbidden_project.identifier}":activeDESCRIPTIONend+it "contains resolved attributes and labels" doresult = pdf[:strings]expected_result = [@@ -406,13 +412,13 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do"Project by identifier:", " ", I18n.t(:general_text_Yes),"Project not found: ","[#{I18n.t('export.macro.error', message:- I18n.t('export.macro.resource_not_found', resource: "Project 1234567890"))}] ",+ I18n.t('export.macro.resource_not_found', resource: 'Project 1234567890'))}] ","Access denied: ","[#{I18n.t('export.macro.error', message:I18n.t('export.macro.resource_not_found', resource: "Project #{forbidden_project.id}"))}] ","Access denied by identifier:", " ", "[Macro error, resource not found: Project", "forbidden-project]",- "2", export_time_formatted, project.name,+ "2", export_time_formatted, project.name].flattenexpect(result.join(" ")).to eq(expected_result.join(" "))end