demarches-normaliennes/app/models/concerns/treeable_concern.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

37 lines
1.2 KiB
Ruby

module TreeableConcern
extend ActiveSupport::Concern
MAX_DEPTH = 6 # deepest level for header_sections is 3.
# but a repetition can be nested an header_section, so 3+3=6=MAX_DEPTH
included do
# as we progress in the list of ordered champs
# we keep a reference to each level of nesting (walk)
# when we encounter an header_section, it depends of its own depth of nesting minus 1, ie:
# h1 belongs to prior (rooted_tree)
# h2 belongs to prior h1
# h3 belongs to prior h2
# h1 belongs to prior (rooted_tree)
# then, each and every champs which are not an header_section
# are added to the current_tree
# given a root_depth at 0, we build a full tree
# given a root_depth > 0, we build a partial tree (aka, a repetition)
def to_tree(champs:)
rooted_tree = []
walk = Array.new(MAX_DEPTH)
walk[0] = rooted_tree
current_tree = rooted_tree
champs.each do |champ|
if champ.header_section?
new_tree = [champ]
walk[champ.header_section_level_value - 1].push(new_tree)
current_tree = walk[champ.header_section_level_value] = new_tree
else
current_tree.push(champ)
end
end
rooted_tree
end
end
end