diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 6123e571b..43e4bccb3 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -714,7 +714,7 @@ class Procedure < ApplicationRecord def create_new_revision draft_revision - .deep_clone(include: [:revision_types_de_champ_public, :revision_types_de_champ_private]) + .deep_clone(include: [:revision_types_de_champ]) .tap(&:save!) end diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 7e7b72928..ab9fb8745 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -18,8 +18,10 @@ class ProcedureRevision < ApplicationRecord has_many :dossiers, inverse_of: :revision, foreign_key: :revision_id + has_many :revision_types_de_champ, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision has_many :revision_types_de_champ_public, -> { root.public_only.ordered }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision has_many :revision_types_de_champ_private, -> { root.private_only.ordered }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision + has_many :types_de_champ, through: :revision_types_de_champ, source: :type_de_champ has_many :types_de_champ_public, through: :revision_types_de_champ_public, source: :type_de_champ has_many :types_de_champ_private, through: :revision_types_de_champ_private, source: :type_de_champ @@ -46,10 +48,8 @@ class ProcedureRevision < ApplicationRecord .tap do |types_de_champ| params[:order_place] = types_de_champ.present? ? types_de_champ.last.order_place + 1 : 0 end.create(params).migrate_parent! - elsif params[:private] - types_de_champ_private.create(params) else - types_de_champ_public.create(params) + types_de_champ.create(params) end end @@ -76,13 +76,11 @@ class ProcedureRevision < ApplicationRecord type_de_champ.update!(order_place: position) type_de_champ.revision_type_de_champ&.update!(position: position) end - elsif type_de_champ.private? - move_type_de_champ_hash(types_de_champ_private.to_a, type_de_champ, position).each do |(id, position)| - revision_types_de_champ_private.find_by!(type_de_champ_id: id).update!(position: position) - end else - move_type_de_champ_hash(types_de_champ_public.to_a, type_de_champ, position).each do |(id, position)| - revision_types_de_champ_public.find_by!(type_de_champ_id: id).update!(position: position) + liste = type_de_champ.private? ? types_de_champ_private : types_de_champ_public + + move_type_de_champ_hash(liste.to_a, type_de_champ, position).each do |(id, position)| + revision_types_de_champ.find_by!(type_de_champ_id: id).update!(position: position) end end end @@ -94,10 +92,8 @@ class ProcedureRevision < ApplicationRecord type_de_champ.destroy elsif type_de_champ.parent.present? find_or_clone_type_de_champ(id).destroy - elsif type_de_champ.private? - types_de_champ_private.delete(type_de_champ) else - types_de_champ_public.delete(type_de_champ) + types_de_champ.delete(type_de_champ) end end @@ -110,8 +106,7 @@ class ProcedureRevision < ApplicationRecord end def different_from?(revision) - types_de_champ_public != revision.types_de_champ_public || - types_de_champ_private != revision.types_de_champ_private || + types_de_champ != revision.types_de_champ || attestation_template != revision.attestation_template end @@ -390,13 +385,12 @@ class ProcedureRevision < ApplicationRecord end def find_type_de_champ_by_id(id) - types_de_champ_public.find_by(stable_id: id) || - types_de_champ_private.find_by(stable_id: id) || + types_de_champ.find_by(stable_id: id) || types_de_champ_in_repetition.find_by!(stable_id: id) end def types_de_champ_in_repetition - parent_ids = types_de_champ_public.repetition.ids + types_de_champ_private.repetition.ids + parent_ids = types_de_champ.repetition.ids TypeDeChamp.where(parent_id: parent_ids) end diff --git a/spec/models/procedure_presentation_and_revisions_spec.rb b/spec/models/procedure_presentation_and_revisions_spec.rb index 473725711..52fa41b5d 100644 --- a/spec/models/procedure_presentation_and_revisions_spec.rb +++ b/spec/models/procedure_presentation_and_revisions_spec.rb @@ -5,7 +5,7 @@ describe ProcedurePresentation do context 'for a draft procedure' do let(:procedure) { create(:procedure) } - context 'when there are one tdc on a published revision' do + context 'when there are one tdc on a draft revision' do let!(:tdc) { { type_champ: :number, libelle: 'libelle 1' } } before { procedure.draft_revision.add_type_de_champ(tdc) } @@ -37,26 +37,26 @@ describe ProcedurePresentation do end context 'add one tdc above the first one' do - let(:tdc2) { { type_champ: :number, libelle: 'libelle 2' } } + let(:tdc0) { { type_champ: :number, libelle: 'libelle 0' } } before do - created_tdc2 = procedure.draft_revision.add_type_de_champ(tdc2) - procedure.draft_revision.move_type_de_champ(created_tdc2.stable_id, 0) + created_tdc0 = procedure.draft_revision.add_type_de_champ(tdc0) + procedure.draft_revision.move_type_de_champ(created_tdc0.stable_id, 0) procedure.publish_revision! end - it { is_expected.to match(['libelle 2', 'libelle 1']) } + it { is_expected.to match(['libelle 0', 'libelle 1']) } context 'and finally, when this tdc is removed' do - let!(:previous_tdc2) { procedure.published_revision.types_de_champ_public.find_by(libelle: 'libelle 2') } + let!(:previous_tdc0) { procedure.published_revision.types_de_champ_public.find_by(libelle: 'libelle 0') } before do - procedure.draft_revision.remove_type_de_champ(previous_tdc2.stable_id) + procedure.draft_revision.remove_type_de_champ(previous_tdc0.stable_id) procedure.publish_revision! end - it { is_expected.to match(['libelle 1', 'libelle 2']) } + it { is_expected.to match(['libelle 1', 'libelle 0']) } end end @@ -73,18 +73,6 @@ describe ProcedurePresentation do it { is_expected.to match(['changed libelle 1']) } end - - context 'when there is another published which removes a previous tdc' do - let!(:previous_tdc) { procedure.published_revision.types_de_champ_public.first } - - before do - type_de_champ = procedure.draft_revision.remove_type_de_champ(previous_tdc.id) - - procedure.publish_revision! - end - - it { is_expected.to match(['libelle 1']) } - end end end end diff --git a/spec/models/procedure_revision_spec.rb b/spec/models/procedure_revision_spec.rb index 1ae640e55..5eee14b40 100644 --- a/spec/models/procedure_revision_spec.rb +++ b/spec/models/procedure_revision_spec.rb @@ -164,6 +164,7 @@ describe ProcedureRevision do libelle: "Un champ text" }) revision.reload + new_revision.reload expect(new_revision.types_de_champ_public.size).to eq(3) expect(new_revision.types_de_champ_public.last).to eq(new_type_de_champ) expect(new_revision.revision_types_de_champ_public.last.position).to eq(2)