refactor(revision): remove old code

This commit is contained in:
Paul Chavard 2022-05-24 15:57:31 +02:00
parent 721b5fd0ec
commit 6f98def288
8 changed files with 44 additions and 103 deletions

View file

@ -26,7 +26,7 @@ module Administrateurs
end end
def move 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 head :no_content
end end

View file

@ -34,7 +34,6 @@ class Champ < ApplicationRecord
delegate :libelle, delegate :libelle,
:type_champ, :type_champ,
:procedure, :procedure,
:order_place,
:mandatory?, :mandatory?,
:description, :description,
:drop_down_list_options, :drop_down_list_options,

View file

@ -165,7 +165,7 @@ class Procedure < ApplicationRecord
if brouillon? if brouillon?
draft_types_de_champ draft_types_de_champ
else else
TypeDeChamp.root TypeDeChamp
.public_only .public_only
.fillable .fillable
.joins(:revisions) .joins(:revisions)
@ -181,7 +181,7 @@ class Procedure < ApplicationRecord
if brouillon? if brouillon?
draft_types_de_champ_private draft_types_de_champ_private
else else
TypeDeChamp.root TypeDeChamp
.private_only .private_only
.fillable .fillable
.joins(:revisions) .joins(:revisions)
@ -460,12 +460,7 @@ class Procedure < ApplicationRecord
populate_champ_stable_ids populate_champ_stable_ids
include_list = { include_list = {
draft_revision: { draft_revision: {
revision_types_de_champ_public: { revision_types_de_champ: [:type_de_champ],
type_de_champ: :types_de_champ
},
revision_types_de_champ_private: {
type_de_champ: :types_de_champ
},
attestation_template: [], attestation_template: [],
dossier_submitted_message: [] dossier_submitted_message: []
} }
@ -515,7 +510,8 @@ class Procedure < ApplicationRecord
end end
procedure.save 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 if is_different_admin || from_library
procedure.draft_types_de_champ.each { |tdc| tdc.options&.delete(:old_pj) } 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]) .deep_clone(include: [:revision_types_de_champ])
.tap(&:save!) .tap(&:save!)
children = new_draft.revision_types_de_champ.where.not(parent_id: nil) move_new_children_to_new_parent_coordinate(new_draft)
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!)
new_draft new_draft
end end
@ -795,6 +777,16 @@ class Procedure < ApplicationRecord
private 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? def validate_for_publication?
validation_context == :publication || publiee? validation_context == :publication || publiee?
end end

View file

@ -39,22 +39,15 @@ class ProcedureRevision < ApplicationRecord
end end
def add_type_de_champ(params) def add_type_de_champ(params)
parent_stable_id = params[:parent_id] parent_stable_id = params.delete(:parent_id)
coordinate = {} coordinate = {}
if parent_stable_id.present? 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) parent_coordinate, parent = coordinate_and_tdc(parent_stable_id)
coordinate[:parent_id] = parent_coordinate.id coordinate[:parent_id] = parent_coordinate.id
coordinate[:position] = children_of(parent).count coordinate[:position] = children_of(parent).count
# old system
params[:order_place] = coordinate[:position]
params[:parent_id] = parent.id
elsif params[:private] elsif params[:private]
coordinate[:position] = revision_types_de_champ_private.count coordinate[:position] = revision_types_de_champ_private.count
else else
@ -72,25 +65,17 @@ class ProcedureRevision < ApplicationRecord
TypeDeChamp.new.tap { |tdc| tdc.errors.add(:base, e.message) } TypeDeChamp.new.tap { |tdc| tdc.errors.add(:base, e.message) }
end end
# Only called by by controller update
def find_or_clone_type_de_champ(stable_id) 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)
coordinate, tdc = coordinate_and_tdc(stable_id) coordinate, tdc = coordinate_and_tdc(stable_id)
if tdc.only_present_on_draft? if tdc.only_present_on_draft?
tdc tdc
else else
replace_tdc_and_children_by_clones(coordinate) replace_type_de_champ_by_clone(coordinate)
end end
end end
def move_type_de_champ(stable_id, position) 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) coordinate, _ = coordinate_and_tdc(stable_id)
siblings = coordinate.siblings.to_a siblings = coordinate.siblings.to_a
@ -101,17 +86,13 @@ class ProcedureRevision < ApplicationRecord
end end
def remove_type_de_champ(stable_id) 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) coordinate, tdc = coordinate_and_tdc(stable_id)
children = children_of(tdc).to_a
coordinate.destroy coordinate.destroy
if tdc.revision_types_de_champ.empty? children.each(&:destroy_if_orphan)
tdc.destroy tdc.destroy_if_orphan
end
reorder(coordinate.siblings) reorder(coordinate.siblings)
end end
@ -166,6 +147,12 @@ class ProcedureRevision < ApplicationRecord
.order("procedure_revision_types_de_champ.position") .order("procedure_revision_types_de_champ.position")
end 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 def types_de_champ_public_as_json
types_de_champ = types_de_champ_public.includes(piece_justificative_template_attachment: :blob) types_de_champ = types_de_champ_public.includes(piece_justificative_template_attachment: :blob)
tdcs_as_json = types_de_champ.map(&:as_json_for_editor) tdcs_as_json = types_de_champ.map(&:as_json_for_editor)
@ -217,11 +204,6 @@ class ProcedureRevision < ApplicationRecord
def reorder(siblings) def reorder(siblings)
siblings.to_a.compact.each.with_index do |sibling, position| siblings.to_a.compact.each.with_index do |sibling, position|
sibling.update(position: 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 end
end end
@ -451,29 +433,11 @@ class ProcedureRevision < ApplicationRecord
changes changes
end end
def replace_tdc_and_children_by_clones(coordinate) def replace_type_de_champ_by_clone(coordinate)
cloned_type_de_champ = coordinate.type_de_champ.deep_clone(include: [:types_de_champ]) do |original, kopy| cloned_type_de_champ = coordinate.type_de_champ.deep_clone do |original, kopy|
PiecesJustificativesService.clone_attachments(original, kopy) PiecesJustificativesService.clone_attachments(original, kopy)
end end
cloned_child_types_de_champ = cloned_type_de_champ.types_de_champ
coordinate.update!(type_de_champ: cloned_type_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 cloned_type_de_champ
end 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 end

View file

@ -7,16 +7,14 @@
# libelle :string # libelle :string
# mandatory :boolean default(FALSE) # mandatory :boolean default(FALSE)
# options :jsonb # options :jsonb
# order_place :integer
# private :boolean default(FALSE), not null # private :boolean default(FALSE), not null
# type_champ :string # type_champ :string
# created_at :datetime # created_at :datetime
# updated_at :datetime # updated_at :datetime
# parent_id :bigint
# stable_id :bigint # stable_id :bigint
# #
class TypeDeChamp < ApplicationRecord class TypeDeChamp < ApplicationRecord
self.ignored_columns = [:migrated_parent, :revision_id] self.ignored_columns = [:migrated_parent, :revision_id, :parent_id, :order_place]
enum type_champs: { enum type_champs: {
text: 'text', text: 'text',
@ -56,9 +54,6 @@ class TypeDeChamp < ApplicationRecord
mesri: 'mesri' 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 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_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_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 :public_only, -> { where(private: false) }
scope :private_only, -> { where(private: true) } 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 :repetition, -> { where(type_champ: type_champs.fetch(:repetition)) }
scope :not_repetition, -> { where.not(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)]) } 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 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 :libelle, presence: true, allow_blank: false, allow_nil: false
validates :type_champ, 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: [ except: [
:created_at, :created_at,
:options, :options,
:order_place,
:parent_id,
:private, :private,
:stable_id, :stable_id,
:type, :type,
@ -371,17 +360,11 @@ class TypeDeChamp < ApplicationRecord
end end
end end
def migrate_parent! def destroy_if_orphan
if parent_id.present? && revision_types_de_champ.empty? if revision_types_de_champ.empty?
parent.revision_types_de_champ.each do |revision_type_de_champ| destroy
ProcedureRevisionTypeDeChamp.create(parent: revision_type_de_champ,
type_de_champ: self,
revision_id: revision_type_de_champ.revision_id,
position: order_place)
end end
end end
self
end
private private
@ -413,8 +396,7 @@ class TypeDeChamp < ApplicationRecord
if !repetition? && procedure.present? if !repetition? && procedure.present?
procedure procedure
.draft_revision # action occurs only on draft .draft_revision # action occurs only on draft
.children_of(self) .remove_children_of(self)
.destroy_all
end end
end end
end end

View file

@ -8,8 +8,8 @@ class TypesDeChamp::RepetitionTypeDeChamp < TypesDeChamp::TypeDeChampBase
def estimated_fill_duration(revision) def estimated_fill_duration(revision)
estimated_rows_in_repetition = 2.5 estimated_rows_in_repetition = 2.5
estimated_row_duration = @type_de_champ estimated_row_duration = revision
.types_de_champ .children_of(@type_de_champ)
.map { |child_tdc| child_tdc.estimated_fill_duration(revision) } .map { |child_tdc| child_tdc.estimated_fill_duration(revision) }
.sum .sum
estimated_row_duration * estimated_rows_in_repetition estimated_row_duration * estimated_rows_in_repetition

View file

@ -8,4 +8,8 @@ class TypeDeChampSerializer < ActiveModel::Serializer
def id def id
object.stable_id || object.id object.stable_id || object.id
end end
def order_place
-1
end
end end

View file

@ -8,7 +8,7 @@
%td.cell-label Type de champ %td.cell-label Type de champ
%td.cell-label Modifier le modèle %td.cell-label Modifier le modèle
%tbody %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', = render partial: 'fields/types_de_champ_collection_field/type_champ_line',
locals: { type_de_champ: type_de_champ } locals: { type_de_champ: type_de_champ }