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

[33496] Don't try to lookup custom value by value

Oliver Güntheraf4010b003b2
diff --git a/app/helpers/work_packages_helper.rb b/app/helpers/work_packages_helper.rb
index 166cf010705..ffebdca6d24 100644
--- a/app/helpers/work_packages_helper.rb
+++ b/app/helpers/work_packages_helper.rb
@@ -80,7 +80,7 @@ module WorkPackagesHelper
parts[:link] << "##{h(package.id)}" if options[:id]
- parts[:link] << "#{h(package.status)}" if options[:id] && options[:status] && package.status
+ parts[:link] << "#{h(package.status)}" if options[:id] && options[:status] && package.status_id
# Hidden link part
diff --git a/app/models/custom_value.rb b/app/models/custom_value.rb
index 28e2857445f..e61df517646 100644
--- a/app/models/custom_value.rb
+++ b/app/models/custom_value.rb
@@ -100,6 +100,9 @@ class CustomValue < ApplicationRecord
end
def strategy
- @strategy ||= OpenProject::CustomFieldFormat.find_by_name(custom_field.field_format).formatter.new(self)
+ @strategy ||= begin
+ format = custom_field&.field_format || 'empty'
+ OpenProject::CustomFieldFormat.find_by_name(format).formatter.new(self)
+ end
end
end
diff --git a/app/models/custom_value/empty_strategy.rb b/app/models/custom_value/empty_strategy.rb
new file mode 100644
index 00000000000..fda72960e10
--- /dev/null
+++ b/app/models/custom_value/empty_strategy.rb
@@ -0,0 +1,40 @@
+#-- encoding: UTF-8
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 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-2017 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.
+#++
+
+class CustomValue::EmptyStrategy < CustomValue::FormatStrategy
+ def typed_value
+ "#{value} #{I18n.t(:label_not_found)}"
+ end
+
+ def formatted_value
+ typed_value
+ end
+
+ def validate_type_of_value; end
+end
diff --git a/app/models/custom_value/list_strategy.rb b/app/models/custom_value/list_strategy.rb
index 850985808e7..01e212884dd 100644
--- a/app/models/custom_value/list_strategy.rb
+++ b/app/models/custom_value/list_strategy.rb
@@ -49,7 +49,7 @@ class CustomValue::ListStrategy < CustomValue::ARObjectStrategy
def ar_object(value)
option = CustomOption.find_by(id: value.to_s)
if option.nil?
- "#{value} not found"
+ "#{value} #{I18n.t(:label_not_found)}"
else
option.value
end
diff --git a/config/initializers/custom_field_format.rb b/config/initializers/custom_field_format.rb
index b1c1f23b759..a69f0f2a9f7 100644
--- a/config/initializers/custom_field_format.rb
+++ b/config/initializers/custom_field_format.rb
@@ -56,6 +56,10 @@ OpenProject::CustomFieldFormat.map do |fields|
label: :label_boolean,
order: 7,
formatter: 'CustomValue::BoolStrategy')
+ fields.register OpenProject::CustomFieldFormat.new('empty',
+ label: :label_empty,
+ order: 7,
+ formatter: 'CustomValue::EmptyStrategy')
fields.register OpenProject::CustomFieldFormat.new('user',
label: Proc.new { User.model_name.human },
only: %w(WorkPackage TimeEntry
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 5f559c46f97..843f9fa6820 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1543,6 +1543,7 @@ en:
label_no_parent_page: "No parent page"
label_nothing_display: "Nothing to display"
label_nobody: "nobody"
+ label_not_found: 'not found'
label_none: "none"
label_none_parentheses: "(none)"
label_not_contains: "doesn't contain"
diff --git a/lib/api/v3/utilities/custom_field_injector.rb b/lib/api/v3/utilities/custom_field_injector.rb
index 180bd461068..4cf151a1158 100644
--- a/lib/api/v3/utilities/custom_field_injector.rb
+++ b/lib/api/v3/utilities/custom_field_injector.rb
@@ -34,6 +34,7 @@ module API
class CustomFieldInjector
TYPE_MAP = {
'string' => 'String',
+ 'empty' => 'String',
'text' => 'Formattable',
'int' => 'Integer',
'float' => 'Float',
diff --git a/modules/reporting/app/helpers/reporting_helper.rb b/modules/reporting/app/helpers/reporting_helper.rb
index 797ca56edfb..cd548f156c3 100644
--- a/modules/reporting/app/helpers/reporting_helper.rb
+++ b/modules/reporting/app/helpers/reporting_helper.rb
@@ -99,7 +99,7 @@ module ReportingHelper
end
def field_representation_map(key, value)
- return I18n.t(:label_none) if value.blank?
+ return I18n.t(:'placeholders.default') if value.blank?
case key.to_sym
when :activity_id then mapped value, Enumeration, "<i>#{l(:caption_material_costs)}</i>"
@@ -110,20 +110,30 @@ module ReportingHelper
when :tmonth then month_name(value.to_i)
when :category_id then h(Category.find(value.to_i).name)
when :cost_type_id then mapped value, CostType, l(:caption_labor)
- when :budget_id then budget_link value
+ when :budget_id then budget_link value
when :work_package_id then link_to_work_package(WorkPackage.find(value.to_i))
when :spent_on then format_date(value.to_date)
when :type_id then h(Type.find(value.to_i).name)
when :week then "#{l(:label_week)} #%s" % value.to_i.modulo(100)
when :priority_id then h(IssuePriority.find(value.to_i).name)
- when :version_id then h(Version.find(value.to_i).name)
+ when :version_id then h(Version.find(value.to_i).name)
when :singleton_value then ''
when :status_id then h(Status.find(value.to_i).name)
- when /custom_field\d+/ then CustomOption.find_by(id: value)&.value || value.to_s
+ when /custom_field\d+/ then custom_value(key, value)
else h(value.to_s)
end
end
+ def custom_value(cf_identifier, value)
+ cf_id = cf_identifier.gsub('custom_field', '').to_i
+
+ # Reuses rails cache to locate the custom field
+ # and then properly cast the value
+ CustomValue
+ .new(custom_field_id: cf_id, value: value)
+ .typed_value
+ end
+
def field_sort_map(key, value)
return '' if value.blank?
diff --git a/modules/reporting/spec/helpers/reporting_helper_spec.rb b/modules/reporting/spec/helpers/reporting_helper_spec.rb
index a91bc7bc2df..a51de1620c9 100644
--- a/modules/reporting/spec/helpers/reporting_helper_spec.rb
+++ b/modules/reporting/spec/helpers/reporting_helper_spec.rb
@@ -32,29 +32,46 @@ describe ReportingHelper, type: :helper do
describe '#field_representation_map' do
context 'for a custom field' do
context 'for which a custom option exists (e.g. a list field)' do
+ let(:custom_field) do
+ FactoryBot.create(
+ :list_wp_custom_field,
+ name: "Ingredients",
+ possible_values: ["ham"]
+ )
+ end
+
it 'returns the option value' do
- option = FactoryBot.build(:custom_option)
+ option = custom_field.possible_values.first
- allow(CustomOption)
- .to receive(:find_by)
- .with(id: "345")
- .and_return(option)
+ expect(field_representation_map("custom_field#{custom_field.id}", option.id))
+ .to eql 'ham'
+ end
- expect(field_representation_map("custom_field22", "345"))
- .to eql option.value
+ it 'returns not found for an outdated value value' do
+ expect(field_representation_map("custom_field#{custom_field.id}", "1234123"))
+ .to eql '1234123 not found'
+ end
+ end
+
+ context 'for which no custom option exists (e.g. a float field)' do
+ let(:custom_field) do
+ FactoryBot.create(
+ :float_wp_custom_field,
+ name: "Estimate"
+ )
+ end
+
+ it 'returns the option value' do
+ expect(field_representation_map("custom_field#{custom_field.id}", 3.0))
+ .to eql 3.0
end
end
end
context 'for which no custom option exists' do
- it 'returns the option value' do
- allow(CustomOption)
- .to receive(:find_by)
- .with(id: "345")
- .and_return(nil)
-
- expect(field_representation_map("custom_field22", "345"))
- .to eql "345"
+ it 'returns the not found value' do
+ expect(field_representation_map("custom_field12345", "345"))
+ .to eql "345 not found"
end
end
end
diff --git a/spec/models/custom_value_spec.rb b/spec/models/custom_value_spec.rb
index 3a7c22894fa..be8493ce8b1 100644
--- a/spec/models/custom_value_spec.rb
+++ b/spec/models/custom_value_spec.rb
@@ -96,6 +96,17 @@ describe CustomValue do
end
end
+ describe 'trying to use a custom field that does not exist' do
+ subject { FactoryBot.build(:custom_value, custom_field_id: 123412341, value: 'my value') }
+
+ it 'returns an empty placeholder' do
+ expect(subject.custom_field).to be_nil
+ expect(subject.send(:strategy)).to be_kind_of CustomValue::EmptyStrategy
+ expect(subject.typed_value).to eq 'my value not found'
+ expect(subject.formatted_value).to eq 'my value not found'
+ end
+ end
+
describe 'value/value=' do
let(:custom_value) { FactoryBot.build_stubbed(:custom_value) }
let(:strategy_double) { double('strategy_double') }