[#69399] new project service considers both flags

This commit is contained in:
Tobias Dillmann
2025-12-04 15:55:22 +01:00
parent 92aee4780d
commit 2d3ea3adfe
3 changed files with 13 additions and 13 deletions
@@ -34,8 +34,8 @@ module ProjectCustomFieldProjectMappings
attribute :custom_field_id
validate :select_project_custom_fields_permission
validate :not_required
validate :visbile_to_user
validate :not_for_all_projects
validate :visible_to_user
def select_project_custom_fields_permission
return if user.allowed_in_project?(:select_project_custom_fields, model.project)
@@ -43,15 +43,15 @@ module ProjectCustomFieldProjectMappings
errors.add :base, :error_unauthorized
end
def not_required
# only mappings of custom fields which are not required can be manipulated by the user
# enabling a custom field which is required happens in an after_save hook within the custom field model itself
return if model.project_custom_field.nil? || !model.project_custom_field.required?
def not_for_all_projects
# only mappings of custom fields which are not activated for all projects can be manipulated by the user
# enabling a custom field which is force-active happens in an after_save hook within the custom field model itself
return if model.project_custom_field.nil? || !model.project_custom_field.is_for_all?
errors.add :custom_field_id, :cannot_delete_mapping
end
def visbile_to_user
def visible_to_user
# "invisible" custom fields can only be seen and edited by admins
# using visible scope to check if the custom field is actually visible to the user
return if model.project_custom_field.nil? ||
@@ -84,11 +84,11 @@ module ProjectCustomFieldProjectMappings
end
def fetch_custom_field_ids
# only custom fields which are not set to required can be disabled
# only custom fields which are set "for all projects" can be disabled
ProjectCustomField
.visible(@user)
.where(custom_field_section_id: @project_custom_field_section.id)
.where(is_required: false)
.where(is_for_all: false)
.pluck(:id)
end
@@ -93,7 +93,7 @@ module Projects::Concerns
# 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 = new_project.custom_values.select { |cv| cv.value.blank? && !cv.required? }.pluck(:custom_field_id)
custom_field_ids = new_project.custom_values.select { |cv| cv.value.blank? && !cv.is_for_all? }.pluck(:custom_field_id)
custom_field_project_mappings = new_project.project_custom_field_project_mappings
custom_field_project_mappings
@@ -104,11 +104,11 @@ module Projects::Concerns
end
def build_missing_project_custom_field_project_mappings(project)
# Activate all custom fields (via mapping table) that are required or
# have a value provided by the user, but no mapping exists.
# Activate all custom fields (via mapping table) that have no mapping, but are either
# intended for all projects, or have a value provided by the user.
custom_field_ids = project.custom_values
.select { |cv| cv.value? || cv.required? }
.select { |cv| cv.value? || cv.is_for_all? }
.pluck(:custom_field_id).uniq
activated_custom_field_ids = project.project_custom_field_project_mappings.pluck(:custom_field_id).uniq