diff --git a/app/controllers/administrateurs/types_de_champ_controller.rb b/app/controllers/administrateurs/types_de_champ_controller.rb index 16609acd6..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 @@ -26,7 +26,7 @@ module Administrateurs end def move - @procedure.draft_revision.move_type_de_champ(params[:id], (params[:position] || params[:order_place]).to_i) + @procedure.draft_revision.move_type_de_champ(params[:id], params[:position].to_i) head :no_content end diff --git a/app/models/champ.rb b/app/models/champ.rb index 9461b18aa..14a0992f7 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -34,7 +34,6 @@ class Champ < ApplicationRecord delegate :libelle, :type_champ, :procedure, - :order_place, :mandatory?, :description, :drop_down_list_options, diff --git a/app/models/procedure.rb b/app/models/procedure.rb index ae0d65f4d..66bd14b1a 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -165,7 +165,7 @@ class Procedure < ApplicationRecord if brouillon? draft_types_de_champ else - TypeDeChamp.root + TypeDeChamp .public_only .fillable .joins(:revisions) @@ -181,7 +181,7 @@ class Procedure < ApplicationRecord if brouillon? draft_types_de_champ_private else - TypeDeChamp.root + TypeDeChamp .private_only .fillable .joins(:revisions) @@ -460,12 +460,7 @@ class Procedure < ApplicationRecord populate_champ_stable_ids include_list = { draft_revision: { - revision_types_de_champ_public: { - type_de_champ: :types_de_champ - }, - revision_types_de_champ_private: { - type_de_champ: :types_de_champ - }, + revision_types_de_champ: [:type_de_champ], attestation_template: [], dossier_submitted_message: [] } @@ -515,7 +510,8 @@ class Procedure < ApplicationRecord end procedure.save - TypeDeChamp.where(parent: procedure.draft_revision.types_de_champ.repetition).find_each(&:migrate_parent!) + + move_new_children_to_new_parent_coordinate(procedure.draft_revision) if is_different_admin || from_library procedure.draft_types_de_champ.each { |tdc| tdc.options&.delete(:old_pj) } @@ -736,21 +732,7 @@ class Procedure < ApplicationRecord .deep_clone(include: [:revision_types_de_champ]) .tap(&:save!) - children = new_draft.revision_types_de_champ.where.not(parent_id: nil) - children.each do |child| - old_parent = draft_revision.revision_types_de_champ.find(child.parent_id) - new_parent = new_draft.revision_types_de_champ.find_by(type_de_champ_id: old_parent.type_de_champ_id) - child.update!(parent_id: new_parent.id) - end - - new_draft.revision_types_de_champ.reload - - # Some revisions do not have links to children types de champ - new_draft - .types_de_champ - .filter(&:repetition?) - .flat_map(&:types_de_champ) - .each(&:migrate_parent!) + move_new_children_to_new_parent_coordinate(new_draft) new_draft end @@ -795,6 +777,16 @@ class Procedure < ApplicationRecord private + def move_new_children_to_new_parent_coordinate(new_draft) + children = new_draft.revision_types_de_champ.where.not(parent_id: nil) + children.each do |child| + old_parent = draft_revision.revision_types_de_champ.find(child.parent_id) + new_parent = new_draft.revision_types_de_champ.find_by(type_de_champ_id: old_parent.type_de_champ_id) + child.update!(parent_id: new_parent.id) + end + new_draft.revision_types_de_champ.reload + end + def validate_for_publication? validation_context == :publication || publiee? end diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 77b8b7fd4..72171fa36 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -39,22 +39,15 @@ class ProcedureRevision < ApplicationRecord end def add_type_de_champ(params) - parent_stable_id = params[:parent_id] + parent_stable_id = params.delete(:parent_id) coordinate = {} if parent_stable_id.present? - # Ensure that if this is a child, it's parent is cloned to the new revision - clone_parent_to_draft_revision(parent_stable_id) - parent_coordinate, parent = coordinate_and_tdc(parent_stable_id) coordinate[:parent_id] = parent_coordinate.id coordinate[:position] = children_of(parent).count - - # old system - params[:order_place] = coordinate[:position] - params[:parent_id] = parent.id elsif params[:private] coordinate[:position] = revision_types_de_champ_private.count else @@ -72,25 +65,17 @@ class ProcedureRevision < ApplicationRecord TypeDeChamp.new.tap { |tdc| tdc.errors.add(:base, e.message) } end - # Only called by by controller update - def find_or_clone_type_de_champ(stable_id) - # Ensure that if this is a child, it's parent is cloned to the new revision - clone_parent_to_draft_revision(stable_id) - + def find_and_ensure_exclusive_use(stable_id) coordinate, tdc = coordinate_and_tdc(stable_id) if tdc.only_present_on_draft? tdc else - replace_tdc_and_children_by_clones(coordinate) + replace_type_de_champ_by_clone(coordinate) end end def move_type_de_champ(stable_id, position) - # Ensure that if this is a child, it's parent is cloned to the new revision - # Needed because the order could be based on the ancient system - clone_parent_to_draft_revision(stable_id) - coordinate, _ = coordinate_and_tdc(stable_id) siblings = coordinate.siblings.to_a @@ -101,17 +86,13 @@ class ProcedureRevision < ApplicationRecord end def remove_type_de_champ(stable_id) - # Ensure that if this is a child, it's parent is cloned to the new revision - # Needed because the order could be based on the ancient system - clone_parent_to_draft_revision(stable_id) - coordinate, tdc = coordinate_and_tdc(stable_id) + children = children_of(tdc).to_a coordinate.destroy - if tdc.revision_types_de_champ.empty? - tdc.destroy - end + children.each(&:destroy_if_orphan) + tdc.destroy_if_orphan reorder(coordinate.siblings) end @@ -166,6 +147,12 @@ class ProcedureRevision < ApplicationRecord .order("procedure_revision_types_de_champ.position") end + def remove_children_of(tdc) + children_of(tdc).each do |child| + remove_type_de_champ(child.stable_id) + end + end + def types_de_champ_public_as_json types_de_champ = types_de_champ_public.includes(piece_justificative_template_attachment: :blob) tdcs_as_json = types_de_champ.map(&:as_json_for_editor) @@ -216,13 +203,8 @@ class ProcedureRevision < ApplicationRecord def reorder(siblings) siblings.to_a.compact.each.with_index do |sibling, position| - sibling.update(position: position) - - # FIXME: to remove when order_place is no longer used - if sibling.parent_id.present? - sibling.type_de_champ.update!(order_place: position) - end - end + sibling.update(position: position) + end end def compare_attestation_template(from_at, to_at) @@ -451,29 +433,11 @@ class ProcedureRevision < ApplicationRecord changes end - def replace_tdc_and_children_by_clones(coordinate) - cloned_type_de_champ = coordinate.type_de_champ.deep_clone(include: [:types_de_champ]) do |original, kopy| + def replace_type_de_champ_by_clone(coordinate) + cloned_type_de_champ = coordinate.type_de_champ.deep_clone do |original, kopy| PiecesJustificativesService.clone_attachments(original, kopy) end - cloned_child_types_de_champ = cloned_type_de_champ.types_de_champ coordinate.update!(type_de_champ: cloned_type_de_champ) - - # sync old and new system - children_coordinates = revision_types_de_champ.where(parent: coordinate) - - children_coordinates.find_each do |child_coordinate| - cloned_child_type_de_champ = cloned_child_types_de_champ.find { |tdc| tdc.stable_id == child_coordinate.type_de_champ.stable_id } - child_coordinate.update!(type_de_champ: cloned_child_type_de_champ) - end - cloned_type_de_champ end - - def clone_parent_to_draft_revision(stable_id) - coordinate, tdc = coordinate_and_tdc(stable_id) - - if coordinate.child? && !tdc.only_present_on_draft? - replace_tdc_and_children_by_clones(coordinate.parent) - end - end end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 9e2674720..86bc5ac47 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -7,16 +7,14 @@ # libelle :string # mandatory :boolean default(FALSE) # options :jsonb -# order_place :integer # private :boolean default(FALSE), not null # type_champ :string # created_at :datetime # updated_at :datetime -# parent_id :bigint # stable_id :bigint # class TypeDeChamp < ApplicationRecord - self.ignored_columns = [:migrated_parent, :revision_id] + self.ignored_columns = [:migrated_parent, :revision_id, :parent_id, :order_place] enum type_champs: { text: 'text', @@ -56,9 +54,6 @@ class TypeDeChamp < ApplicationRecord mesri: 'mesri' } - belongs_to :parent, class_name: 'TypeDeChamp', optional: true - has_many :types_de_champ, -> { ordered }, foreign_key: :parent_id, class_name: 'TypeDeChamp', inverse_of: :parent, dependent: :destroy - store_accessor :options, :cadastres, :old_pj, :drop_down_options, :skip_pj_validation, :skip_content_type_pj_validation, :drop_down_secondary_libelle, :drop_down_secondary_description, :drop_down_other has_many :revision_types_de_champ, -> { revision_ordered }, class_name: 'ProcedureRevisionTypeDeChamp', dependent: :destroy, inverse_of: :type_de_champ has_one :revision_type_de_champ, -> { revision_ordered }, class_name: 'ProcedureRevisionTypeDeChamp', inverse_of: false @@ -87,8 +82,6 @@ class TypeDeChamp < ApplicationRecord scope :public_only, -> { where(private: false) } scope :private_only, -> { where(private: true) } - scope :ordered, -> { order(order_place: :asc) } - scope :root, -> { where(parent_id: nil) } scope :repetition, -> { where(type_champ: type_champs.fetch(:repetition)) } scope :not_repetition, -> { where.not(type_champ: type_champs.fetch(:repetition)) } scope :fillable, -> { where.not(type_champ: [type_champs.fetch(:header_section), type_champs.fetch(:explication)]) } @@ -112,8 +105,6 @@ class TypeDeChamp < ApplicationRecord has_one_attached :piece_justificative_template - accepts_nested_attributes_for :types_de_champ, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true - validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :type_champ, presence: true, allow_blank: false, allow_nil: false @@ -344,8 +335,6 @@ class TypeDeChamp < ApplicationRecord except: [ :created_at, :options, - :order_place, - :parent_id, :private, :stable_id, :type, @@ -371,16 +360,10 @@ class TypeDeChamp < ApplicationRecord end end - def migrate_parent! - if parent_id.present? && revision_types_de_champ.empty? - parent.revision_types_de_champ.each do |revision_type_de_champ| - ProcedureRevisionTypeDeChamp.create(parent: revision_type_de_champ, - type_de_champ: self, - revision_id: revision_type_de_champ.revision_id, - position: order_place) - end + def destroy_if_orphan + if revision_types_de_champ.empty? + destroy end - self end private @@ -413,8 +396,7 @@ class TypeDeChamp < ApplicationRecord if !repetition? && procedure.present? procedure .draft_revision # action occurs only on draft - .children_of(self) - .destroy_all + .remove_children_of(self) end end end diff --git a/app/models/types_de_champ/repetition_type_de_champ.rb b/app/models/types_de_champ/repetition_type_de_champ.rb index 15e448256..9af53de27 100644 --- a/app/models/types_de_champ/repetition_type_de_champ.rb +++ b/app/models/types_de_champ/repetition_type_de_champ.rb @@ -8,8 +8,8 @@ class TypesDeChamp::RepetitionTypeDeChamp < TypesDeChamp::TypeDeChampBase def estimated_fill_duration(revision) estimated_rows_in_repetition = 2.5 - estimated_row_duration = @type_de_champ - .types_de_champ + estimated_row_duration = revision + .children_of(@type_de_champ) .map { |child_tdc| child_tdc.estimated_fill_duration(revision) } .sum estimated_row_duration * estimated_rows_in_repetition diff --git a/app/serializers/type_de_champ_serializer.rb b/app/serializers/type_de_champ_serializer.rb index b8ef6ecb7..bf07af980 100644 --- a/app/serializers/type_de_champ_serializer.rb +++ b/app/serializers/type_de_champ_serializer.rb @@ -8,4 +8,8 @@ class TypeDeChampSerializer < ActiveModel::Serializer def id object.stable_id || object.id end + + def order_place + -1 + end end diff --git a/app/views/fields/types_de_champ_collection_field/_show.html.haml b/app/views/fields/types_de_champ_collection_field/_show.html.haml index 9e709a09a..7fd5a168f 100644 --- a/app/views/fields/types_de_champ_collection_field/_show.html.haml +++ b/app/views/fields/types_de_champ_collection_field/_show.html.haml @@ -8,7 +8,7 @@ %td.cell-label Type de champ %td.cell-label Modifier le modèle %tbody - - field.data.order(:order_place).each do |type_de_champ| + - field.data.each do |type_de_champ| = render partial: 'fields/types_de_champ_collection_field/type_champ_line', locals: { type_de_champ: type_de_champ } diff --git a/spec/controllers/api/v1/procedures_controller_spec.rb b/spec/controllers/api/v1/procedures_controller_spec.rb index bcc6b4a3b..9410920ed 100644 --- a/spec/controllers/api/v1/procedures_controller_spec.rb +++ b/spec/controllers/api/v1/procedures_controller_spec.rb @@ -48,7 +48,6 @@ describe API::V1::ProceduresController, type: :controller do it { expect(subject[:id]).to eq(champ.id) } it { expect(subject[:libelle]).to eq(champ.libelle) } it { expect(subject[:type_champ]).to eq(champ.type_champ) } - it { expect(subject[:order_place]).to eq(champ.order_place) } it { expect(subject[:description]).to eq(champ.description) } end diff --git a/spec/controllers/api/v2/graphql_controller_spec.rb b/spec/controllers/api/v2/graphql_controller_spec.rb index 51589359b..8ba4e6bca 100644 --- a/spec/controllers/api/v2/graphql_controller_spec.rb +++ b/spec/controllers/api/v2/graphql_controller_spec.rb @@ -149,7 +149,7 @@ describe API::V2::GraphqlController do type: tdc.type_champ, description: tdc.description, required: tdc.mandatory?, - champDescriptors: tdc.repetition? ? tdc.reload.types_de_champ.map { |tdc| { id: tdc.to_typed_id, type: tdc.type_champ } } : nil, + champDescriptors: tdc.repetition? ? procedure.active_revision.children_of(tdc.reload).map { |tdc| { id: tdc.to_typed_id, type: tdc.type_champ } } : nil, options: tdc.drop_down_list? ? tdc.drop_down_list_options.reject(&:empty?) : nil } end, diff --git a/spec/factories/champ.rb b/spec/factories/champ.rb index 77310c872..6724c9f91 100644 --- a/spec/factories/champ.rb +++ b/spec/factories/champ.rb @@ -222,31 +222,19 @@ FactoryBot.define do type_de_champ_text = types_de_champ.find { |tdc| tdc.libelle == 'Nom' } if !type_de_champ_text type_de_champ_text = build(:type_de_champ_text, + procedure: champ_repetition.type_de_champ.procedure, position: 0, - parent: champ_repetition.type_de_champ, - libelle: 'Nom') - revision.revision_types_de_champ << build(:procedure_revision_type_de_champ, - revision: revision, - type_de_champ: type_de_champ_text, parent: parent, - position: 0) - - champ_repetition.type_de_champ.types_de_champ << type_de_champ_text + libelle: 'Nom') end type_de_champ_number = types_de_champ.find { |tdc| tdc.libelle == 'Age' } if !type_de_champ_number type_de_champ_number = build(:type_de_champ_number, + procedure: champ_repetition.type_de_champ.procedure, position: 1, - parent: champ_repetition.type_de_champ, - libelle: 'Age') - revision.revision_types_de_champ << build(:procedure_revision_type_de_champ, - revision: revision, - type_de_champ: type_de_champ_number, parent: parent, - position: 1) - - champ_repetition.type_de_champ.types_de_champ << type_de_champ_number + libelle: 'Age') end evaluator.rows.times do |row| diff --git a/spec/factories/type_de_champ.rb b/spec/factories/type_de_champ.rb index 57a4c053d..cc0e18cac 100644 --- a/spec/factories/type_de_champ.rb +++ b/spec/factories/type_de_champ.rb @@ -3,7 +3,6 @@ FactoryBot.define do sequence(:libelle) { |n| "Libelle du champ #{n}" } sequence(:description) { |n| "description du champ #{n}" } type_champ { TypeDeChamp.type_champs.fetch(:text) } - order_place { 1 } mandatory { false } add_attribute(:private) { false } @@ -14,22 +13,16 @@ FactoryBot.define do end after(:build) do |type_de_champ, evaluator| - if evaluator.procedure - revision = evaluator.procedure.active_revision + revision = evaluator.procedure&.active_revision || build(:procedure_revision) + evaluator.procedure&.save - evaluator.procedure.save + revision.revision_types_de_champ << build(:procedure_revision_type_de_champ, + position: evaluator.position || 0, + revision: revision, + type_de_champ: type_de_champ, + parent: evaluator.parent) - create(:procedure_revision_type_de_champ, - position: evaluator.position || 0, - revision: revision, - type_de_champ: type_de_champ) - - elsif evaluator.parent - type_de_champ.order_place = evaluator.position || evaluator.parent.types_de_champ.size - evaluator.parent.types_de_champ << type_de_champ - else - type_de_champ.order_place = evaluator.position - end + revision.save end trait :private do @@ -177,36 +170,28 @@ FactoryBot.define do after(:build) do |type_de_champ_repetition, evaluator| evaluator.procedure&.save! - evaluator.types_de_champ.each do |type_de_champ| - revision = evaluator.procedure&.active_revision || build(:procedure_revision) - parent = revision.revision_types_de_champ.find { |rtdc| rtdc.type_de_champ == type_de_champ_repetition } - types_de_champ = revision.revision_types_de_champ.filter { |rtdc| rtdc.parent == parent } - position = types_de_champ.size + revision = evaluator.procedure&.active_revision || build(:procedure_revision) + parent = revision.revision_types_de_champ.find { |rtdc| rtdc.type_de_champ == type_de_champ_repetition } + types_de_champ = revision.revision_types_de_champ.filter { |rtdc| rtdc.parent == parent } + position = types_de_champ.size + evaluator.types_de_champ.each.with_index(position) do |type_de_champ, position| revision.revision_types_de_champ << build(:procedure_revision_type_de_champ, revision: revision, type_de_champ: type_de_champ, parent: parent, position: position) - - # old system - type_de_champ.order_place = position - type_de_champ_repetition.types_de_champ << type_de_champ end + + revision.save end trait :with_types_de_champ do after(:build) do |type_de_champ_repetition, evaluator| - type_de_champ = build(:type_de_champ, libelle: 'sub type de champ', parent: type_de_champ_repetition) revision = evaluator.procedure.active_revision parent = revision.revision_types_de_champ.find { |rtdc| rtdc.type_de_champ == type_de_champ_repetition } - evaluator.procedure.save! - revision.revision_types_de_champ << build(:procedure_revision_type_de_champ, - revision: revision, - type_de_champ: type_de_champ, - parent: parent, - position: 0) + build(:type_de_champ, procedure: evaluator.procedure, libelle: 'sub type de champ', parent: parent, position: 0) end end end 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..54e0a38e4 100644 --- a/spec/models/procedure_revision_spec.rb +++ b/spec/models/procedure_revision_spec.rb @@ -174,14 +174,12 @@ describe ProcedureRevision do it 'reorders' do children = draft.children_of(type_de_champ_repetition) expect(children.pluck(:position)).to eq([0, 1, 2]) - expect(children.pluck(:order_place)).to eq([0, 1, 2]) draft.remove_type_de_champ(children[1].stable_id) children.reload expect(children.pluck(:position)).to eq([0, 1]) - expect(children.pluck(:order_place)).to eq([0, 1]) end end end @@ -193,7 +191,6 @@ describe ProcedureRevision do it 'can remove its children' do draft.remove_type_de_champ(child.stable_id) - expect(type_de_champ_repetition.types_de_champ).to be_empty expect { child.reload }.to raise_error ActiveRecord::RecordNotFound expect(draft.types_de_champ_public.size).to eq(1) end @@ -284,7 +281,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 +327,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 +428,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 @@ -444,7 +441,7 @@ describe ProcedureRevision do private: false, from: "text", to: "drop_down_list", - stable_id: new_draft.types_de_champ_public.last.types_de_champ.first.stable_id + stable_id: new_draft.children_of(new_draft.types_de_champ_public.last).first.stable_id }, { model: :type_de_champ, @@ -454,7 +451,7 @@ describe ProcedureRevision do private: false, from: [], to: ["one", "two"], - stable_id: new_draft.types_de_champ_public.last.types_de_champ.first.stable_id + stable_id: new_draft.children_of(new_draft.types_de_champ_public.last).first.stable_id } ]) end @@ -464,8 +461,8 @@ describe ProcedureRevision do let(:procedure) { create(:procedure, :with_repetition) } 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 }) + child = new_draft.children_of(new_draft.types_de_champ_public.last).first + new_draft.find_and_ensure_exclusive_use(child.stable_id).update(type_champ: :carte, options: { cadastres: true, znieff: true }) end it do @@ -478,7 +475,7 @@ describe ProcedureRevision do private: false, from: "text", to: "carte", - stable_id: new_draft.types_de_champ_public.last.types_de_champ.first.stable_id + stable_id: new_draft.children_of(new_draft.types_de_champ_public.last).first.stable_id }, { model: :type_de_champ, @@ -488,7 +485,7 @@ describe ProcedureRevision do private: false, from: [], to: [:cadastres, :znieff], - stable_id: new_draft.types_de_champ_public.last.types_de_champ.first.stable_id + stable_id: new_draft.children_of(new_draft.types_de_champ_public.last).first.stable_id } ]) end @@ -578,13 +575,13 @@ describe ProcedureRevision do end context 'when there are repetitions' do - let(:types_de_champ) do - [ - build(:type_de_champ_repetition, position: 1, mandatory: true, types_de_champ: [ - build(:type_de_champ_text, position: 1, mandatory: true), - build(:type_de_champ_piece_justificative, position: 2, mandatory: true) - ]) - ] + let(:procedure) do + procedure = create(:procedure, types_de_champ: []) + create(:type_de_champ_repetition, position: 1, mandatory: true, procedure: procedure, types_de_champ: [ + build(:type_de_champ_text, position: 1, mandatory: true), + build(:type_de_champ_piece_justificative, position: 2, mandatory: true) + ]) + procedure end it 'estimates that between 2 and 3 rows will be filled for each repetition' do diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 4224f8803..c951883ec 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -486,22 +486,24 @@ describe Procedure do expect(subject.draft_types_de_champ_private.size).to eq(procedure.draft_types_de_champ_private.size) procedure.draft_types_de_champ.zip(subject.draft_types_de_champ).each do |ptc, stc| - expect(stc).to have_same_attributes_as(ptc, except: ["revision_id"]) + expect(stc).to have_same_attributes_as(ptc) expect(stc.revision).to eq(subject.draft_revision) end - TypeDeChamp.where(parent: procedure.draft_types_de_champ.repetition).zip(TypeDeChamp.where(parent: subject.draft_types_de_champ.repetition)).each do |ptc, stc| - expect(stc).to have_same_attributes_as(ptc, except: ["revision_id", "parent_id", "migrated_parent"]) + public_repetition = procedure.draft_types_de_champ.repetition.first + procedure.draft_revision.children_of(public_repetition).zip(subject.draft_revision.children_of(public_repetition)).each do |ptc, stc| + expect(stc).to have_same_attributes_as(ptc) expect(stc.revision).to eq(subject.draft_revision) end procedure.draft_types_de_champ_private.zip(subject.draft_types_de_champ_private).each do |ptc, stc| - expect(stc).to have_same_attributes_as(ptc, except: ["revision_id"]) + expect(stc).to have_same_attributes_as(ptc) expect(stc.revision).to eq(subject.draft_revision) end - TypeDeChamp.where(parent: procedure.draft_types_de_champ_private.repetition).zip(TypeDeChamp.where(parent: subject.draft_types_de_champ_private.repetition)).each do |ptc, stc| - expect(stc).to have_same_attributes_as(ptc, except: ["revision_id", "parent_id", "migrated_parent"]) + private_repetition = procedure.draft_types_de_champ_private.repetition.first + procedure.draft_revision.children_of(private_repetition).zip(subject.draft_revision.children_of(private_repetition)).each do |ptc, stc| + expect(stc).to have_same_attributes_as(ptc) expect(stc.revision).to eq(subject.draft_revision) end diff --git a/spec/models/type_de_champ_shared_example.rb b/spec/models/type_de_champ_shared_example.rb index 3275cc1b6..ea83a4910 100644 --- a/spec/models/type_de_champ_shared_example.rb +++ b/spec/models/type_de_champ_shared_example.rb @@ -27,12 +27,6 @@ shared_examples 'type_de_champ_spec' do end end - context 'order_place' do - # it { is_expected.not_to allow_value(nil).for(:order_place) } - # it { is_expected.not_to allow_value('').for(:order_place) } - it { is_expected.to allow_value(1).for(:order_place) } - end - context 'description' do it { is_expected.to allow_value(nil).for(:description) } it { is_expected.to allow_value('').for(:description) } diff --git a/spec/serializers/champ_serializer_spec.rb b/spec/serializers/champ_serializer_spec.rb index 27868f291..488e7c9a8 100644 --- a/spec/serializers/champ_serializer_spec.rb +++ b/spec/serializers/champ_serializer_spec.rb @@ -65,7 +65,6 @@ describe ChampSerializer do } let(:serialized_id) { champ.type_de_champ.stable_id } let(:serialized_description) { champ.description } - let(:serialized_order_place) { champ.order_place } let(:serialized_libelle) { champ.libelle } let(:serialized_type_champ) { champ.type_champ } let(:serialized_value) { nil } diff --git a/spec/serializers/dossier_serializer_spec.rb b/spec/serializers/dossier_serializer_spec.rb index eb7681891..a4899219b 100644 --- a/spec/serializers/dossier_serializer_spec.rb +++ b/spec/serializers/dossier_serializer_spec.rb @@ -73,7 +73,7 @@ describe DossierSerializer do "libelle" => cloned_type_de_champ.libelle, "description" => 'Pour optimiser vos chances, soignez la chorégraphie et privilégiez le chant polyphonique.', "lien_demarche" => 'https://www.dance-academy.gouv.fr', - "order_place" => cloned_type_de_champ.order_place + "order_place" => 0 } ], pieces_justificatives: [ diff --git a/spec/serializers/procedure_serializer_spec.rb b/spec/serializers/procedure_serializer_spec.rb index e7f64b4bd..81be02559 100644 --- a/spec/serializers/procedure_serializer_spec.rb +++ b/spec/serializers/procedure_serializer_spec.rb @@ -30,7 +30,7 @@ describe ProcedureSerializer do "libelle" => cloned_type_de_champ.libelle, "description" => 'Pour optimiser vos chances, soignez la chorégraphie et privilégiez le chant polyphonique.', "lien_demarche" => 'https://www.dance-academy.gouv.fr', - "order_place" => cloned_type_de_champ.order_place + "order_place" => 0 } ] ) diff --git a/spec/services/procedure_export_service_spec.rb b/spec/services/procedure_export_service_spec.rb index 06d2e619c..5f6a53c3f 100644 --- a/spec/services/procedure_export_service_spec.rb +++ b/spec/services/procedure_export_service_spec.rb @@ -353,8 +353,7 @@ describe ProcedureExportService do let(:other_parent) { create(:type_de_champ_repetition, stable_id: champ_repetition.stable_id) } before do - create(:procedure_revision_type_de_champ, type_de_champ: other_parent, revision: create(:procedure).active_revision, position: 0) - create(:type_de_champ, parent: other_parent) + create(:type_de_champ, parent: create(:procedure_revision_type_de_champ, type_de_champ: other_parent, revision: create(:procedure).active_revision, position: 0)) end it 'should have headers' do diff --git a/spec/system/users/brouillon_spec.rb b/spec/system/users/brouillon_spec.rb index 68d41d392..47d3cf5a6 100644 --- a/spec/system/users/brouillon_spec.rb +++ b/spec/system/users/brouillon_spec.rb @@ -203,7 +203,7 @@ describe 'The user' do let(:old_procedure_with_disabled_pj_validation) do tdcs = [ - create(:type_de_champ_piece_justificative, mandatory: true, libelle: 'Pièce justificative 1', order_place: 1, skip_pj_validation: true) + create(:type_de_champ_piece_justificative, mandatory: true, libelle: 'Pièce justificative 1', position: 1, skip_pj_validation: true) ] create(:procedure, :published, :for_individual, types_de_champ: tdcs) end