diff --git a/app/models/procedure.rb b/app/models/procedure.rb index a4a4d7a50..fcd4b5f6b 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -498,11 +498,7 @@ class Procedure < ApplicationRecord end procedure.save - procedure.draft_types_de_champ.update_all(revision_id: procedure.draft_revision.id) - procedure.draft_types_de_champ_private.update_all(revision_id: procedure.draft_revision.id) - types_de_champ_in_repetition = TypeDeChamp.where(parent: procedure.draft_types_de_champ.repetition + procedure.draft_types_de_champ_private.repetition) - types_de_champ_in_repetition.update_all(revision_id: procedure.draft_revision.id) - types_de_champ_in_repetition.each(&:migrate_parent!) + TypeDeChamp.where(parent: procedure.draft_revision.types_de_champ.repetition).find_each(&:migrate_parent!) if is_different_admin || from_library procedure.draft_types_de_champ.each { |tdc| tdc.options&.delete(:old_pj) } diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index f8c6441b7..b0c872966 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -25,7 +25,6 @@ class ProcedureRevision < ApplicationRecord 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 - has_many :owned_types_de_champ, class_name: 'TypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision has_one :draft_procedure, -> { with_discarded }, class_name: 'Procedure', foreign_key: :draft_revision_id, dependent: :nullify, inverse_of: :draft_revision has_one :published_procedure, -> { with_discarded }, class_name: 'Procedure', foreign_key: :published_revision_id, dependent: :nullify, inverse_of: :published_revision @@ -40,8 +39,6 @@ class ProcedureRevision < ApplicationRecord end def add_type_de_champ(params) - params[:revision] = self - if params[:parent_id] find_or_clone_type_de_champ(params.delete(:parent_id)) .types_de_champ @@ -398,7 +395,6 @@ class ProcedureRevision < ApplicationRecord cloned_type_de_champ = type_de_champ.deep_clone(include: [:types_de_champ]) do |original, kopy| PiecesJustificativesService.clone_attachments(original, kopy) end - cloned_type_de_champ.revision = self revision_type_de_champ.update!(type_de_champ: cloned_type_de_champ) cloned_type_de_champ.types_de_champ.each(&:migrate_parent!) cloned_type_de_champ diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index ec0cbeab5..52259f7f3 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -18,7 +18,7 @@ # stable_id :bigint # class TypeDeChamp < ApplicationRecord - self.ignored_columns = [:migrated_parent] + self.ignored_columns = [:migrated_parent, :revision_id] enum type_champs: { text: 'text', @@ -58,9 +58,6 @@ class TypeDeChamp < ApplicationRecord mesri: 'mesri' } - belongs_to :revision, class_name: 'ProcedureRevision', optional: true - has_one :procedure, through: :revision - 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 @@ -68,6 +65,8 @@ class TypeDeChamp < ApplicationRecord 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 has_many :revisions, -> { ordered }, through: :revision_types_de_champ + has_one :revision, through: :revision_type_de_champ + has_one :procedure, through: :revision delegate :tags_for_template, :libelle_for_export, to: :dynamic_type diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index dd132520d..4077ec9a3 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -354,9 +354,8 @@ def add_types_de_champs(types_de_champ, to: nil, scope: :public) association_name = scope == :private ? :revision_types_de_champ_private : :revision_types_de_champ_public types_de_champ.each do |type_de_champ| - type_de_champ.revision = revision type_de_champ.private = (scope == :private) - type_de_champ.revision.public_send(association_name) << build(:procedure_revision_type_de_champ, + revision.public_send(association_name) << build(:procedure_revision_type_de_champ, revision: revision, position: type_de_champ.order_place, type_de_champ: type_de_champ) diff --git a/spec/factories/type_de_champ.rb b/spec/factories/type_de_champ.rb index 4e0556b52..095709a73 100644 --- a/spec/factories/type_de_champ.rb +++ b/spec/factories/type_de_champ.rb @@ -15,20 +15,19 @@ FactoryBot.define do after(:build) do |type_de_champ, evaluator| if evaluator.procedure - type_de_champ.revision = evaluator.procedure.active_revision + revision = evaluator.procedure.active_revision build(:procedure_revision_type_de_champ, position: evaluator.position, - revision: evaluator.procedure.active_revision, + revision: revision, type_de_champ: type_de_champ) if type_de_champ.private? - type_de_champ.revision.types_de_champ_private << type_de_champ + revision.types_de_champ_private << type_de_champ else - type_de_champ.revision.types_de_champ_public << type_de_champ + revision.types_de_champ_public << type_de_champ end elsif evaluator.parent - type_de_champ.revision = evaluator.parent.revision type_de_champ.order_place = evaluator.position || evaluator.parent.types_de_champ.size evaluator.parent.types_de_champ << type_de_champ else @@ -181,7 +180,6 @@ FactoryBot.define do after(:build) do |type_de_champ_repetition, evaluator| evaluator.types_de_champ.each do |type_de_champ| - type_de_champ.revision = type_de_champ_repetition.revision type_de_champ.order_place = type_de_champ_repetition.types_de_champ.size type_de_champ_repetition.types_de_champ << type_de_champ end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index f49e0e08b..04702ba7f 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -320,7 +320,6 @@ describe Procedure do procedure.validate expect(procedure.errors.full_messages_for(:draft_types_de_champ)).to include(invalid_repetition_error_message) - text_field.revision = repetition.revision text_field.order_place = repetition.types_de_champ.size procedure.draft_types_de_champ.find(&:repetition?).types_de_champ << text_field