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

force all required project custom fields to be enabled in all projects

Jonas Jabari4b3fb79171a2
diff --git a/app/models/project_custom_field.rb b/app/models/project_custom_field.rb
index 539724a4ad3..accd5426120 100644
--- a/app/models/project_custom_field.rb
+++ b/app/models/project_custom_field.rb
@@ -48,6 +48,7 @@ class ProjectCustomField < CustomField
end
# TODO: write specs for this
+ # TODO: write migrations for existing required custom fields
def activate_required_field_in_all_projects
return unless required?
diff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rb
index 6e0fb7a015c..a37ae5236b1 100644
--- a/app/models/projects/acts_as_customizable_patches.rb
+++ b/app/models/projects/acts_as_customizable_patches.rb
@@ -35,10 +35,35 @@ module Projects::ActsAsCustomizablePatches
# not needed for now, but might be relevant if we want to have edit dialogs just for one custom field
included do
+ has_many :project_custom_field_project_mappings, class_name: 'ProjectCustomFieldProjectMapping', foreign_key: :project_id,
+ dependent: :destroy, inverse_of: :project
+
+ before_save :build_missing_project_custom_field_project_mappings
+
+ def build_missing_project_custom_field_project_mappings
+ # activate custom fields for this project (via mapping table) if values have been provided for custom_fields but no mapping exists
+ # current shortcommings:
+ # - boolean custom fields are always activated as a nil value is never provided (always true/false)
+ # - custom fields with a default value are always activated as a nil value is never provided (even if set to blank in project creation form)
+ custom_field_ids = project.custom_values.reject { |cv| cv.value.blank? }.pluck(:custom_field_id)
+ activated_custom_field_ids = project_custom_field_project_mappings.pluck(:custom_field_id)
+
+ mappings = (custom_field_ids - activated_custom_field_ids)
+ .map { |pcf_id| { project_id: id, custom_field_id: pcf_id } }
+
+ project_custom_field_project_mappings.build(mappings)
+ end
+
def active_custom_field_ids_of_project
- @active_custom_field_ids_of_project ||= ProjectCustomFieldProjectMapping
- .where(project_id: project.id)
- .pluck(:custom_field_id)
+ # show all project custom fields in the project creation form
+ # later on, only those with values will be activated via before_save hook `build_missing_project_custom_field_project_mappings`
+ # a persisted project will then only show the activated custom fields
+ # this approach also supports project duplication based on project templates
+ if new_record?
+ ProjectCustomField.pluck(:id)
+ else
+ project_custom_field_project_mappings.pluck(:custom_field_id)
+ end
end
def available_custom_fields
diff --git a/db/migrate/20240208100316_enable_required_project_custom_fields_in_all_projects.rb b/db/migrate/20240208100316_enable_required_project_custom_fields_in_all_projects.rb
new file mode 100644
index 00000000000..9da66c00d58
--- /dev/null
+++ b/db/migrate/20240208100316_enable_required_project_custom_fields_in_all_projects.rb
@@ -0,0 +1,17 @@
+class EnableRequiredProjectCustomFieldsInAllProjects < ActiveRecord::Migration[7.1]
+ def up
+ required_project_custom_fields = ProjectCustomField.required.find_each.to_a
+
+ Project.includes(:project_custom_field_project_mappings).find_each do |project|
+ required_project_custom_fields.each do |pcf|
+ if project.project_custom_field_project_mappings.pluck(:custom_field_id).exclude?(pcf.id)
+ ProjectCustomFieldProjectMapping.create!(project_id: project.id, custom_field_id: pcf.id)
+ end
+ end
+ end
+ end
+
+ def down
+ # reversing this migration is not possible as we don't store the original state
+ end
+end