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

refactored project attributes update via project overview page dialogs and enable reset of multi value custom fields

Jonas Jabari390d1bef3986
diff --git a/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.rb b/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.rb
index d914a9975eb..6e7fd1df73a 100644
--- a/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.rb
+++ b/modules/overviews/app/components/project_custom_fields/sections/edit_dialog_component.rb
@@ -70,38 +70,40 @@ module ProjectCustomFields
end
def render_single_value_custom_field_input(form, custom_field, custom_field_value)
+ form_args = { custom_field:, custom_field_value:, project: @project }
+
case custom_field.field_format
when "string"
- render(Project::CustomValueForm::String.new(form, custom_field:, custom_field_value:, project: @project))
+ render(Project::CustomValueForm::String.new(form, **form_args))
when "text"
- render(Project::CustomValueForm::Text.new(form, custom_field:, custom_field_value:, project: @project))
+ render(Project::CustomValueForm::Text.new(form, **form_args))
when "int"
- render(Project::CustomValueForm::Int.new(form, custom_field:, custom_field_value:, project: @project))
+ render(Project::CustomValueForm::Int.new(form, **form_args))
when "float"
- render(Project::CustomValueForm::Float.new(form, custom_field:, custom_field_value:, project: @project))
+ render(Project::CustomValueForm::Float.new(form, **form_args))
when "list"
- render(Project::CustomValueForm::SingleSelectList.new(form, custom_field:, custom_field_value:, project: @project))
+ render(Project::CustomValueForm::SingleSelectList.new(form, **form_args))
when "date"
- render(Project::CustomValueForm::Date.new(form, custom_field:, custom_field_value:, project: @project))
+ render(Project::CustomValueForm::Date.new(form, **form_args))
when "bool"
- render(Project::CustomValueForm::Bool.new(form, custom_field:, custom_field_value:, project: @project))
+ render(Project::CustomValueForm::Bool.new(form, **form_args))
when "user"
- render(Project::CustomValueForm::SingleUserSelectList.new(form, custom_field:, custom_field_value:, project: @project))
+ render(Project::CustomValueForm::SingleUserSelectList.new(form, **form_args))
when "version"
- render(Project::CustomValueForm::SingleVersionSelectList.new(form, custom_field:, custom_field_value:,
- project: @project))
+ render(Project::CustomValueForm::SingleVersionSelectList.new(form, **form_args))
end
end
def render_multi_value_custom_field_input(form, custom_field, custom_field_values)
+ form_args = { custom_field:, custom_field_values:, project: @project }
+
case custom_field.field_format
when "list"
- render(Project::CustomValueForm::MultiSelectList.new(form, custom_field:, custom_field_values:, project: @project))
+ render(Project::CustomValueForm::MultiSelectList.new(form, **form_args))
when "user"
- render(Project::CustomValueForm::MultiUserSelectList.new(form, custom_field:, custom_field_values:, project: @project))
+ render(Project::CustomValueForm::MultiUserSelectList.new(form, **form_args))
when "version"
- render(Project::CustomValueForm::MultiVersionSelectList.new(form, custom_field:, custom_field_values:,
- project: @project))
+ render(Project::CustomValueForm::MultiVersionSelectList.new(form, **form_args))
end
end
end
diff --git a/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.html.erb b/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.html.erb
index ae66097c7c2..c4a0fc06047 100644
--- a/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.html.erb
+++ b/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.html.erb
@@ -6,10 +6,10 @@
end
custom_field_value_container.with_row do
- if @project_custom_field_value.blank?
+ if @project_custom_field_values.empty?
render(Primer::Beta::Text.new(color: :subtle)) { t('label_not_set_yet') }
else
- render(Primer::Beta::Text.new()) { formated_value }
+ render(Primer::Beta::Text.new()) { render_formated_value }
end
end
end
diff --git a/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.rb b/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.rb
index 602bb6f1820..0a877733c75 100644
--- a/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.rb
+++ b/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.rb
@@ -33,25 +33,43 @@ module ProjectCustomFields
include ApplicationHelper
include OpPrimer::ComponentHelpers
- def initialize(project_custom_field:, project_custom_field_value:)
+ def initialize(project_custom_field:, project_custom_field_values:)
super
@project_custom_field = project_custom_field
- @project_custom_field_value = project_custom_field_value
+ @project_custom_field_values = project_custom_field_values
end
private
- def formated_value
- return if @project_custom_field_value.blank?
+ def render_formated_value
+ return if @project_custom_field_values.empty?
+ if @project_custom_field_values.one?
+ render_single_value(@project_custom_field_values.first)
+ else
+ render_multiple_values(@project_custom_field_values)
+ end
+ end
+
+ def render_single_value(value)
+ formated_value(value)
+ end
+
+ def render_multiple_values(values)
+ values.map do |value|
+ formated_value(value)
+ end.join(", ")
+ end
+
+ def formated_value(value)
case @project_custom_field.field_format
when "text"
- ::OpenProject::TextFormatting::Renderer.format_text(@project_custom_field_value.typed_value)
+ ::OpenProject::TextFormatting::Renderer.format_text(value.typed_value)
when "date"
- format_date(@project_custom_field_value.typed_value)
+ format_date(value.typed_value)
else
- @project_custom_field_value.typed_value&.to_s
+ value.typed_value&.to_s
end
end
end
diff --git a/modules/overviews/app/components/project_custom_fields/sections/show_component.html.erb b/modules/overviews/app/components/project_custom_fields/sections/show_component.html.erb
index 6a7f26a3d00..66c999d7ae6 100644
--- a/modules/overviews/app/components/project_custom_fields/sections/show_component.html.erb
+++ b/modules/overviews/app/components/project_custom_fields/sections/show_component.html.erb
@@ -24,7 +24,7 @@
details_container.with_row(mb: 3) do
render(ProjectCustomFields::Sections::ProjectCustomFields::ShowComponent.new(
project_custom_field: project_custom_field,
- project_custom_field_value: get_eager_loaded_project_custom_field_value_for(project_custom_field.id)
+ project_custom_field_values: get_eager_loaded_project_custom_field_values_for(project_custom_field.id)
))
end
end
diff --git a/modules/overviews/app/components/project_custom_fields/sections/show_component.rb b/modules/overviews/app/components/project_custom_fields/sections/show_component.rb
index d7463350800..45a49107b7d 100644
--- a/modules/overviews/app/components/project_custom_fields/sections/show_component.rb
+++ b/modules/overviews/app/components/project_custom_fields/sections/show_component.rb
@@ -60,8 +60,8 @@ module ProjectCustomFields
@project_custom_fields.sort_by { |pcf| pcf.position_in_custom_field_section }
end
- def get_eager_loaded_project_custom_field_value_for(custom_field_id)
- @eager_loaded_project_custom_field_values.find { |pcfv| pcfv.custom_field_id == custom_field_id }
+ def get_eager_loaded_project_custom_field_values_for(custom_field_id)
+ @eager_loaded_project_custom_field_values.select { |pcfv| pcfv.custom_field_id == custom_field_id }
end
end
end
diff --git a/modules/overviews/app/controllers/overviews/overviews_controller.rb b/modules/overviews/app/controllers/overviews/overviews_controller.rb
index e09105c6d52..35b431f3264 100644
--- a/modules/overviews/app/controllers/overviews/overviews_controller.rb
+++ b/modules/overviews/app/controllers/overviews/overviews_controller.rb
@@ -20,11 +20,8 @@ module ::Overviews
def attribute_section_dialog
section = ProjectCustomFieldSection.find(params[:section_id])
- active_project_custom_fields_of_section = active_project_custom_fields_grouped_by_section[section.id]
- .sort_by(&:position_in_custom_field_section)
-
eager_loaded_project_custom_field_values = CustomValue.where(
- custom_field_id: active_project_custom_fields_of_section.pluck(:id),
+ custom_field_id: active_project_custom_fields_of_section(section.id).pluck(:id),
customized_id: @project.id
).to_a
@@ -32,7 +29,7 @@ module ::Overviews
ProjectCustomFields::Sections::EditDialogComponent.new(
project: @project,
project_custom_field_section: section,
- active_project_custom_fields_of_section:,
+ active_project_custom_fields_of_section: active_project_custom_fields_of_section(section.id),
project_custom_field_values: eager_loaded_project_custom_field_values
),
layout: false
@@ -40,85 +37,27 @@ module ::Overviews
end
def update_attributes
- # prototypical implementation
- # manual nested attributes update as the project model is not yet natively supporting it
- # needs refactoring
-
- section = ProjectCustomFieldSection.find(params[:section_id])
-
- active_project_custom_fields_of_section = active_project_custom_fields_grouped_by_section[section.id]
- .sort_by(&:position_in_custom_field_section)
-
- modified_custom_field_values = []
+ section = find_project_custom_field_section
has_errors = false
ActiveRecord::Base.transaction do
- # transaction to rollback if any of the custom field values fails to save
- project_attribute_params[:custom_field_values_attributes]&.each do |custom_value_id, attributes|
- custom_value = CustomValue.find(custom_value_id.to_i)
-
- custom_value.value = attributes[:value]
- has_errors = true if custom_value.invalid?
- modified_custom_field_values << custom_value
- end
-
- project_attribute_params[:new_custom_field_values_attributes]&.each do |custom_field_id, attributes|
- custom_value = CustomValue.new(
- custom_field_id: custom_field_id.to_i,
- value: attributes[:value],
- customized_type: "Project",
- customized_id: @project.id
- )
+ modified_custom_field_values = update_custom_field_values(section)
+ modified_custom_field_values = mark_required_missing_custom_values(section, modified_custom_field_values)
- has_errors = true if custom_value.invalid?
- modified_custom_field_values << custom_value
- end
-
- # TODO: Cannot detect if all values are removed from a multi value custom field
- # autocompleter does not send '_blank' as value when no option is selected as configured
- project_attribute_params[:multi_custom_field_values_attributes]&.each do |custom_field_id, attributes|
- # Detect removed values
- @project.custom_values
- .where(custom_field_id: custom_field_id.to_i)
- .where.not(value: attributes[:values])
- .destroy_all
-
- attributes[:values]&.each do |value|
- custom_value = CustomValue.find_or_initialize_by(
- custom_field_id: custom_field_id.to_i,
- value:,
- customized_type: "Project",
- customized_id: @project.id
- )
-
- has_errors = true if custom_value.invalid?
- modified_custom_field_values << custom_value
- end
- end
+ has_errors = modified_custom_field_values.any?(&:invalid?)
if has_errors
- update_via_turbo_stream(
- component: ProjectCustomFields::Sections::EditDialogComponent.new(
- project: @project,
- project_custom_field_section: section,
- active_project_custom_fields_of_section:,
- project_custom_field_values: modified_custom_field_values
- )
- )
+ handle_errors(section, modified_custom_field_values)
else
- modified_custom_field_values.each(&:save!)
- update_via_turbo_stream(
- component: ProjectCustomFields::SidebarComponent.new(
- project: @project,
- project_custom_field_sections: ProjectCustomFieldSection.all,
- active_project_custom_fields_grouped_by_section:
- )
- )
+ save_custom_field_values(modified_custom_field_values)
+ delete_missing_custom_field_values(section, modified_custom_field_values)
+
+ update_sidebar_component
end
end
- respond_with_turbo_streams
+ respond_to_with_turbo_streams(status: has_errors ? :unprocessable_entity : :ok)
end
def jump_to_project_menu_item
@@ -150,5 +89,157 @@ module ::Overviews
.sort_by { |pcf| pcf.project_custom_field_section.position }
.group_by(&:custom_field_section_id)
end
+
+ def active_project_custom_fields_of_section(section_id)
+ active_project_custom_fields_grouped_by_section[section_id]
+ .sort_by(&:position_in_custom_field_section)
+ end
+
+ def find_project_custom_field_section
+ ProjectCustomFieldSection.find(params[:section_id])
+ end
+
+ def update_custom_field_values(section)
+ custom_field_values = []
+
+ transaction_custom_field_values(section, :custom_field_values_attributes) do |custom_value_id, attributes|
+ custom_value = update_custom_value(custom_value_id, attributes)
+ custom_field_values << custom_value
+ end
+
+ transaction_custom_field_values(section, :new_custom_field_values_attributes) do |custom_field_id, attributes|
+ custom_value = build_new_custom_value(custom_field_id, attributes)
+ custom_field_values << custom_value
+ end
+
+ transaction_custom_field_values(section, :multi_custom_field_values_attributes) do |custom_field_id, attributes|
+ custom_field_values.concat(update_multi_custom_field_values(custom_field_id, attributes))
+ end
+
+ custom_field_values
+ end
+
+ def transaction_custom_field_values(_section, attribute_key)
+ project_attribute_params[attribute_key]&.each do |custom_value_id, attributes|
+ yield(custom_value_id.to_i, attributes)
+ end
+ end
+
+ def update_custom_value(custom_value_id, attributes)
+ custom_value = CustomValue.find(custom_value_id.to_i)
+ custom_value.value = attributes[:value]
+ custom_value
+ end
+
+ def build_new_custom_value(custom_field_id, attributes)
+ CustomValue.new(
+ custom_field_id: custom_field_id.to_i,
+ value: attributes[:value],
+ customized_type: "Project",
+ customized_id: @project.id
+ )
+ end
+
+ def update_multi_custom_field_values(custom_field_id, attributes)
+ custom_field_values = []
+
+ existing_values_to_keep = attributes[:values] || []
+ remove_unused_multi_values(custom_field_id, existing_values_to_keep)
+
+ existing_values_to_keep.each do |value|
+ custom_value = find_or_initialize_custom_value(custom_field_id, value)
+ custom_field_values << custom_value
+ end
+
+ custom_field_values
+ end
+
+ def remove_unused_multi_values(custom_field_id, existing_values_to_keep)
+ @project.custom_values
+ .where(custom_field_id: custom_field_id.to_i)
+ .where.not(value: existing_values_to_keep)
+ .destroy_all
+ end
+
+ def find_or_initialize_custom_value(custom_field_id, value)
+ CustomValue.find_or_initialize_by(
+ custom_field_id: custom_field_id.to_i,
+ value:,
+ customized_type: "Project",
+ customized_id: @project.id
+ )
+ end
+
+ def handle_errors(section, modified_custom_field_values)
+ update_via_turbo_stream(
+ component: ProjectCustomFields::Sections::EditDialogComponent.new(
+ project: @project,
+ project_custom_field_section: section,
+ active_project_custom_fields_of_section: active_project_custom_fields_of_section(section.id),
+ project_custom_field_values: modified_custom_field_values
+ )
+ )
+ end
+
+ def save_custom_field_values(modified_custom_field_values)
+ modified_custom_field_values.each(&:save!)
+ end
+
+ def handle_missing_values(section, modified_custom_field_values)
+ mark_missing_values_as_required(section, modified_custom_field_values)
+ end
+
+ def get_missing_custom_field_ids(section, modified_custom_field_values)
+ custom_field_ids_of_section = active_project_custom_fields_of_section(section.id).pluck(:id)
+ modified_custom_field_ids = modified_custom_field_values.pluck(:custom_field_id)
+
+ custom_field_ids_of_section - modified_custom_field_ids
+ end
+
+ def delete_missing_custom_field_values(section, modified_custom_field_values)
+ missing_custom_field_ids = get_missing_custom_field_ids(section, modified_custom_field_values)
+
+ non_required_custom_field_ids = ProjectCustomField
+ .where(id: missing_custom_field_ids)
+ .where.not(is_required: true)
+ .pluck(:id)
+
+ CustomValue
+ .where(custom_field_id: non_required_custom_field_ids, customized_id: @project.id)
+ .destroy_all
+ end
+
+ def mark_required_missing_custom_values(section, modified_custom_field_values)
+ missing_custom_field_ids = get_missing_custom_field_ids(section, modified_custom_field_values)
+
+ required_custom_field_ids = ProjectCustomField
+ .where(id: missing_custom_field_ids)
+ .where(is_required: true)
+ .pluck(:id)
+
+ required_custom_field_ids.each do |custom_field_id|
+ custom_value = CustomValue.find_or_initialize_by(
+ custom_field_id: custom_field_id.to_i,
+ customized_type: "Project",
+ customized_id: @project.id
+ )
+ custom_value.value = nil
+ custom_value.errors.add(:value, :blank)
+
+ modified_custom_field_values << custom_value
+ end
+
+ modified_custom_field_values
+ end
+
+ def update_sidebar_component
+ update_via_turbo_stream(
+ component: ProjectCustomFields::SidebarComponent.new(
+ project: @project,
+ project_custom_field_sections: ProjectCustomFieldSection.all,
+ active_project_custom_fields_grouped_by_section:
+ )
+ )
+ end
end
end