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
WIP: enable required custom fields automatically in all projects and disallow disabling them on project level
Jonas Jabarifdf08efc3c9b
diff --git a/app/components/projects/settings/project_custom_field_sections/custom_field_row_component.html.erb b/app/components/projects/settings/project_custom_field_sections/custom_field_row_component.html.erbindex bc485e076a4..fb574113e04 100644--- a/app/components/projects/settings/project_custom_field_sections/custom_field_row_component.html.erb+++ b/app/components/projects/settings/project_custom_field_sections/custom_field_row_component.html.erb@@ -10,30 +10,40 @@@project_custom_field.nameendend- description_container.with_column(data: { qa_selector: "custom-field-type" } ) do+ description_container.with_column(mr: 2, data: { qa_selector: "custom-field-type" } ) dorender(Primer::Beta::Text.new(font_size: :small, color: :subtle)) do@project_custom_field.field_format.capitalizeendendend- custom_field_container.with_column() do- # buggy currently:- # small variant + status_label_position: :start leads to a small bounce while toggling- # behavior can be seen on primer's viewbook as well -> https://view-components-storybook.eastus.cloudapp.azure.com/view-components/lookbook/inspect/primer/alpha/toggle_switch/small- render(Primer::Alpha::ToggleSwitch.new(- src: toggle_project_settings_project_custom_fields_path(- project_custom_field_project_mapping: {- project_id: @project.id,- custom_field_id: @project_custom_field.id- }- ),- csrf_token: form_authenticity_token,- data: { 'turbo-method': :put, 'turbo-stream': true,- qa_selector: "toggle-project-custom-field-mapping-#{@project_custom_field.id}" },- checked: active_in_project?,- size: :small,- status_label_position: :start- ))+ custom_field_container.with_column(align_items: :center, flex_layout: true) do |toggle_container|+ if @project_custom_field.required?+ toggle_container.with_column(data: { qa_selector: "custom-field-type" } ) do+ render(Primer::Beta::Label.new(scheme: :attention, size: :medium)) do+ t("label_required")+ end+ end+ end+ toggle_container.with_column do+ # buggy currently:+ # small variant + status_label_position: :start leads to a small bounce while toggling+ # behavior can be seen on primer's viewbook as well -> https://view-components-storybook.eastus.cloudapp.azure.com/view-components/lookbook/inspect/primer/alpha/toggle_switch/small+ render(Primer::Alpha::ToggleSwitch.new(+ src: toggle_project_settings_project_custom_fields_path(+ project_custom_field_project_mapping: {+ project_id: @project.id,+ custom_field_id: @project_custom_field.id+ }+ ),+ csrf_token: form_authenticity_token,+ data: { 'turbo-method': :put, 'turbo-stream': true,+ qa_selector: "toggle-project-custom-field-mapping-#{@project_custom_field.id}" },+ checked: active_in_project?,+ enabled: !@project_custom_field.required?, # required fields cannot be disabled+ size: :small,+ status_label_position: :start+ ))+ endendendenddiff --git a/app/contracts/project_custom_field_project_mappings/base_contract.rb b/app/contracts/project_custom_field_project_mappings/base_contract.rbindex aa362cde087..fa50115f8bd 100644--- a/app/contracts/project_custom_field_project_mappings/base_contract.rb+++ b/app/contracts/project_custom_field_project_mappings/base_contract.rb@@ -32,11 +32,20 @@ module ProjectCustomFieldProjectMappingsattribute :custom_field_idvalidate :validate_has_select_project_custom_fields_permission+ validate :validate_is_not_requireddef validate_has_select_project_custom_fields_permissionreturn if user.allowed_in_project?(:select_project_custom_fields, model.project)errors.add :base, :error_unauthorizedend++ def validate_is_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?++ errors.add :custom_field_id, :invalid+ endendenddiff --git a/app/models/project_custom_field.rb b/app/models/project_custom_field.rbindex 489b12f0966..539724a4ad3 100644--- a/app/models/project_custom_field.rb+++ b/app/models/project_custom_field.rb@@ -33,7 +33,7 @@ class ProjectCustomField < CustomFieldacts_as_list column: :position_in_custom_field_section, scope: [:custom_field_section_id]- # after_create :activate_in_all_projects+ after_save :activate_required_field_in_all_projectsdef type_name:label_project_plural@@ -47,9 +47,15 @@ class ProjectCustomField < CustomFieldendend- # TODO: check if custom field is set to be activated in all projects in creation form- # def activate_in_all_projects- # mappings = Project.active.map { |project| { project_id: project.id, custom_field_id: id } }- # ProjectCustomFieldProjectMapping.create!(mappings)- # end+ # TODO: write specs for this+ def activate_required_field_in_all_projects+ return unless required?++ already_activated_in_project_ids = ProjectCustomFieldProjectMapping.where(custom_field_id: id).pluck(:project_id)++ mappings = Project.where.not(id: already_activated_in_project_ids).map do |project|+ { project_id: project.id, custom_field_id: id }+ end+ ProjectCustomFieldProjectMapping.create!(mappings)+ endenddiff --git a/app/services/project_custom_field_project_mappings/bulk_edit_service.rb b/app/services/project_custom_field_project_mappings/bulk_edit_service.rbindex e504b8aa092..0795b244a7f 100644--- a/app/services/project_custom_field_project_mappings/bulk_edit_service.rb+++ b/app/services/project_custom_field_project_mappings/bulk_edit_service.rb@@ -70,7 +70,8 @@ module ProjectCustomFieldProjectMappingsenddef fetch_custom_field_ids- @project_custom_field_section.custom_fields.pluck(:id)+ # only custom fields which are not set to required can be disabled+ @project_custom_field_section.custom_fields.where(is_required: false).pluck(:id)enddef enable_custom_fields(custom_field_ids)