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
[#60404] Hierarchy custom field - pdf output display only last item without hierarchy
as-op56aef1037720
diff --git a/app/models/exports/formatters/custom_field.rb b/app/models/exports/formatters/custom_field.rbindex ef55bd0aff2..3c536bcafd7 100644--- a/app/models/exports/formatters/custom_field.rb+++ b/app/models/exports/formatters/custom_field.rb@@ -1,3 +1,33 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 Exportsmodule Formattersclass CustomField < Default@@ -20,6 +50,7 @@ module Exports# - For boolean values, don't use the Yes/No formatting for the UI# treat nil as false# - For long text values, output the plain value+ # - For hierarchy values, print out the full hierarchy of the item(s)def format_for_export(object, custom_field)case custom_field.field_formatwhen "bool"@@ -27,6 +58,8 @@ module Exportsvalue == nil ? false : valuewhen "text"object.typed_custom_value_for(custom_field)+ when "hierarchy"+ ::Exports::Formatters::Hierarchy::HierarchyFormatter.new.format(object, custom_field)elseobject.formatted_custom_value_for(custom_field)enddiff --git a/app/models/exports/formatters/custom_field_pdf.rb b/app/models/exports/formatters/custom_field_pdf.rbindex 3dba22f0796..351e72e23f2 100644--- a/app/models/exports/formatters/custom_field_pdf.rb+++ b/app/models/exports/formatters/custom_field_pdf.rb@@ -1,3 +1,33 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 Exportsmodule Formattersclass CustomFieldPdf < CustomField@@ -6,52 +36,18 @@ module Exportsend##- # Print the value meant for export.+ # Print the value meant for PDF export.## - For boolean values, use the Yes/No formatting for the PDF# treat nil as false- # - For long text values, output the plain valuedef format_for_export(object, custom_field)- case custom_field.field_format- when "bool"+ if custom_field.field_format == "bool"value = object.typed_custom_value_for(custom_field)value ? I18n.t(:general_text_Yes) : I18n.t(:general_text_No)- when "text"- object.typed_custom_value_for(custom_field)- when "hierarchy"- format_hierarchy_for_export(object, custom_field)- else- object.formatted_custom_value_for(custom_field)- end- end-- def format_hierarchy_for_export(object, custom_field)- cvs = object.custom_value_for(custom_field)- case cvs- when Array- cvs.map { |item| format_hierarchy_item_for_export(item) }- when CustomValue- format_hierarchy_item_for_export(cvs)else- cvs+ superendend-- def format_hierarchy_item_for_export(item_value)- item = ::CustomField::Hierarchy::Item.find_by(id: item_value.to_s)- return "#{item_value} #{I18n.t(:label_not_found)}" if item.nil?-- nodes = hierarchy_item_service.get_branch(item:).value!- nodes- .reject(&:root?)- .map do |branch_item|- branch_item.short.present? ? "#{branch_item.label} (#{branch_item.short})" : branch_item.label- end.join(" / ")- end-- def hierarchy_item_service- ::CustomFields::Hierarchy::HierarchicalItemService.new- endendendenddiff --git a/app/models/exports/formatters/hierarchy/hierarchy_formatter.rb b/app/models/exports/formatters/hierarchy/hierarchy_formatter.rbnew file mode 100644index 00000000000..2f930da8983--- /dev/null+++ b/app/models/exports/formatters/hierarchy/hierarchy_formatter.rb@@ -0,0 +1,65 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 Exports+ module Formatters+ module Hierarchy+ class HierarchyFormatter+ def format(object, custom_field)+ cvs = object.custom_value_for(custom_field)+ case cvs+ when Array+ cvs.map { |item| format_hierarchy_item_for_export(item) }.join(", ")+ when CustomValue+ format_hierarchy_item_for_export(cvs)+ else+ cvs+ end+ end++ def format_hierarchy_item_for_export(item_value)+ item = ::CustomField::Hierarchy::Item.find_by(id: item_value.to_s)+ return "#{item_value} #{I18n.t(:label_not_found)}" if item.nil?++ nodes = hierarchy_item_service.get_branch(item:).value!+ nodes+ .reject(&:root?)+ .map do |branch_item|+ branch_item.short.present? ? "#{branch_item.label} (#{branch_item.short})" : branch_item.label+ end.join(" / ")+ end++ def hierarchy_item_service+ ::CustomFields::Hierarchy::HierarchicalItemService.new+ end+ end+ end+ end+enddiff --git a/spec/models/exports/formatters/custom_field_hierarchy_spec.rb b/spec/models/exports/formatters/custom_field_hierarchy_spec.rbnew file mode 100644index 00000000000..4c9f832497a--- /dev/null+++ b/spec/models/exports/formatters/custom_field_hierarchy_spec.rb@@ -0,0 +1,93 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+#++++require "spec_helper"++RSpec.describe Exports::Formatters::CustomField do+ let(:service) { CustomFields::Hierarchy::HierarchicalItemService.new }+ let(:custom_field) { create(:custom_field, field_format: "hierarchy", hierarchy_root: nil) }+ let(:root) { service.generate_root(custom_field).value! }+ let!(:homer) { service.insert_item(parent: root, label: "Homer", short: "HS").value! }+ let!(:bart) { service.insert_item(parent: homer, label: "Bart", short: "BS").value! }+ let!(:lisa) { service.insert_item(parent: homer, label: "Lisa").value! }+ let(:work_package) do+ cf = build_stubbed(:work_package)+ allow(cf)+ .to receive(:custom_value_for)+ .and_return(custom_values)+ cf+ end+ let!(:custom_values) { [] }++ subject { described_class.new("cf_hierarchy") }++ describe "#format_for_export" do+ describe "with empty values" do+ it "returns empty string" do+ expect(subject.format_for_export(work_package, custom_field)).to eq("")+ end+ end++ describe "with a single label with short" do+ let(:custom_values) do+ [CustomValue.new(custom_field:, value: bart.id)]+ end++ it "returns the label and short" do+ expect(subject.format_for_export(work_package, custom_field)).to eq("Homer (HS) / Bart (BS)")+ end+ end++ describe "with a single label only" do+ let(:custom_values) do+ [CustomValue.new(custom_field:, value: lisa.id)]+ end++ it "returns the label and short" do+ expect(subject.format_for_export(work_package, custom_field)).to eq("Homer (HS) / Lisa")+ end+ end++ describe "with a multiple values" do+ let(:custom_values) do+ [+ CustomValue.new(custom_field:, value: homer.id),+ CustomValue.new(custom_field:, value: bart.id),+ CustomValue.new(custom_field:, value: lisa.id)+ ]+ end++ it "returns mulitple comma-separated values" do+ expect(subject.format_for_export(work_package, custom_field))+ .to eq("Homer (HS), Homer (HS) / Bart (BS), Homer (HS) / Lisa")+ end+ end+ end+end