demarches-normaliennes/app/components/editable_champ/section_component.rb
simon lehericey e64ac79f05 tech(refactor): much nicer code, thx LeSim
root -> rooted_tree

depth_cach -> walk

smal refactor

comment

remove form for header_section

remove seen_at from header section

header_section: champ -> header_section

champ_subree: remove if not

remove root_depth use header_section_level_value instead

remove unused include

remove ChampTreeComponent

rename ChampsSubtreeComponent to SectionComponent

use TreeableConcern only in section component

remove fields_for_champ_component

champs -> tail

add split_section_champ helper

refactor(editable_champ::header_section): keep same interface everywhere

fix(repetition): add spec for SectionComponent on repetitions
2023-04-20 08:22:02 +02:00

64 lines
1.3 KiB
Ruby

class EditableChamp::SectionComponent < ApplicationComponent
include ApplicationHelper
include TreeableConcern
def initialize(nodes: nil, champs: nil)
if (nodes.nil?)
nodes = to_tree(champs:)
end
@nodes = to_fieldset(nodes:)
end
def render_within_fieldset?
first_champ_is_an_header_section? && any_champ_fillable?
end
def header_section
return @nodes.first if @nodes.first.is_a?(Champs::HeaderSectionChamp)
end
def splitted_tail
tail.map { split_section_champ(_1) }
end
def tail
return @nodes if !first_champ_is_an_header_section?
_, *rest_of_champ = @nodes
rest_of_champ
end
def tag_for_depth
"h#{header_section.level + 1}"
end
# if two headers follows each others [h1, [h2, c]]
# the first one must not be contained in fieldset
# so we make the tree not fillable
def fillable?
false
end
def split_section_champ(node)
case node
when EditableChamp::SectionComponent
[node, nil]
else
[nil, node]
end
end
private
def to_fieldset(nodes:)
nodes.map { _1.is_a?(Array) ? EditableChamp::SectionComponent.new(nodes: _1) : _1 }
end
def first_champ_is_an_header_section?
header_section.present?
end
def any_champ_fillable?
tail.any? { _1&.fillable? }
end
end