Skip to content

Commit 26ce4e3

Browse files
authored
Merge pull request #13406 from demarche-numerique/push-usuuxqzlxsno
Tech: renommer la relation dossier.champs en champ_data
2 parents 3786acb + 7856347 commit 26ce4e3

167 files changed

Lines changed: 539 additions & 539 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/components/instructeurs/cell_component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def simple_layout
3333

3434
def raw_value_for_column(dossier, column)
3535
data = if @column.champ_column?
36-
@dossier.champs.find { _1.stable_id == column.stable_id }
36+
@dossier.champ_data.find { _1.stable_id == column.stable_id }
3737
else
3838
@dossier
3939
end

app/controllers/champs/champ_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Champs::ChampController < ApplicationController
88
private
99

1010
def find_champ
11-
dossier = Dossier.with_revision.includes(:champs).find(params[:dossier_id])
11+
dossier = Dossier.with_revision.includes(:champ_data).find(params[:dossier_id])
1212
authorize dossier, :read?
1313

1414
type_de_champ = dossier.find_type_de_champ_by_stable_id(params[:stable_id])

app/jobs/bulk_route_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class BulkRouteJob < ApplicationJob
66
def perform(procedure)
77
dossiers = procedure.dossiers
88
.with_revision
9-
.includes(:procedure, :groupe_instructeur, :champs)
9+
.includes(:procedure, :groupe_instructeur, :champ_data)
1010
.state_not_termine
1111

1212
dossiers.each do |dossier|

app/jobs/migrations/backfill_dossier_repetition_job.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
class Migrations::BackfillDossierRepetitionJob < ApplicationJob
44
def perform(dossier_ids)
55
Dossier.where(id: dossier_ids)
6-
.includes(:champs, revision: :types_de_champ)
6+
.includes(:champ_data, revision: :types_de_champ)
77
.find_each do |dossier|
88
dossier
99
.revision
1010
.types_de_champ
1111
.filter do |type_de_champ|
12-
type_de_champ.type_champ == 'repetition' && dossier.champs.none? { _1.stable_id == type_de_champ.stable_id }
12+
type_de_champ.type_champ == 'repetition' && dossier.champ_data.none? { _1.stable_id == type_de_champ.stable_id }
1313
end
1414
.each do |type_de_champ|
15-
dossier.champs << type_de_champ.build_champ
15+
dossier.champ_data << type_de_champ.build_champ
1616
end
1717
end
1818
end

app/lib/recovery/importer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def load
9797
import(dossier.justificatif_motivation)
9898
end
9999

100-
dossier.champs.each do |champ|
100+
dossier.champ_data.each do |champ|
101101
champ.piece_justificative_file.each { |pj| import(pj) }
102102

103103
if champ.etablissement.present?

app/models/concerns/dossier_champs_concern.rb

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,19 @@ def merge_instructeur_buffer_stream!
199199
end
200200

201201
def reset_user_buffer_stream!
202-
champs.where(stream: Champ::USER_BUFFER_STREAM).destroy_all
202+
champ_data.where(stream: Champ::USER_BUFFER_STREAM).destroy_all
203203

204204
# update loaded champ instances
205-
association(:champs).target = champs.filter { _1.stream != Champ::USER_BUFFER_STREAM }
205+
association(:champ_data).target = champ_data.filter { _1.stream != Champ::USER_BUFFER_STREAM }
206206

207207
reset_champs_cache
208208
end
209209

210210
def reset_instructeur_buffer_stream!
211-
champs.where(stream: Champ::INSTRUCTEUR_BUFFER_STREAM).destroy_all
211+
champ_data.where(stream: Champ::INSTRUCTEUR_BUFFER_STREAM).destroy_all
212212

213213
# update loaded champ instances
214-
association(:champs).target = champs.filter { _1.stream != Champ::INSTRUCTEUR_BUFFER_STREAM }
214+
association(:champ_data).target = champ_data.filter { _1.stream != Champ::INSTRUCTEUR_BUFFER_STREAM }
215215

216216
reset_champs_cache
217217
end
@@ -268,7 +268,7 @@ def stream
268268
end
269269

270270
def history
271-
champs.filter(&:history_stream?)
271+
champ_data.filter(&:history_stream?)
272272
end
273273

274274
def set_default_value_for_france_connect_champs(user_email)
@@ -302,13 +302,13 @@ def instructeur_changed_columns
302302
private
303303

304304
def changed_champ_ids_for_merge(stream)
305-
stream_h = champs.where(stream:, stable_id: revision_stable_ids)
305+
stream_h = champ_data.where(stream:, stable_id: revision_stable_ids)
306306
.pluck(:stable_id, :row_id, :id)
307307
.to_h { |(stable_id, row_id, id)| [TypeDeChamp.public_id(stable_id, row_id), id] }
308308

309309
return [[], []] if stream_h.empty?
310310

311-
main_h = champs.where(stream: Champ::MAIN_STREAM, stable_id: revision_stable_ids)
311+
main_h = champ_data.where(stream: Champ::MAIN_STREAM, stable_id: revision_stable_ids)
312312
.pluck(:stable_id, :row_id, :id)
313313
.to_h { |(stable_id, row_id, id)| [TypeDeChamp.public_id(stable_id, row_id), id] }
314314

@@ -318,14 +318,14 @@ def changed_champ_ids_for_merge(stream)
318318
changed_ids = main_public_ids.intersection(stream_public_ids).map { main_h[it] }
319319
stream_ids = stream_h.values
320320

321-
# mark champs in discarded rows as changed
322-
discarded_row_ids = champs.where(stream:, stable_id: revision_stable_ids)
321+
# mark champ_data in discarded rows as changed
322+
discarded_row_ids = champ_data.where(stream:, stable_id: revision_stable_ids)
323323
.where.not(row_id: nil)
324324
.where.not(discarded_at: nil)
325325
.pluck(:row_id)
326326

327327
if discarded_row_ids.present?
328-
changed_ids += champs.where(stream: Champ::MAIN_STREAM, row_id: discarded_row_ids).pluck(:id)
328+
changed_ids += champ_data.where(stream: Champ::MAIN_STREAM, row_id: discarded_row_ids).pluck(:id)
329329
end
330330

331331
[stream_ids, changed_ids]
@@ -334,25 +334,25 @@ def changed_champ_ids_for_merge(stream)
334334
def merge_buffer_champs(buffer_ids, changed_ids, stream)
335335
now = Time.zone.now
336336
history_stream = "#{Champ::HISTORY_STREAM}#{now}"
337-
buffer_champs = champs.filter { buffer_ids.member?(it.id) }
337+
buffer_champs = champ_data.filter { buffer_ids.member?(it.id) }
338338

339339
transaction do
340340
# if merging user buffer, discard any instructeur made changes
341341
if stream == Champ::USER_BUFFER_STREAM
342-
champs.where(id: buffer_ids, stream:).pluck(:stable_id, :row_id).each do |(stable_id, row_id)|
343-
champs.where(stream: Champ::INSTRUCTEUR_BUFFER_STREAM, stable_id:, row_id:).destroy_all
342+
champ_data.where(id: buffer_ids, stream:).pluck(:stable_id, :row_id).each do |(stable_id, row_id)|
343+
champ_data.where(stream: Champ::INSTRUCTEUR_BUFFER_STREAM, stable_id:, row_id:).destroy_all
344344
end
345345
end
346346

347-
# move champs with changes from "main" to "history" stream
348-
champs.where(id: changed_ids, stream: Champ::MAIN_STREAM).update_all(stream: history_stream)
349-
# move champs from "buffer" to "main"
350-
champs.where(id: buffer_ids, stream:).update_all(stream: Champ::MAIN_STREAM, updated_at: now, checkpoint: history_stream)
347+
# move champ_data with changes from "main" to "history" stream
348+
champ_data.where(id: changed_ids, stream: Champ::MAIN_STREAM).update_all(stream: history_stream)
349+
# move champ_data from "buffer" to "main"
350+
champ_data.where(id: buffer_ids, stream:).update_all(stream: Champ::MAIN_STREAM, updated_at: now, checkpoint: history_stream)
351351
update_champs_timestamps(buffer_champs, stream)
352352
end
353353

354354
# update loaded champ instances
355-
champs.each do |champ|
355+
champ_data.each do |champ|
356356
if champ.id.in?(changed_ids)
357357
champ.stream = history_stream
358358
elsif champ.id.in?(buffer_ids)
@@ -401,7 +401,7 @@ def champs_on_stream
401401
when Champ::INSTRUCTEUR_BUFFER_STREAM
402402
(champs_on_instructeur_buffer_stream + champs_on_main_stream).uniq(&:public_id)
403403
when Champ::USER_HISTORY_STREAM
404-
champs
404+
champ_data
405405
# only "main" and "history"
406406
.reject(&:buffer_stream?)
407407
# only updates made before last submission
@@ -420,11 +420,11 @@ def revision_stable_ids
420420
end
421421

422422
def champs_in_revision
423-
champs.filter { stable_id_in_revision?(_1.stable_id) }
423+
champ_data.filter { stable_id_in_revision?(_1.stable_id) }
424424
end
425425

426426
def discarded_champs_on_main_stream
427-
champs.filter(&:main_stream?).reject { stable_id_in_revision?(_1.stable_id) }
427+
champ_data.filter(&:main_stream?).reject { stable_id_in_revision?(_1.stable_id) }
428428
end
429429

430430
def champs_on_main_stream
@@ -466,11 +466,11 @@ def champ_upsert_by!(type_de_champ, row_id)
466466
Champ.where(dossier_id: id, row_id: Champ::NULL_ROW_ID).update_all(row_id: nil)
467467

468468
# FIXME: Try to find the champ in memory before querying the database
469-
champ = champs.find { _1.stream == stream && _1.public_id == type_de_champ.public_id(row_id) }
469+
champ = champ_data.find { _1.stream == stream && _1.public_id == type_de_champ.public_id(row_id) }
470470

471471
if champ.nil?
472472
champ = Dossier.no_touching do
473-
champs
473+
champ_data
474474
.create_with(**type_de_champ.params_for_champ, source_stream: stream)
475475
.create_or_find_by!(stable_id: type_de_champ.stable_id, row_id:, stream:)
476476
end
@@ -481,14 +481,14 @@ def champ_upsert_by!(type_de_champ, row_id)
481481
champ = champ.becomes!(type_de_champ.champ_class)
482482
champ.assign_attributes(value: nil, value_json: nil, external_id: nil, data: nil)
483483
elsif stream != Champ::MAIN_STREAM && champ.previously_new_record?
484-
main_stream_champ = champs.find_by(stable_id: type_de_champ.stable_id, row_id:, stream: Champ::MAIN_STREAM)
484+
main_stream_champ = champ_data.find_by(stable_id: type_de_champ.stable_id, row_id:, stream: Champ::MAIN_STREAM)
485485
champ.clone_value_from(main_stream_champ) if main_stream_champ.present?
486486
end
487487

488-
# If the champ returned from `create_or_find_by` is not the same as the one already loaded in `dossier.champs`, we need to update the association cache
489-
loaded_champ = champs.find { [_1.stream, _1.public_id] == [champ.stream, champ.public_id] }
488+
# If the champ returned from `create_or_find_by` is not the same as the one already loaded in `dossier.champ_data`, we need to update the association cache
489+
loaded_champ = champ_data.find { [_1.stream, _1.public_id] == [champ.stream, champ.public_id] }
490490
if loaded_champ.present? && loaded_champ.object_id != champ.object_id
491-
association(:champs).target = champs - [loaded_champ] + [champ]
491+
association(:champ_data).target = champ_data - [loaded_champ] + [champ]
492492
end
493493

494494
# If the dossier instance on champ has changed we need to update the association cache

app/models/concerns/dossier_clone_concern.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def clone(user: nil)
2727
kopy.parent_dossier = original
2828
kopy.user = user || original.user
2929
kopy.state = Dossier.states.fetch(:brouillon)
30-
kopy.champs = cloned_champs.map do |champ|
30+
kopy.champ_data = cloned_champs.map do |champ|
3131
champ.dossier = kopy
3232
champ
3333
end

app/models/concerns/dossier_rebase_concern.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ def rebase
4040
# update dossier revision
4141
update_column(:revision_id, target_revision.id)
4242

43-
# mark updated champs as rebased
44-
champs.where(stable_id: updated_stable_ids).update_all(rebased_at: Time.zone.now)
43+
# mark updated champ_data as rebased
44+
champ_data.where(stable_id: updated_stable_ids).update_all(rebased_at: Time.zone.now)
4545

4646
# add rows for new repetitions
4747
target_revision
4848
.types_de_champ
4949
.filter { _1.repetition? && _1.stable_id.in?(added_stable_ids) && (_1.mandatory? || _1.private?) }
5050
.each do |type_de_champ|
51-
self.champs << type_de_champ.build_champ(row_id: ULID.generate, rebased_at: Time.zone.now)
51+
self.champ_data << type_de_champ.build_champ(row_id: ULID.generate, rebased_at: Time.zone.now)
5252
end
5353
end
5454
end

app/models/concerns/dossier_state_concern.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,14 @@ def clean_champs_after_instruction!
391391
private
392392

393393
def remove_not_in_revision_champs!
394-
champs.where.not(stable_id: revision_stable_ids).where(stream: Champ::MAIN_STREAM).destroy_all
394+
champ_data.where.not(stable_id: revision_stable_ids).where(stream: Champ::MAIN_STREAM).destroy_all
395395
end
396396

397397
def remove_discarded_rows!
398-
row_to_remove_ids = champs.filter { _1.row? && _1.discarded? }.map(&:row_id)
398+
row_to_remove_ids = champ_data.filter { _1.row? && _1.discarded? }.map(&:row_id)
399399

400400
return if row_to_remove_ids.empty?
401-
champs.where(row_id: row_to_remove_ids, stream: Champ::MAIN_STREAM).destroy_all
401+
champ_data.where(row_id: row_to_remove_ids, stream: Champ::MAIN_STREAM).destroy_all
402402
end
403403

404404
def remove_not_visible_or_empty_repetitions!
@@ -407,27 +407,27 @@ def remove_not_visible_or_empty_repetitions!
407407
.flat_map(&:row_ids)
408408

409409
return if row_to_remove_ids.empty?
410-
champs.where(row_id: row_to_remove_ids, stream: Champ::MAIN_STREAM).destroy_all
410+
champ_data.where(row_id: row_to_remove_ids, stream: Champ::MAIN_STREAM).destroy_all
411411
end
412412

413413
def clear_not_visible_or_empty_champs!
414414
champs_to_clear = project_champs_public_all
415415
.reject(&:repetition?)
416416
.filter { _1.blank? || !_1.visible? }
417417

418-
champs.where(id: champs_to_clear, stream: Champ::MAIN_STREAM).find_each(&:clear)
418+
champ_data.where(id: champs_to_clear, stream: Champ::MAIN_STREAM).find_each(&:clear)
419419
end
420420

421421
def clear_france_connect_champs_piece_justificatives!
422422
champs_to_clear = project_champs_public.filter(&:france_connect?)
423423

424424
return if champs_to_clear.empty?
425-
champs.where(id: champs_to_clear).find_each(&:clear_piece_justificative)
425+
champ_data.where(id: champs_to_clear).find_each(&:clear_piece_justificative)
426426
end
427427

428428
def clear_titres_identite!
429-
champ_to_clear_stable_ids = champs.filter do |champ|
430-
# Use STI type to avoid querying type_de_champ for orphaned champs
429+
champ_to_clear_stable_ids = champ_data.filter do |champ|
430+
# Use STI type to avoid querying type_de_champ for orphaned champ_data
431431
next false unless champ.is_a?(Champs::PieceJustificativeChamp)
432432

433433
begin
@@ -438,7 +438,7 @@ def clear_titres_identite!
438438
end
439439
end.to_set(&:stable_id)
440440

441-
champs.where(stable_id: champ_to_clear_stable_ids).find_each(&:clear)
441+
champ_data.where(stable_id: champ_to_clear_stable_ids).find_each(&:clear)
442442
end
443443

444444
def clear_auto_purged_piece_justificatives!
@@ -450,7 +450,7 @@ def clear_auto_purged_piece_justificatives!
450450
.filter(&:pj_auto_purge?)
451451
.map(&:stable_id)
452452

453-
champs.where(stable_id: champ_to_clear_stable_ids).find_each(&:clear)
453+
champ_data.where(stable_id: champ_to_clear_stable_ids).find_each(&:clear)
454454
end
455455

456456
def remove_attente_avis_notification
@@ -462,7 +462,7 @@ def remove_auto_purged_piece_justificatives!
462462

463463
return if champ_to_remove_ids.empty?
464464

465-
champs.where(id: champ_to_remove_ids, stream: Champ::MAIN_STREAM).destroy_all
465+
champ_data.where(id: champ_to_remove_ids, stream: Champ::MAIN_STREAM).destroy_all
466466
end
467467

468468
def enqueue_ami_notification(trigger: :dossier_state_change, state: nil)

app/models/dossier.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def self.instruction_action?(action)
6060

6161
# autosave is required to import champ validation errors on the dossier
6262
# when validating with the :champs_public_value/:champs_private_value contexts
63-
has_many :champs, dependent: :destroy, autosave: true
63+
has_many :champ_data, dependent: :destroy, class_name: 'Champ', autosave: true
6464
has_many :commentaires, inverse_of: :dossier, dependent: :destroy
6565
has_many :commentaires_chronological, -> { chronological }, class_name: 'Commentaire', inverse_of: :dossier
6666
has_many :preloaded_commentaires, -> { includes(:dossier_correction, :dossier_pending_response, :instructeur, :expert, piece_jointe_attachments: :blob).order(created_at: :desc) }, class_name: 'Commentaire', inverse_of: :dossier
@@ -280,7 +280,7 @@ def classer_sans_suite(motivation: nil, instructeur: nil, processed_at: Time.zon
280280
scope :hidden_by_administration_since, -> (since) { where('dossiers.hidden_by_administration_at IS NOT NULL AND dossiers.hidden_by_administration_at >= ?', since) }
281281
scope :hidden_since, -> (since) { hidden_by_user_since(since).or(hidden_by_administration_since(since)) }
282282

283-
scope :with_type_de_champ, -> (stable_id) { joins(:champs).where(champs: { stream: 'main', stable_id: }) }
283+
scope :with_type_de_champ, -> (stable_id) { joins(:champ_data).where(champs: { stream: 'main', stable_id: }) }
284284
scope :without_type_de_champ, -> (stable_id) { where.not(id: with_type_de_champ(stable_id).select(:id)) }
285285

286286
scope :with_unread_messages_for_user, -> {
@@ -1139,7 +1139,7 @@ def prefill_and_enqueue_fetch_external_data_jobs(champs, types_de_champ)
11391139
def purge_freeing_champs_cascade
11401140
transaction do
11411141
yield if block_given?
1142-
champs.in_batches(of: 50).each(&:destroy_all)
1142+
champ_data.in_batches(of: 50).each(&:destroy_all)
11431143
destroy
11441144
rescue => e
11451145
Sentry.capture_exception(e, extra: { dossier: id })
@@ -1150,12 +1150,12 @@ def purge_freeing_champs_cascade
11501150
end
11511151

11521152
def build_default_champs
1153-
build_default_champs_for(revision.types_de_champ_public) if !champs.any?(&:public?)
1154-
build_default_champs_for(revision.types_de_champ_private) if !champs.any?(&:private?)
1153+
build_default_champs_for(revision.types_de_champ_public) if !champ_data.any?(&:public?)
1154+
build_default_champs_for(revision.types_de_champ_private) if !champ_data.any?(&:private?)
11551155
end
11561156

11571157
def build_default_champs_for(types_de_champ)
1158-
self.champs << types_de_champ.filter(&:fillable?).filter_map do |type_de_champ|
1158+
self.champ_data << types_de_champ.filter(&:fillable?).filter_map do |type_de_champ|
11591159
if type_de_champ.repetition?
11601160
if type_de_champ.private? || type_de_champ.mandatory?
11611161
type_de_champ.build_champ(dossier: self, row_id: ULID.generate)

0 commit comments

Comments
 (0)