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 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.rb
index 84bfde71333..4b64a68872e 100644
--- a/app/contracts/projects/base_contract.rb
+++ b/app/contracts/projects/base_contract.rb
@@ -50,6 +50,10 @@ module Projects
validate_templated_set_by_admin
end
+ 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_manage
def assignable_parents
diff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rb
index 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::ActsAsCustomizablePatches
extend ActiveSupport::Concern
+ attr_accessor :touched_custom_field_section_id
+
included do
def active_custom_field_ids_of_project
@active_custom_field_ids_of_project ||= ProjectCustomFieldProjectMapping
@@ -85,18 +87,11 @@ module Projects::ActsAsCustomizablePatches
end
def 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_id
else
true # validate all custom values if no specific section was marked as touched via `update_custom_field_values_of_section`
end
end
-
- 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)
- end
end
end
diff --git a/modules/overviews/app/controllers/overviews/overviews_controller.rb b/modules/overviews/app/controllers/overviews/overviews_controller.rb
index 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 ::Overviews
end
def 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)
end
def jump_to_project_menu_item
@@ -85,11 +91,11 @@ module ::Overviews
ProjectCustomFieldSection.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
)
)
end
diff --git a/spec/features/projects/project_custom_fields/overview_page/dialog_spec.rb b/spec/features/projects/project_custom_fields/overview_page/dialog_spec.rb
index 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 do
field.set_value('Foooo')
- page.save_screenshot('screenshot.png')
-
dialog.submit
field.expect_error(I18n.t('activerecord.errors.messages.too_long', count: 3))