Skip to content

Commit 81b0114

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 ce32f03 commit 81b0114

17 files changed

Lines changed: 48 additions & 100 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_component/repetition_component.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
id="<%= dom_id(@champ, :rows) %>"
4747
data-repetition-toggle-all-target="rowsContainer"
4848
>
49-
<% @champ.row_ids.each.with_index(1) do |row_id, row_number| %>
50-
<%= render EditableChamp::RepetitionRowComponent.new(form: @form, dossier: @champ.dossier, champ: @champ, row_id: row_id, row_number: row_number, seen_at: @seen_at, expanded: expand_row?(row_id, @champ.row_ids.count)) %>
49+
<% @champ.rows.each do |row| %>
50+
<%= render EditableChamp::RepetitionRowComponent.new(form: @form, dossier: @champ.dossier, champ: @champ, row:, seen_at: @seen_at, expanded: expand_row?(row.id, @champ.row_ids.count)) %>
5151
<% end %>
5252
</div>
5353

app/components/editable_champ/repetition_row_component.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
class EditableChamp::RepetitionRowComponent < ApplicationComponent
44
include ChampAriaLabelledbyHelper
55

6-
def initialize(form:, dossier:, champ:, row_id:, row_number:, expanded: false, seen_at: nil)
7-
@form, @dossier, @champ, @row_id, @row_number, @expanded, @seen_at = form, dossier, champ, row_id, row_number, expanded, seen_at
6+
def initialize(form:, dossier:, champ:, row:, expanded: false, seen_at: nil)
7+
@form, @dossier, @champ, @row, @expanded, @seen_at = form, dossier, champ, row, expanded, seen_at
88
@type_de_champ = champ.type_de_champ
99
@types_de_champ = dossier.revision.children_of(@type_de_champ)
1010
end
1111

12-
attr_reader :row_id, :row_number
12+
def row_id = @row.id
13+
def row_number = @row.index
1314

1415
def has_fieldset?
1516
@types_de_champ.size > 1
@@ -18,7 +19,7 @@ def has_fieldset?
1819
private
1920

2021
def section_component
21-
EditableChamp::SectionComponent.new(dossier: @dossier, types_de_champ: @types_de_champ, row_id:, row_number: @row_number)
22+
EditableChamp::SectionComponent.new(dossier: @dossier, champs: @row.champs_tree, row_number:)
2223
end
2324

2425
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(it) }
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 & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,25 @@
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, @header_section = dossier, header_section
8+
@demande_seen_at, @profile = demande_seen_at, profile
9+
@section_champs, @champs = champs.partition(&:header_section?)
1110
end
1211

1312
private
1413

14+
attr_reader :header_section, :champs
15+
1516
def section_id
1617
@section_id ||= header_section ? dom_id(header_section, :content) : SecureRandom.uuid
1718
end
1819

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-
24-
def champs
25-
tail.filter_map { _1.is_a?(TypeDeChamp) ? @dossier.project_champ(_1, row_id: @row_id) : nil }
26-
end
27-
2820
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
21+
@sections ||= @section_champs.map do |champ|
22+
ViewableChamp::SectionComponent.new(dossier: @dossier, champs: champ.children, header_section: champ, demande_seen_at: @demande_seen_at, profile: @profile)
23+
end
3724
end
3825

3926
def reset_tag_for_depth
@@ -53,10 +40,4 @@ def repetition_heading_level
5340
# there are 2 levels of heading before the repetition heading
5441
[relative_level + 2, 6].min
5542
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
6243
end

app/controllers/champs/repetition_controller.rb

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

33
class Champs::RepetitionController < Champs::ChampController
44
def add
5-
@row_id = @champ.add_row(updated_by: current_user.email)
5+
row_id = @champ.add_row(updated_by: current_user.email)
6+
@row = @champ.rows.find { it.id == row_id }
67
@first_champ_id = @champ.focusable_input_id
7-
@row_number = @row_id.nil? ? 0 : @champ.row_ids.find_index(@row_id) + 1
88
@champ.update_timestamps
99
end
1010

app/views/champs/repetition/add.turbo_stream.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<% if @row_id.present? %>
1+
<% if @row.present? %>
22
<%= fields_for @champ.input_name, @champ do |form| %>
3-
<%= turbo_stream.append dom_id(@champ, :rows), render(EditableChamp::RepetitionRowComponent.new(form: form, dossier: @champ.dossier, champ: @champ, row_id: @row_id, row_number: @row_number, expanded: true)) %>
3+
<%= turbo_stream.append dom_id(@champ, :rows), render(EditableChamp::RepetitionRowComponent.new(form: form, dossier: @champ.dossier, champ: @champ, row: @row, expanded: true)) %>
44

55
<% if @first_champ_id %>
66
<%= turbo_stream.focus(@first_champ_id, delay: 50) %>

app/views/champs/repetition/remove.turbo_stream.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<% end %>
44

55
<%= turbo_stream.update dom_id(@champ, :rows) do %>
6-
<% @champ.row_ids.each.with_index(1) do |row_id, row_number| %>
6+
<% @champ.rows.each do |row| %>
77
<%= fields_for @champ.input_name, @champ do |form| %>
8-
<%= render EditableChamp::RepetitionRowComponent.new(form: form, dossier: @champ.dossier, champ: @champ, row_id: row_id, row_number: row_number, seen_at: nil, expanded: row_number == 1) %>
8+
<%= render EditableChamp::RepetitionRowComponent.new(form: form, dossier: @champ.dossier, champ: @champ, row:, seen_at: nil, expanded: row.index == 1) %>
99
<% end %>
1010
<% end %>
1111
<% 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>

0 commit comments

Comments
 (0)