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

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.erb
index 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.name
end
end
- description_container.with_column(data: { qa_selector: "custom-field-type" } ) do
+ description_container.with_column(mr: 2, data: { qa_selector: "custom-field-type" } ) do
render(Primer::Beta::Text.new(font_size: :small, color: :subtle)) do
@project_custom_field.field_format.capitalize
end
end
end
- 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
+ ))
+ end
end
end
end
diff --git a/app/contracts/project_custom_field_project_mappings/base_contract.rb b/app/contracts/project_custom_field_project_mappings/base_contract.rb
index 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 ProjectCustomFieldProjectMappings
attribute :custom_field_id
validate :validate_has_select_project_custom_fields_permission
+ validate :validate_is_not_required
def validate_has_select_project_custom_fields_permission
return if user.allowed_in_project?(:select_project_custom_fields, model.project)
errors.add :base, :error_unauthorized
end
+
+ 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
+ end
end
end
diff --git a/app/models/project_custom_field.rb b/app/models/project_custom_field.rb
index 489b12f0966..539724a4ad3 100644
--- a/app/models/project_custom_field.rb
+++ b/app/models/project_custom_field.rb
@@ -33,7 +33,7 @@ class ProjectCustomField < CustomField
acts_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_projects
def type_name
:label_project_plural
@@ -47,9 +47,15 @@ class ProjectCustomField < CustomField
end
end
- # 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)
+ end
end
diff --git a/app/services/project_custom_field_project_mappings/bulk_edit_service.rb b/app/services/project_custom_field_project_mappings/bulk_edit_service.rb
index 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 ProjectCustomFieldProjectMappings
end
def 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)
end
def enable_custom_fields(custom_field_ids)