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
refactored custom value update to contract/service based approach
Jonas Jabaric98ddc1a9272
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rbindex 84bfde71333..4b64a68872e 100644--- a/app/contracts/projects/base_contract.rb+++ b/app/contracts/projects/base_contract.rb@@ -50,6 +50,10 @@ module Projectsvalidate_templated_set_by_adminend+ attribute :touched_custom_field_section_id+ # `touched_custom_field_section_id` used in Projects::ActsAsCustomizablePatches in order to+ # only validate custom fields of the touched section+validate :validate_user_allowed_to_managedef assignable_parentsdiff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rbindex 321792f33e5..81963e4bb15 100644--- a/app/models/projects/acts_as_customizable_patches.rb+++ b/app/models/projects/acts_as_customizable_patches.rb@@ -29,6 +29,8 @@module Projects::ActsAsCustomizablePatchesextend ActiveSupport::Concern+ attr_accessor :touched_custom_field_section_id+included dodef active_custom_field_ids_of_project@active_custom_field_ids_of_project ||= ProjectCustomFieldProjectMapping@@ -85,18 +87,11 @@ module Projects::ActsAsCustomizablePatchesenddef of_touched_custom_field_section?(custom_value)- if @touched_section_id.present?- custom_field_section_ids[custom_value.custom_field_id] == @touched_section_id+ if touched_custom_field_section_id.present?+ custom_field_section_ids[custom_value.custom_field_id] == touched_custom_field_section_idelsetrue # validate all custom values if no specific section was marked as touched via `update_custom_field_values_of_section`endend-- def update_custom_field_values_of_section(section, params)- # we need to set the touched section id to validate only the custom values of the touched section- @touched_section_id = section.id-- update(params)- endendenddiff --git a/modules/overviews/app/controllers/overviews/overviews_controller.rb b/modules/overviews/app/controllers/overviews/overviews_controller.rbindex b0e780fd1a7..fc316d16c80 100644--- a/modules/overviews/app/controllers/overviews/overviews_controller.rb+++ b/modules/overviews/app/controllers/overviews/overviews_controller.rb@@ -34,20 +34,26 @@ module ::Overviewsenddef update_attributes- @section = find_project_custom_field_section-- # TODO: transform to contract/service-based approach with permission checks- @project.update_custom_field_values_of_section(@section, permitted_params.project)-- has_errors = @project.errors.any?-- if has_errors- handle_errors- else+ section = find_project_custom_field_section++ service_call = ::Projects::UpdateService+ .new(+ user: current_user,+ model: @project+ )+ .call(+ permitted_params.project.merge(+ touched_custom_field_section_id: section.id+ )+ )++ if service_call.success?update_sidebar_component+ else+ handle_errors(service_call.result, section)end- respond_to_with_turbo_streams(status: has_errors ? :unprocessable_entity : :ok)+ respond_to_with_turbo_streams(status: service_call.success? ? :ok : :unprocessable_entity)enddef jump_to_project_menu_item@@ -85,11 +91,11 @@ module ::OverviewsProjectCustomFieldSection.find(params[:section_id])end- def handle_errors+ def handle_errors(project_with_errors, section)update_via_turbo_stream(component: ProjectCustomFields::Sections::EditDialogComponent.new(- project: @project,- project_custom_field_section: @section+ project: project_with_errors,+ project_custom_field_section: section))enddiff --git a/spec/features/projects/project_custom_fields/overview_page/dialog_spec.rb b/spec/features/projects/project_custom_fields/overview_page/dialog_spec.rbindex 52b80099a46..2e48e099b69 100644--- a/spec/features/projects/project_custom_fields/overview_page/dialog_spec.rb+++ b/spec/features/projects/project_custom_fields/overview_page/dialog_spec.rb@@ -884,8 +884,6 @@ RSpec.describe 'Edit project custom fields on project overview page', :js dofield.set_value('Foooo')- page.save_screenshot('screenshot.png')-dialog.submitfield.expect_error(I18n.t('activerecord.errors.messages.too_long', count: 3))