diff --git a/app/models/work_package/exporter/formatters.rb b/app/models/work_package/exporter/formatters.rb index 0b6e86ac26..7b1f2beab0 100644 --- a/app/models/work_package/exporter/formatters.rb +++ b/app/models/work_package/exporter/formatters.rb @@ -1,10 +1,18 @@ module WorkPackage::Exporter module Formatters + def self.default_formatter_strings + @default_formatter_strings ||= %i[default costs estimated_hours].map do |key| + "WorkPackage::Exporter::Formatters::#{key.to_s.camelize}" + end + end + + def self.all_formatter_strings + @all_formatter_strings ||= default_formatter_strings + end + def self.all - @formatters ||= begin - %i[default costs estimated_hours].map do |key| - Kernel.const_get("WorkPackage::Exporter::Formatters::#{key.to_s.camelize}") - end + all_formatter_strings.map do |formatter_string| + Kernel.const_get(formatter_string) end end @@ -12,8 +20,8 @@ module WorkPackage::Exporter all.map(&:key) end - def self.register(clz) - @formatters << clz + def self.register(class_string) + @all_formatter_strings = all_formatter_strings + [class_string] end ## diff --git a/app/models/work_package/exporter/formatters/default.rb b/app/models/work_package/exporter/formatters/default.rb index 789b493203..320bc9b955 100644 --- a/app/models/work_package/exporter/formatters/default.rb +++ b/app/models/work_package/exporter/formatters/default.rb @@ -41,4 +41,4 @@ module WorkPackage::Exporter end end end -end \ No newline at end of file +end diff --git a/modules/bim/lib/open_project/bim/engine.rb b/modules/bim/lib/open_project/bim/engine.rb index fa51df1745..ebc77a7743 100644 --- a/modules/bim/lib/open_project/bim/engine.rb +++ b/modules/bim/lib/open_project/bim/engine.rb @@ -215,6 +215,7 @@ module OpenProject::Bim config.to_prepare do ::WorkPackage::Exporter .register_for_list(:bcf, OpenProject::Bim::BcfXml::Exporter) + ::WorkPackage::Exporter::Formatters.register("OpenProject::Bim::WorkPackage::Exporter::Formatters::BcfThumbnail") ::Queries::Register.filter ::Query, ::Bim::Queries::WorkPackages::Filter::BcfIssueAssociatedFilter ::Queries::Register.column ::Query, ::Bim::Queries::WorkPackages::Columns::BcfThumbnailColumn diff --git a/modules/bim/lib/open_project/bim/work_package/exporter/formatters/bcf_thumbnail.rb b/modules/bim/lib/open_project/bim/work_package/exporter/formatters/bcf_thumbnail.rb new file mode 100644 index 0000000000..2050145f9e --- /dev/null +++ b/modules/bim/lib/open_project/bim/work_package/exporter/formatters/bcf_thumbnail.rb @@ -0,0 +1,11 @@ +module OpenProject::Bim::WorkPackage::Exporter::Formatters + class BcfThumbnail < WorkPackage::Exporter::Formatters::Default + def self.apply?(column) + column.is_a? ::Bim::Queries::WorkPackages::Columns::BcfThumbnailColumn + end + + def format(work_package, _column, **_options) + work_package&.bcf_issue&.viewpoints&.any? ? "x" : '' + end + end +end diff --git a/modules/bim/spec/lib/open_project/bim/work_package/exporter/formatters/bcf_thumbnail_spec.rb b/modules/bim/spec/lib/open_project/bim/work_package/exporter/formatters/bcf_thumbnail_spec.rb new file mode 100644 index 0000000000..bedcb71cd4 --- /dev/null +++ b/modules/bim/spec/lib/open_project/bim/work_package/exporter/formatters/bcf_thumbnail_spec.rb @@ -0,0 +1,62 @@ +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details. +#++ + +require 'spec_helper' + +describe OpenProject::Bim::WorkPackage::Exporter::Formatters::BcfThumbnail do + let(:bcf_thumbnail_column) { ::Bim::Queries::WorkPackages::Columns::BcfThumbnailColumn.new("Some column name") } + let(:not_bcf_thumbnail_column) { "This not a BcfThumbnailColumn" } + + describe '::apply?' do + it 'returns TRUE for any other class' do + expect(described_class.apply?(bcf_thumbnail_column)).to be_truthy + end + + it 'returns FALSE for any other class' do + expect(described_class.apply?(not_bcf_thumbnail_column)).to be_falsey + end + end + + describe '::format' do + let(:work_package_with_viewpoint) { FactoryBot.create(:work_package) } + let(:bcf_issue) { FactoryBot.create(:bcf_issue_with_viewpoint, work_package: work_package_with_viewpoint) } + let(:work_package_without_viewpoint) { FactoryBot.create(:work_package) } + + before do + bcf_issue + end + + it 'returns "x" for work packages that have BCF issues with at least one viewpoint' do + expect(described_class.new.format(work_package_with_viewpoint, bcf_thumbnail_column)).to eql('x') + end + + it 'returns "" for work packages without viewpoints attached' do + expect(described_class.new.format(work_package_without_viewpoint, bcf_thumbnail_column)).to eql('') + end + end +end