diff --git a/app/controllers/administrateurs/types_de_champ_controller.rb b/app/controllers/administrateurs/types_de_champ_controller.rb index a4ea69be7..158427102 100644 --- a/app/controllers/administrateurs/types_de_champ_controller.rb +++ b/app/controllers/administrateurs/types_de_champ_controller.rb @@ -15,7 +15,7 @@ module Administrateurs end def update - type_de_champ = @procedure.draft_revision.find_or_clone_type_de_champ(params[:id]) + type_de_champ = @procedure.draft_revision.find_and_ensure_exclusive_use(params[:id]) if type_de_champ.update(type_de_champ_update_params) reset_procedure diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 6ac2f13fa..72171fa36 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -65,7 +65,7 @@ class ProcedureRevision < ApplicationRecord TypeDeChamp.new.tap { |tdc| tdc.errors.add(:base, e.message) } end - def find_or_clone_type_de_champ(stable_id) + def find_and_ensure_exclusive_use(stable_id) coordinate, tdc = coordinate_and_tdc(stable_id) if tdc.only_present_on_draft? diff --git a/spec/models/concern/tags_substitution_concern_spec.rb b/spec/models/concern/tags_substitution_concern_spec.rb index e5ecb7970..be992abde 100644 --- a/spec/models/concern/tags_substitution_concern_spec.rb +++ b/spec/models/concern/tags_substitution_concern_spec.rb @@ -397,7 +397,7 @@ describe TagsSubstitutionConcern, type: :model do context 'when procedure has revisions' do let(:types_de_champ) { [build(:type_de_champ, libelle: 'mon ancien libellé')] } - let(:draft_type_de_champ) { procedure.draft_revision.find_or_clone_type_de_champ(types_de_champ[0].stable_id) } + let(:draft_type_de_champ) { procedure.draft_revision.find_and_ensure_exclusive_use(types_de_champ[0].stable_id) } before do draft_type_de_champ.update(libelle: 'mon nouveau libellé') diff --git a/spec/models/dossier_rebase_concern_spec.rb b/spec/models/dossier_rebase_concern_spec.rb index ae697e644..640ef84fd 100644 --- a/spec/models/dossier_rebase_concern_spec.rb +++ b/spec/models/dossier_rebase_concern_spec.rb @@ -34,7 +34,7 @@ describe Dossier do context 'with type de champ made optional' do before do - procedure.draft_revision.find_or_clone_type_de_champ(mandatory_type_de_champ.stable_id).update(mandatory: false) + procedure.draft_revision.find_and_ensure_exclusive_use(mandatory_type_de_champ.stable_id).update(mandatory: false) procedure.publish_revision! dossier.reload end @@ -47,7 +47,7 @@ describe Dossier do context 'with type de champ made mandatory' do before do - procedure.draft_revision.find_or_clone_type_de_champ(type_de_champ.stable_id).update(mandatory: true) + procedure.draft_revision.find_and_ensure_exclusive_use(type_de_champ.stable_id).update(mandatory: true) procedure.publish_revision! dossier.reload end @@ -138,7 +138,7 @@ describe Dossier do context 'with type de champ made optional' do before do - procedure.draft_revision.find_or_clone_type_de_champ(mandatory_type_de_champ.stable_id).update(mandatory: false) + procedure.draft_revision.find_and_ensure_exclusive_use(mandatory_type_de_champ.stable_id).update(mandatory: false) procedure.publish_revision! dossier.reload end @@ -176,9 +176,9 @@ describe Dossier do type_champ: TypeDeChamp.type_champs.fetch(:text), libelle: "Un champ text" }) - procedure.draft_revision.find_or_clone_type_de_champ(text_type_de_champ).update(mandatory: false, libelle: "nouveau libelle") - procedure.draft_revision.find_or_clone_type_de_champ(datetime_type_de_champ).update(type_champ: TypeDeChamp.type_champs.fetch(:date)) - procedure.draft_revision.find_or_clone_type_de_champ(repetition_text_type_de_champ).update(libelle: "nouveau libelle dans une repetition") + procedure.draft_revision.find_and_ensure_exclusive_use(text_type_de_champ).update(mandatory: false, libelle: "nouveau libelle") + procedure.draft_revision.find_and_ensure_exclusive_use(datetime_type_de_champ).update(type_champ: TypeDeChamp.type_champs.fetch(:date)) + procedure.draft_revision.find_and_ensure_exclusive_use(repetition_text_type_de_champ).update(libelle: "nouveau libelle dans une repetition") procedure.draft_revision.add_type_de_champ({ type_champ: TypeDeChamp.type_champs.fetch(:checkbox), libelle: "oui ou non", @@ -247,7 +247,7 @@ describe Dossier do dossier.champs.first.update(value: 'v1') stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'l1') - tdc_to_update = procedure.draft_revision.find_or_clone_type_de_champ(stable_id) + tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id) tdc_to_update.update(drop_down_list_value: 'option updated') end @@ -269,7 +269,7 @@ describe Dossier do dossier.champs.first.update(value: 'v1', geo_areas: [create(:geo_area, :cadastre)]) stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'l1') - tdc_to_update = procedure.draft_revision.find_or_clone_type_de_champ(stable_id) + tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id) tdc_to_update.update(cadastres: false) end @@ -301,7 +301,7 @@ describe Dossier do context 'when the first tdc is removed' do before do stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'l1') - tdc_to_remove = procedure.draft_revision.find_or_clone_type_de_champ(stable_id) + tdc_to_remove = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id) procedure.draft_revision.remove_type_de_champ(tdc_to_remove.stable_id) end @@ -320,7 +320,7 @@ describe Dossier do context 'when the first tdc libelle is updated' do before do stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'l1') - tdc_to_update = procedure.draft_revision.find_or_clone_type_de_champ(stable_id) + tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id) tdc_to_update.update(libelle: 'l1 updated') end @@ -345,7 +345,7 @@ describe Dossier do first_champ.update_column('updated_at', Time.zone.parse('01/01/1901')) stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'l1') - tdc_to_update = procedure.draft_revision.find_or_clone_type_de_champ(stable_id) + tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id) tdc_to_update.update(type_champ: :integer_number) end @@ -396,7 +396,7 @@ describe Dossier do context 'when the first child libelle tdc is updated' do before do stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'c1') - tdc_to_update = procedure.draft_revision.find_or_clone_type_de_champ(stable_id) + tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id) tdc_to_update.update(libelle: 'c1 updated') end @@ -406,7 +406,7 @@ describe Dossier do context 'when the first child tdc type is updated' do before do stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'c1') - tdc_to_update = procedure.draft_revision.find_or_clone_type_de_champ(stable_id) + tdc_to_update = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id) tdc_to_update.update(type_champ: :integer_number) end @@ -416,7 +416,7 @@ describe Dossier do context 'when the parents type is changed' do before do stable_id = procedure.draft_revision.types_de_champ.find_by(libelle: 'p1') - parent = procedure.draft_revision.find_or_clone_type_de_champ(stable_id) + parent = procedure.draft_revision.find_and_ensure_exclusive_use(stable_id) parent.update(type_champ: :integer_number) end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 6bcef4f40..db59d6708 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1453,9 +1453,9 @@ describe Dossier do dossier procedure.draft_revision.remove_type_de_champ(text_type_de_champ.stable_id) procedure.draft_revision.add_type_de_champ(type_champ: TypeDeChamp.type_champs.fetch(:text), libelle: 'New text field') - procedure.draft_revision.find_or_clone_type_de_champ(yes_no_type_de_champ.stable_id).update(libelle: 'Updated yes/no') - procedure.draft_revision.find_or_clone_type_de_champ(commune_type_de_champ.stable_id).update(libelle: 'Commune de naissance') - procedure.draft_revision.find_or_clone_type_de_champ(repetition_type_de_champ.stable_id).update(libelle: 'Repetition') + procedure.draft_revision.find_and_ensure_exclusive_use(yes_no_type_de_champ.stable_id).update(libelle: 'Updated yes/no') + procedure.draft_revision.find_and_ensure_exclusive_use(commune_type_de_champ.stable_id).update(libelle: 'Commune de naissance') + procedure.draft_revision.find_and_ensure_exclusive_use(repetition_type_de_champ.stable_id).update(libelle: 'Repetition') procedure.update(published_revision: procedure.draft_revision, draft_revision: procedure.create_new_revision) dossier.reload procedure.reload diff --git a/spec/models/procedure_presentation_and_revisions_spec.rb b/spec/models/procedure_presentation_and_revisions_spec.rb index 482fb0e14..b593941f2 100644 --- a/spec/models/procedure_presentation_and_revisions_spec.rb +++ b/spec/models/procedure_presentation_and_revisions_spec.rb @@ -65,7 +65,7 @@ describe ProcedurePresentation do let!(:changed_tdc) { { type_champ: :number, libelle: 'changed libelle 1' } } before do - type_de_champ = procedure.draft_revision.find_or_clone_type_de_champ(previous_tdc.id) + type_de_champ = procedure.draft_revision.find_and_ensure_exclusive_use(previous_tdc.id) type_de_champ.update(changed_tdc) procedure.publish_revision! diff --git a/spec/models/procedure_revision_spec.rb b/spec/models/procedure_revision_spec.rb index 62f1c2416..34b585121 100644 --- a/spec/models/procedure_revision_spec.rb +++ b/spec/models/procedure_revision_spec.rb @@ -284,7 +284,7 @@ describe ProcedureRevision do before do procedure.publish! procedure.reload - draft.find_or_clone_type_de_champ(last_type_de_champ.stable_id).update(libelle: 'new libelle') + draft.find_and_ensure_exclusive_use(last_type_de_champ.stable_id).update(libelle: 'new libelle') procedure.reload draft.reload end @@ -330,7 +330,7 @@ describe ProcedureRevision do let(:procedure) { create(:procedure, :with_type_de_champ) } before do - updated_tdc = new_draft.find_or_clone_type_de_champ(first_tdc.stable_id) + updated_tdc = new_draft.find_and_ensure_exclusive_use(first_tdc.stable_id) updated_tdc.update(libelle: 'modifier le libelle', description: 'une description', mandatory: !updated_tdc.mandatory) end @@ -431,7 +431,7 @@ describe ProcedureRevision do before do child = new_draft.children_of(new_draft.types_de_champ_public.last).first - new_draft.find_or_clone_type_de_champ(child.stable_id).update(type_champ: :drop_down_list, drop_down_options: ['one', 'two']) + new_draft.find_and_ensure_exclusive_use(child.stable_id).update(type_champ: :drop_down_list, drop_down_options: ['one', 'two']) end it do @@ -465,7 +465,7 @@ describe ProcedureRevision do before do child = new_draft.types_de_champ_public.last.types_de_champ.first - new_draft.find_or_clone_type_de_champ(child.stable_id).update(type_champ: :carte, options: { cadastres: true, znieff: true }) + new_draft.find_and_ensure_exclusive_use(child.stable_id).update(type_champ: :carte, options: { cadastres: true, znieff: true }) end it do