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

simplify & unify attribute value resolving

as-op6ab7b50e97c3
diff --git a/app/models/work_package/exports/macros/attributes.rb b/app/models/work_package/exports/macros/attributes.rb
index 90a8e9f892a..fa979b5e4d5 100644
--- a/app/models/work_package/exports/macros/attributes.rb
+++ b/app/models/work_package/exports/macros/attributes.rb
@@ -127,48 +127,26 @@ module WorkPackage::Exports
end
def self.resolve_value_project(project, attribute)
- cf = ProjectCustomField.find_by(name: attribute)
- if cf.nil?
- ar_name = ::API::Utilities::PropertyNameConverter.to_ar_name(attribute.to_sym, context: project)
- else
- ar_name = "cf_#{cf.id}"
- # currently we do not support embedding rich text fields: long text custom fields
- return msg_macro_error_rich_text if cf.formattable?
-
- # Is the user allowed to see this custom field/"project attribute"?
- if project.available_custom_fields.find { |pcf| pcf.id == cf.id }.nil?
- return msg_macro_error(I18n.t("export.macro.resource_not_found", resource: attribute))
- end
- end
-
- # currently we do not support embedding rich text field: e.g. projectValue:1234:description
- if DISABLED_PROJECT_RICH_TEXT_FIELDS.include?(ar_name.to_sym)
- return msg_macro_error_rich_text
-
- end
-
- format_attribute_value(ar_name, Project, project)
+ resolve_value(project, attribute, DISABLED_PROJECT_RICH_TEXT_FIELDS)
end
def self.resolve_value_work_package(work_package, attribute)
- cf = CustomField.find_by(name: attribute, type: "WorkPackageCustomField")
- if cf.nil?
- ar_name = ::API::Utilities::PropertyNameConverter.to_ar_name(attribute.to_sym, context: work_package)
- else
- ar_name = "cf_#{cf.id}"
- # currently we do not support embedding rich text fields: long text custom fields
- return msg_macro_error_rich_text if cf.formattable?
+ resolve_value(work_package, attribute, DISABLED_WORK_PACKAGE_RICH_TEXT_FIELDS)
+ end
- # TODO: Are there access restrictions on work_package custom fields?
- end
+ def self.resolve_value(obj, attribute, disabled_rich_text_fields)
+ cf = obj.available_custom_fields.find { |pcf| pcf.name == attribute }
- # currently we do not support embedding rich text field: workPackageValue:1234:description
- if DISABLED_WORK_PACKAGE_RICH_TEXT_FIELDS.include?(ar_name.to_sym)
- return msg_macro_error_rich_text
+ return msg_macro_error_rich_text if cf&.formattable?
- end
+ ar_name = if cf.nil?
+ ::API::Utilities::PropertyNameConverter.to_ar_name(attribute.to_sym, context: obj)
+ else
+ "cf_#{cf.id}"
+ end
+ return msg_macro_error_rich_text if disabled_rich_text_fields.include?(ar_name.to_sym)
- format_attribute_value(ar_name, WorkPackage, work_package)
+ format_attribute_value(ar_name, obj.class, obj)
end
def self.format_attribute_value(ar_name, model, obj)
diff --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.rb
index 814f76bc9a1..9a703b1fb54 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
@@ -61,7 +61,8 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
active: true,
parent: parent_project,
custom_field_values: {
- project_custom_field_bool.id => true
+ project_custom_field_bool.id => true,
+ 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
@@ -389,13 +390,13 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
"Custom field boolean", I18n.t(:general_text_Yes),
"Custom field rich text", "[#{I18n.t('export.macro.rich_text_unsupported')}]",
"Custom field hidden",
- "[#{I18n.t('export.macro.error', message:
- I18n.t('export.macro.resource_not_found', resource: "Secret string"))}]",
- "No replacement of:", "projectValue:1:status", " ", "projectLabel:status",
+ "No replacement of:", "projectValue:1:status", "projectLabel:status",
"projectValue:2:status projectLabel:status",
"projectValue:3:status", "projectLabel:status",
+ "1", export_time_formatted, project.name,
+
"Project not found: ",
"[#{I18n.t('export.macro.error', message:
I18n.t('export.macro.resource_not_found', resource: "Project 1234567890"))}] ",
@@ -403,7 +404,7 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
"[#{I18n.t('export.macro.error', message:
I18n.t('export.macro.resource_not_found', resource: "Project #{forbidden_project.id}"))}]",
- "1", export_time_formatted, project.name
+ "2", export_time_formatted, project.name,
].flatten
expect(result.join(" ")).to eq(expected_result.join(" "))
end