From 54926db89b592b6ee50b07e00fc9270b90e98bcc Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 22 Aug 2024 17:35:40 +0200 Subject: [PATCH] refactor(champ): more concistent champ_for_update usage --- app/controllers/champs/champ_controller.rb | 6 +++++- app/models/concerns/dossier_champs_concern.rb | 2 +- spec/models/concerns/dossier_searchable_concern_spec.rb | 6 ++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/controllers/champs/champ_controller.rb b/app/controllers/champs/champ_controller.rb index 8d82161bb..5af93d346 100644 --- a/app/controllers/champs/champ_controller.rb +++ b/app/controllers/champs/champ_controller.rb @@ -9,7 +9,11 @@ class Champs::ChampController < ApplicationController def find_champ dossier = policy_scope(Dossier).includes(:champs, revision: [:types_de_champ]).find(params[:dossier_id]) type_de_champ = dossier.find_type_de_champ_by_stable_id(params[:stable_id]) - dossier.champ_for_update(type_de_champ, params_row_id, updated_by: current_user.email) + if type_de_champ.repetition? + dossier.project_champ(type_de_champ, nil) + else + dossier.champ_for_update(type_de_champ, params_row_id, updated_by: current_user.email) + end end def params_row_id diff --git a/app/models/concerns/dossier_champs_concern.rb b/app/models/concerns/dossier_champs_concern.rb index 485343e5c..53a6bb96e 100644 --- a/app/models/concerns/dossier_champs_concern.rb +++ b/app/models/concerns/dossier_champs_concern.rb @@ -57,7 +57,7 @@ module DossierChampsConcern .types_de_champ .filter { _1.stable_id.in?(stable_ids) } .filter { !revision.child?(_1) } - .map { champ_for_update(_1, nil, updated_by: nil) } + .map { _1.repetition? ? project_champ(_1, nil) : champ_for_update(_1, nil, updated_by: nil) } end def champ_for_update(type_de_champ, row_id, updated_by:) diff --git a/spec/models/concerns/dossier_searchable_concern_spec.rb b/spec/models/concerns/dossier_searchable_concern_spec.rb index 560184c6c..d1b0c1114 100644 --- a/spec/models/concerns/dossier_searchable_concern_spec.rb +++ b/spec/models/concerns/dossier_searchable_concern_spec.rb @@ -38,10 +38,8 @@ describe DossierSearchableConcern do end it "update columns en construction" do - dossier.update( - champs_public_attributes: [{ id: champ_public.id, value: 'nouvelle valeur publique' }], - champs_private_attributes: [{ id: champ_private.id, value: 'nouvelle valeur privee' }] - ) + dossier.update_champs_attributes({ champ_public.public_id => { value: 'nouvelle valeur publique' } }, :public, updated_by: 'test') + dossier.update_champs_attributes({ champ_private.public_id => { value: 'nouvelle valeur privee' } }, :private, updated_by: 'test') assert_enqueued_jobs(1, only: DossierIndexSearchTermsJob) do dossier.passer_en_construction