Skip to content

Commit f0db321

Browse files
authored
Merge pull request #13477 from demarche-numerique/push-ukyolpppunvm
Tech: déplace les constantes et helpers de stream dans des concerns dédiés
2 parents 4d5149a + a2ca40a commit f0db321

27 files changed

Lines changed: 202 additions & 169 deletions

app/components/dossiers/champ_updated_component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def updated_by_instructeur?
3030
end
3131

3232
def instructeur?
33-
source_stream == Champ::INSTRUCTEUR_BUFFER_STREAM
33+
source_stream == Dossier::INSTRUCTEUR_BUFFER_STREAM
3434
end
3535

3636
def tooltip_id

app/components/dossiers/champs_rows_show_component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def row_show_component(champ)
2626
end
2727

2828
def updated_by(champ)
29-
if usager? && champ.source_stream == Champ::INSTRUCTEUR_BUFFER_STREAM && champ.procedure.hide_instructeurs_email?
29+
if usager? && champ.instructeur_buffer_source_stream? && champ.procedure.hide_instructeurs_email?
3030
nil
3131
else
3232
champ.updated_by

app/components/editable_champ/editable_champ_component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def stimulus_values
100100
def turbo_poll_url_value
101101
if @champ.private?
102102
annotation_instructeur_dossier_path(@champ.dossier.procedure, @champ.dossier, @champ.stable_id, row_id: @champ.row_id)
103-
elsif @champ.dossier.stream == Champ::INSTRUCTEUR_BUFFER_STREAM
103+
elsif @champ.dossier.instructeur_buffer_stream?
104104
champ_instructeur_dossier_path(@champ.dossier.procedure, @champ.dossier, @champ.stable_id, row_id: @champ.row_id)
105105
else
106106
champ_dossier_path(@champ.dossier, @champ.stable_id, row_id: @champ.row_id)

app/controllers/attachments_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def user_or_invite_changing_an_attachment?
7373
end
7474

7575
def instructeur_changing_an_attachment?
76-
(champ&.private? || champ&.stream == Champ::INSTRUCTEUR_BUFFER_STREAM) && current_user.instructeur? && current_instructeur.in?(champ.dossier.groupe_instructeur.instructeurs)
76+
(champ&.private? || champ&.instructeur_buffer_stream?) && current_user.instructeur? && current_instructeur.in?(champ.dossier.groupe_instructeur.instructeurs)
7777
end
7878

7979
def admin_changing_its_procedure?

app/jobs/migrations/backfill_stable_id_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Migrations::BackfillStableIdJob < ApplicationJob
66
DEFAULT_LIMIT = 50_000
77

88
def perform(iteration, limit = DEFAULT_LIMIT)
9-
sql = "UPDATE champs SET stable_id = t.stable_id, stream = 'main'
9+
sql = "UPDATE champs SET stable_id = t.stable_id, stream = '#{Dossier::MAIN_STREAM}'
1010
FROM types_de_champ t
1111
WHERE champs.type_de_champ_id = t.id
1212
AND champs.id IN (SELECT id FROM champs WHERE champs.stable_id IS NULL LIMIT ?);"

app/models/champ.rb

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Champ < ApplicationRecord
55
include ChampValidateConcern
66
include ChampRevisionConcern
77
include ChampExternalDataConcern
8+
include ChampStreamConcern
89

910
self.ignored_columns += [:type_de_champ_id, :parent_id]
1011

@@ -265,7 +266,7 @@ def clone
265266
deep_clone(only: champ_attributes + value_attributes, include: relationships, validate: true) do |original, kopy|
266267
if original.is_a?(Champ)
267268
kopy.write_attribute(:stable_id, original.stable_id)
268-
kopy.write_attribute(:stream, 'main')
269+
kopy.write_attribute(:stream, Dossier::MAIN_STREAM)
269270
# TODO: overwrite row_id "N" with nil
270271
kopy.write_attribute(:row_id, kopy.row_id)
271272
end
@@ -285,32 +286,6 @@ def html_id
285286
type_de_champ.html_id(row_id)
286287
end
287288

288-
MAIN_STREAM = 'main'
289-
USER_BUFFER_STREAM = 'user:buffer'
290-
INSTRUCTEUR_BUFFER_STREAM = 'instructeur:buffer'
291-
USER_HISTORY_STREAM = 'user:history'
292-
HISTORY_STREAM = 'history:'
293-
294-
def main_stream?
295-
stream == MAIN_STREAM
296-
end
297-
298-
def user_buffer_stream?
299-
stream == USER_BUFFER_STREAM
300-
end
301-
302-
def instructeur_buffer_stream?
303-
stream == INSTRUCTEUR_BUFFER_STREAM
304-
end
305-
306-
def history_stream?
307-
stream.start_with?(HISTORY_STREAM)
308-
end
309-
310-
def buffer_stream?
311-
persisted? && (user_buffer_stream? || instructeur_buffer_stream?)
312-
end
313-
314289
def clear
315290
update_columns(value: nil, value_json: nil, external_id: nil, data: nil)
316291
Champ.no_touching do

app/models/champs/referentiel_champ.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def propagate_prefill(types_de_champ)
100100
private
101101

102102
def prefillable_types_de_champ
103-
if stream == Champ::MAIN_STREAM
103+
if main_stream?
104104
dossier.revision.types_de_champ
105105
else
106106
dossier.types_de_champ_public_all
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
module ChampStreamConcern
4+
extend ActiveSupport::Concern
5+
6+
def main_stream?
7+
stream == Dossier::MAIN_STREAM
8+
end
9+
10+
def user_buffer_stream?
11+
stream == Dossier::USER_BUFFER_STREAM
12+
end
13+
14+
def instructeur_buffer_stream?
15+
stream == Dossier::INSTRUCTEUR_BUFFER_STREAM
16+
end
17+
18+
def history_stream?
19+
stream.start_with?(Dossier::HISTORY_STREAM)
20+
end
21+
22+
def buffer_stream?
23+
persisted? && (user_buffer_stream? || instructeur_buffer_stream?)
24+
end
25+
26+
def instructeur_buffer_source_stream?
27+
source_stream == Dossier::INSTRUCTEUR_BUFFER_STREAM
28+
end
29+
end

app/models/concerns/dossier_champs_concern.rb

Lines changed: 23 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -183,35 +183,35 @@ def reload
183183
end
184184

185185
def merge_user_buffer_stream!
186-
buffer_ids, changed_ids = changed_champ_ids_for_merge(Champ::USER_BUFFER_STREAM)
186+
buffer_ids, changed_ids = changed_champ_ids_for_merge(Dossier::USER_BUFFER_STREAM)
187187

188188
return if buffer_ids.blank?
189189

190-
merge_buffer_champs(buffer_ids, changed_ids, Champ::USER_BUFFER_STREAM)
190+
merge_buffer_champs(buffer_ids, changed_ids, Dossier::USER_BUFFER_STREAM)
191191
end
192192

193193
def merge_instructeur_buffer_stream!
194-
buffer_ids, changed_ids = changed_champ_ids_for_merge(Champ::INSTRUCTEUR_BUFFER_STREAM)
194+
buffer_ids, changed_ids = changed_champ_ids_for_merge(Dossier::INSTRUCTEUR_BUFFER_STREAM)
195195

196196
return if buffer_ids.blank?
197197

198-
merge_buffer_champs(buffer_ids, changed_ids, Champ::INSTRUCTEUR_BUFFER_STREAM)
198+
merge_buffer_champs(buffer_ids, changed_ids, Dossier::INSTRUCTEUR_BUFFER_STREAM)
199199
end
200200

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

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

207207
reset_champs_cache
208208
end
209209

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

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

216216
reset_champs_cache
217217
end
@@ -224,49 +224,6 @@ def instructeur_buffer_changes?
224224
champs_on_instructeur_buffer_stream.present?
225225
end
226226

227-
def can_update_as_user?(user)
228-
return false unless en_construction?
229-
user.owns_or_invite?(self)
230-
end
231-
232-
def can_update_as_instructeur?(user)
233-
return false unless en_construction?
234-
return false unless procedure.instructeurs_can_edit_dossiers?
235-
return false unless user.instructeur?
236-
return false if can_update_as_user?(user)
237-
groupe_instructeur.instructeurs.include?(user.instructeur)
238-
end
239-
240-
def with_update_stream(user, &block)
241-
if can_update_as_user?(user)
242-
with_stream(Champ::USER_BUFFER_STREAM, &block)
243-
elsif can_update_as_instructeur?(user)
244-
with_stream(Champ::INSTRUCTEUR_BUFFER_STREAM, &block)
245-
else
246-
with_stream(Champ::MAIN_STREAM, &block)
247-
end
248-
end
249-
250-
def with_main_stream(&block)
251-
with_stream(Champ::MAIN_STREAM, &block)
252-
end
253-
254-
def with_instructeur_buffer_stream(&block)
255-
with_stream(Champ::INSTRUCTEUR_BUFFER_STREAM, &block)
256-
end
257-
258-
def with_user_history_stream(&block)
259-
with_stream(Champ::USER_HISTORY_STREAM, &block)
260-
end
261-
262-
def with_champ_stream(champ, &block)
263-
with_stream(champ.stream, &block)
264-
end
265-
266-
def stream
267-
@stream || Champ::MAIN_STREAM
268-
end
269-
270227
def history
271228
champ_data.filter(&:history_stream?)
272229
end
@@ -308,7 +265,7 @@ def changed_champ_ids_for_merge(stream)
308265

309266
return [[], []] if stream_h.empty?
310267

311-
main_h = champ_data.where(stream: Champ::MAIN_STREAM, stable_id: revision_stable_ids)
268+
main_h = champ_data.where(stream: Dossier::MAIN_STREAM, stable_id: revision_stable_ids)
312269
.pluck(:stable_id, :row_id, :id)
313270
.to_h { |(stable_id, row_id, id)| [TypeDeChamp.public_id(stable_id, row_id), id] }
314271

@@ -325,29 +282,29 @@ def changed_champ_ids_for_merge(stream)
325282
.pluck(:row_id)
326283

327284
if discarded_row_ids.present?
328-
changed_ids += champ_data.where(stream: Champ::MAIN_STREAM, row_id: discarded_row_ids).pluck(:id)
285+
changed_ids += champ_data.where(stream: Dossier::MAIN_STREAM, row_id: discarded_row_ids).pluck(:id)
329286
end
330287

331288
[stream_ids, changed_ids]
332289
end
333290

334291
def merge_buffer_champs(buffer_ids, changed_ids, stream)
335292
now = Time.zone.now
336-
history_stream = "#{Champ::HISTORY_STREAM}#{now}"
293+
history_stream = "#{Dossier::HISTORY_STREAM}#{now}"
337294
buffer_champs = champ_data.filter { buffer_ids.member?(it.id) }
338295

339296
transaction do
340297
# if merging user buffer, discard any instructeur made changes
341-
if stream == Champ::USER_BUFFER_STREAM
298+
if stream == Dossier::USER_BUFFER_STREAM
342299
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
300+
champ_data.where(stream: Dossier::INSTRUCTEUR_BUFFER_STREAM, stable_id:, row_id:).destroy_all
344301
end
345302
end
346303

347304
# 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)
305+
champ_data.where(id: changed_ids, stream: Dossier::MAIN_STREAM).update_all(stream: history_stream)
349306
# 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)
307+
champ_data.where(id: buffer_ids, stream:).update_all(stream: Dossier::MAIN_STREAM, updated_at: now, checkpoint: history_stream)
351308
update_champs_timestamps(buffer_champs, stream)
352309
end
353310

@@ -356,7 +313,7 @@ def merge_buffer_champs(buffer_ids, changed_ids, stream)
356313
if champ.id.in?(changed_ids)
357314
champ.stream = history_stream
358315
elsif champ.id.in?(buffer_ids)
359-
champ.stream = Champ::MAIN_STREAM
316+
champ.stream = Dossier::MAIN_STREAM
360317
champ.checkpoint = history_stream
361318
end
362319
end
@@ -370,22 +327,6 @@ def merge_buffer_champs(buffer_ids, changed_ids, stream)
370327
history_stream
371328
end
372329

373-
def with_stream(stream)
374-
if block_given?
375-
previous_stream = @stream
376-
@stream = stream
377-
reset_champs_cache
378-
result = yield
379-
@stream = previous_stream
380-
reset_champs_cache
381-
result
382-
else
383-
@stream = stream
384-
reset_champs_cache
385-
self
386-
end
387-
end
388-
389330
def champs_by_public_id
390331
@champs_by_public_id ||= champs_on_stream.index_by(&:public_id)
391332
end
@@ -395,12 +336,11 @@ def discarded_champs_by_public_id
395336
end
396337

397338
def champs_on_stream
398-
@champs_on_stream ||= case stream
399-
when Champ::USER_BUFFER_STREAM
339+
@champs_on_stream ||= if user_buffer_stream?
400340
(champs_on_user_buffer_stream + champs_on_main_stream).uniq(&:public_id)
401-
when Champ::INSTRUCTEUR_BUFFER_STREAM
341+
elsif instructeur_buffer_stream?
402342
(champs_on_instructeur_buffer_stream + champs_on_main_stream).uniq(&:public_id)
403-
when Champ::USER_HISTORY_STREAM
343+
elsif user_history_stream?
404344
champ_data
405345
# only "main" and "history"
406346
.reject(&:buffer_stream?)
@@ -480,8 +420,8 @@ def champ_upsert_by!(type_de_champ, row_id)
480420
if champ.class != type_de_champ.champ_class
481421
champ = champ.becomes!(type_de_champ.champ_class)
482422
champ.assign_attributes(value: nil, value_json: nil, external_id: nil, data: nil)
483-
elsif stream != Champ::MAIN_STREAM && champ.previously_new_record?
484-
main_stream_champ = champ_data.find_by(stable_id: type_de_champ.stable_id, row_id:, stream: Champ::MAIN_STREAM)
423+
elsif !main_stream? && champ.previously_new_record?
424+
main_stream_champ = champ_data.find_by(stable_id: type_de_champ.stable_id, row_id:, stream: Dossier::MAIN_STREAM)
485425
champ.clone_value_from(main_stream_champ) if main_stream_champ.present?
486426
end
487427

@@ -505,10 +445,10 @@ def champ_upsert_by!(type_de_champ, row_id)
505445

506446
def check_valid_stream_on_write?(type_de_champ)
507447
if type_de_champ.private?
508-
if stream != Champ::MAIN_STREAM
448+
if !main_stream?
509449
raise "Can not write a private champ to \"#{stream}\" stream"
510450
end
511-
elsif stream == Champ::MAIN_STREAM && en_construction?
451+
elsif main_stream? && en_construction?
512452
raise 'Can not write to "main" stream on a dossier "en construction"'
513453
end
514454
end

app/models/concerns/dossier_state_concern.rb

Lines changed: 5 additions & 5 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-
champ_data.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: Dossier::MAIN_STREAM).destroy_all
395395
end
396396

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

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

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

409409
return if row_to_remove_ids.empty?
410-
champ_data.where(row_id: row_to_remove_ids, stream: Champ::MAIN_STREAM).destroy_all
410+
champ_data.where(row_id: row_to_remove_ids, stream: Dossier::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-
champ_data.where(id: champs_to_clear, stream: Champ::MAIN_STREAM).find_each(&:clear)
418+
champ_data.where(id: champs_to_clear, stream: Dossier::MAIN_STREAM).find_each(&:clear)
419419
end
420420

421421
def clear_france_connect_champs_piece_justificatives!
@@ -462,7 +462,7 @@ def remove_auto_purged_piece_justificatives!
462462

463463
return if champ_to_remove_ids.empty?
464464

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

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

0 commit comments

Comments
 (0)