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

Refactor and cache available_custom_fields methods

Dombi Attila02d890bc0cae
diff --git a/app/models/projects/custom_fields.rb b/app/models/projects/custom_fields.rb
index 55369e14c72..1b6c92bacaf 100644
--- a/app/models/projects/custom_fields.rb
+++ b/app/models/projects/custom_fields.rb
@@ -40,19 +40,15 @@ module Projects::CustomFields
# overrides acts_as_customizable
# in contrast to acts_as_customizable, custom_fields are enabled per project
# thus we need to check the project_custom_field_project_mappings
- custom_fields = all_available_custom_fields.visible
+ visible_fields = all_available_custom_fields.visible
+ return visible_fields if new_record?
- unless new_record?
- custom_fields = custom_fields
- .where(id: project_custom_field_project_mappings.select(:custom_field_id))
- .or(ProjectCustomField.required)
- end
-
- custom_fields
+ visible_fields.where(id: project_custom_field_project_mappings.select(:custom_field_id))
+ .or(ProjectCustomField.required)
end
def all_available_custom_fields
- ProjectCustomField
+ @all_available_custom_fields ||= ProjectCustomField
.includes(:project_custom_field_section)
.order("custom_field_sections.position", :position_in_custom_field_section)
end
diff --git a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
index cc23ec17cda..7b94e34a5f2 100644
--- a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
+++ b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
@@ -73,7 +73,7 @@ module Redmine
end
def all_available_custom_fields
- available_custom_fields
+ @all_available_custom_fields ||= available_custom_fields
end
# Sets the values of the object's custom fields
@@ -214,6 +214,7 @@ module Redmine
def reset_custom_values_change_tracker
@custom_field_values_cache = nil
+ @all_available_custom_fields = nil
self.custom_value_destroyed = false
end