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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
disable custom fields with default values when user explicitly provided a blank value
Jonas Jabari0facd6fc0bda
diff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rbindex a37ae5236b1..e4ebb4fa890 100644--- a/app/models/projects/acts_as_customizable_patches.rb+++ b/app/models/projects/acts_as_customizable_patches.rb@@ -39,14 +39,14 @@ module Projects::ActsAsCustomizablePatchesdependent: :destroy, inverse_of: :projectbefore_save :build_missing_project_custom_field_project_mappings+ after_create :disable_custom_fields_with_empty_valuesdef 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)+ custom_field_ids = project.custom_values.reject { |cv| cv.value.blank? }.pluck(:custom_field_id).uniq+ activated_custom_field_ids = project_custom_field_project_mappings.pluck(:custom_field_id).uniqmappings = (custom_field_ids - activated_custom_field_ids).map { |pcf_id| { project_id: id, custom_field_id: pcf_id } }@@ -54,6 +54,19 @@ module Projects::ActsAsCustomizablePatchesproject_custom_field_project_mappings.build(mappings)end+ def disable_custom_fields_with_empty_values+ # run only on initial creation! (otherwise we would deactivate custom fields with empty values on every update!)+ #+ # ideally, `build_missing_project_custom_field_project_mappings` would not activate custom fields with empty values+ # but:+ # this hook is required as acts_as_customizable build custom values with their default value even if a blank value was provided in the project creation form+ # `build_missing_project_custom_field_project_mappings` will then activate the custom field although the user explicitly provided a blank value+ # in order to not patch `acts_as_customizable` further, we simply identify these custom values and deactivate the custom field+ custom_field_ids = project.custom_values.select { |cv| cv.value.blank? && !cv.required? }.pluck(:custom_field_id)++ project_custom_field_project_mappings.where(custom_field_id: custom_field_ids).destroy_all+ end+def active_custom_field_ids_of_project# 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`