Skip to content

Commit 2038855

Browse files
tchakclaude
andcommitted
tech: section components navigate champs with the tree api
Refactor ViewableChamp::SectionComponent and EditableChamp::SectionComponent to take a list of champs (tree entry points) and recurse through HeaderSectionChamp#children, instead of rebuilding the tree from flat types de champ and projecting champs one by one. Repetition rows go through RepetitionRow#champs_tree, removing the row_id plumbing from section components. Root callers pass dossier.champs_public_tree and champs_private_tree. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent db45282 commit 2038855

13 files changed

Lines changed: 40 additions & 86 deletions

File tree

app/components/dossiers/champs_rows_show_component/champs_rows_show_component.html.erb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<% each_champ do |champ| %>
22
<% if champ.repetition? %>
3-
<% types_de_champ = champ.dossier.revision.children_of(champ.type_de_champ) %>
4-
<% champ.row_ids.each.with_index(1) do |row_id, row_number| %>
3+
<% champ.rows.each do |row| %>
54
<div class="fr-background-alt--grey fr-p-2w fr-my-3w fr-ml-2w champ-repetition">
6-
<%= tag.send(repetition_heading_tag, t(".repetition_heading", libelle: champ.libelle, row_number: row_number), class: "fr-h6 fr-text--bold") %>
7-
<%= render ViewableChamp::SectionComponent.new(dossier: champ.dossier, types_de_champ:, row_id:, demande_seen_at: seen_at, profile:) %>
5+
<%= tag.send(repetition_heading_tag, t(".repetition_heading", libelle: champ.libelle, row_number: row.index), class: "fr-h6 fr-text--bold") %>
6+
<%= render ViewableChamp::SectionComponent.new(dossier: champ.dossier, champs: row.champs_tree, demande_seen_at: seen_at, profile:) %>
87
</div>
98
<% end %>
109
<% else %>

app/components/editable_champ/repetition_row_component.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ def has_fieldset?
1818
private
1919

2020
def section_component
21-
EditableChamp::SectionComponent.new(dossier: @dossier, types_de_champ: @types_de_champ, row_id:, row_number: @row_number)
21+
EditableChamp::SectionComponent.new(dossier: @dossier, champs: row.champs_tree, row_number: @row_number)
22+
end
23+
24+
def row
25+
@row ||= @champ.rows.find { _1.id == row_id }
2226
end
2327

2428
def delete_button

app/components/editable_champ/section_component.rb

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,30 @@
22

33
class EditableChamp::SectionComponent < ApplicationComponent
44
include ApplicationHelper
5-
include TreeableConcern
65

7-
def initialize(dossier:, nodes: nil, types_de_champ: nil, row_id: nil, row_number: nil)
8-
nodes ||= to_tree(types_de_champ:)
9-
@dossier = dossier
10-
@row_id = row_id
11-
@row_number = row_number
12-
@nodes = to_fieldset(nodes:)
6+
def initialize(dossier:, champs:, header_section: nil, row_number: nil)
7+
@dossier, @champs, @header_section, @row_number = dossier, champs, header_section, row_number
138
end
149

15-
def render_within_fieldset?
16-
first_champ_is_an_header_section?
17-
end
10+
attr_reader :header_section
1811

19-
def header_section
20-
node = @nodes.first
21-
@dossier.project_champ(node, row_id: @row_id) if node.is_a?(TypeDeChamp) && node.header_section?
12+
def render_within_fieldset?
13+
header_section.present?
2214
end
2315

2416
def splitted_tail
25-
tail.map { split_section_champ(_1) }
26-
end
27-
28-
def tail
29-
return @nodes if !first_champ_is_an_header_section?
30-
_, *rest_of_champ = @nodes
31-
32-
rest_of_champ
17+
@champs.map { split_section_champ(_1) }
3318
end
3419

3520
def tag_for_depth
3621
"h#{header_section.level + 1}"
3722
end
3823

39-
def split_section_champ(node)
40-
case node
41-
when EditableChamp::SectionComponent
42-
[node, nil]
24+
def split_section_champ(champ)
25+
if champ.header_section?
26+
[EditableChamp::SectionComponent.new(dossier: @dossier, champs: champ.children, header_section: champ), nil]
4327
else
44-
[nil, @dossier.project_champ(node, row_id: @row_id)]
28+
[nil, champ]
4529
end
4630
end
47-
48-
private
49-
50-
def to_fieldset(nodes:)
51-
nodes.map { _1.is_a?(Array) ? EditableChamp::SectionComponent.new(dossier: @dossier, nodes: _1, row_id: @row_id) : _1 }
52-
end
53-
54-
def first_champ_is_an_header_section?
55-
header_section.present?
56-
end
5731
end

app/components/viewable_champ/section_component.rb

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,28 @@
22

33
class ViewableChamp::SectionComponent < ApplicationComponent
44
include ApplicationHelper
5-
include TreeableConcern
65

7-
def initialize(dossier:, nodes: nil, types_de_champ: nil, row_id: nil, demande_seen_at:, profile:)
8-
@dossier, @demande_seen_at, @profile, @row_id = dossier, demande_seen_at, profile, row_id
9-
nodes ||= to_tree(types_de_champ:)
10-
@nodes = to_sections(nodes:)
6+
def initialize(dossier:, champs:, header_section: nil, demande_seen_at:, profile:)
7+
@dossier, @champs, @header_section = dossier, champs, header_section
8+
@demande_seen_at, @profile = demande_seen_at, profile
119
end
1210

1311
private
1412

13+
attr_reader :header_section
14+
1515
def section_id
1616
@section_id ||= header_section ? dom_id(header_section, :content) : SecureRandom.uuid
1717
end
1818

19-
def header_section
20-
node = @nodes.first
21-
@dossier.project_champ(node, row_id: @row_id) if node.is_a?(TypeDeChamp) && node.header_section?
22-
end
23-
2419
def champs
25-
tail.filter_map { _1.is_a?(TypeDeChamp) ? @dossier.project_champ(_1, row_id: @row_id) : nil }
20+
@champs_without_sections ||= @champs.reject(&:header_section?)
2621
end
2722

2823
def sections
29-
tail.filter { _1.is_a?(ViewableChamp::SectionComponent) }
30-
end
31-
32-
def tail
33-
return @nodes if header_section.nil?
34-
_, *rest_of_champ = @nodes
35-
36-
rest_of_champ
24+
@sections ||= @champs.filter(&:header_section?).map do |champ|
25+
ViewableChamp::SectionComponent.new(dossier: @dossier, champs: champ.children, header_section: champ, demande_seen_at: @demande_seen_at, profile: @profile)
26+
end
3727
end
3828

3929
def reset_tag_for_depth
@@ -53,10 +43,4 @@ def repetition_heading_level
5343
# there are 2 levels of heading before the repetition heading
5444
[relative_level + 2, 6].min
5545
end
56-
57-
private
58-
59-
def to_sections(nodes:)
60-
nodes.map { _1.is_a?(Array) ? ViewableChamp::SectionComponent.new(dossier: @dossier, nodes: _1, demande_seen_at: @demande_seen_at, profile: @profile, row_id: @row_id) : _1 }
61-
end
6246
end

app/views/instructeurs/dossiers/print.html.haml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515

1616
%h2 Formulaire
1717

18-
- types_de_champ_public = @dossier.revision.types_de_champ_public
19-
- if types_de_champ_public.any? || @dossier.procedure.routing_enabled?
20-
= render ViewableChamp::SectionComponent.new(dossier: @dossier, types_de_champ: types_de_champ_public, demande_seen_at: nil, profile: 'instructeur')
18+
- if @dossier.revision.types_de_champ_public.any? || @dossier.procedure.routing_enabled?
19+
= render ViewableChamp::SectionComponent.new(dossier: @dossier, champs: @dossier.champs_public_tree, demande_seen_at: nil, profile: 'instructeur')
2120

2221
%h2 Annotations privées
2322

24-
- types_de_champ_private = @dossier.revision.types_de_champ_private
25-
- if types_de_champ_private.any?
26-
= render ViewableChamp::SectionComponent.new(dossier: @dossier, types_de_champ: types_de_champ_private, demande_seen_at: nil, profile: 'instructeur')
23+
- if @dossier.revision.types_de_champ_private.any?
24+
= render ViewableChamp::SectionComponent.new(dossier: @dossier, champs: @dossier.champs_private_tree, demande_seen_at: nil, profile: 'instructeur')
2725
- else
2826
Aucune annotation privée
2927

app/views/instructeurs/dossiers/show_submitted_revision.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
data-clipboard2-copy-text-value="<%= t('clipboard.copy') %>"
3535
data-clipboard2-copied-text-value="<%= t('clipboard.copied') %>"
3636
>
37-
<%= render ViewableChamp::SectionComponent.new(dossier: @dossier, types_de_champ: @dossier.revision.types_de_champ_public, demande_seen_at: @demande_seen_at, profile: 'instructeur') %>
37+
<%= render ViewableChamp::SectionComponent.new(dossier: @dossier, champs: @dossier.champs_public_tree, demande_seen_at: @demande_seen_at, profile: 'instructeur') %>
3838
</div>
3939
</div>
4040
</div>

app/views/root/patron.html.haml

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

3434
%form.form
3535
= form_for @dossier, url: '', html: { class: 'form' } do |f|
36-
= render EditableChamp::SectionComponent.new(dossier: @dossier, types_de_champ: @dossier.revision.types_de_champ_public)
36+
= render EditableChamp::SectionComponent.new(dossier: @dossier, champs: @dossier.champs_public_tree)
3737

3838
.editable-champ.editable-champ-text
3939
%label Mot de passe

app/views/shared/dossiers/_demande.html.erb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@
121121
<% end %>
122122
<% end %>
123123

124-
<% types_de_champ = dossier.revision.types_de_champ_public %>
125-
126-
<% if types_de_champ.any? || dossier.procedure.routing_enabled? %>
127-
<%= render ViewableChamp::SectionComponent.new(dossier:, types_de_champ:, demande_seen_at:, profile:) %>
124+
<% if dossier.revision.types_de_champ_public.any? || dossier.procedure.routing_enabled? %>
125+
<%= render ViewableChamp::SectionComponent.new(dossier:, champs: dossier.champs_public_tree, demande_seen_at:, profile:) %>
128126
<% end %>
129127
</div>

app/views/shared/dossiers/_edit.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<h2 class="fr-sr-only">Formulaire</h2>
5151

5252
<fieldset class="fr-fieldset">
53-
<%= render EditableChamp::SectionComponent.new(dossier:, types_de_champ: dossier.revision.types_de_champ_public) %>
53+
<%= render EditableChamp::SectionComponent.new(dossier:, champs: dossier.champs_public_tree) %>
5454
</fieldset>
5555

5656
<%= render Dossiers::PendingCorrectionCheckboxComponent.new(dossier:) %>

app/views/shared/dossiers/_edit_annotations.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<%= form_for dossier, url: annotations_instructeur_dossier_path(dossier.procedure, dossier), html: { class: 'form', multipart: true } do |f| %>
77
<fieldset class="fr-fieldset">
8-
<%= render EditableChamp::SectionComponent.new(dossier:, types_de_champ: dossier.revision.types_de_champ_private) %>
8+
<%= render EditableChamp::SectionComponent.new(dossier:, champs: dossier.champs_private_tree) %>
99
</fieldset>
1010
<% end %>
1111

0 commit comments

Comments
 (0)