Module: LcpRuby::TreeHelper

Defined in:
app/helpers/lcp_ruby/tree_helper.rb

Instance Method Summary collapse

Instance Method Details

#render_tree_rows(roots, children_map, columns, depth:, default_expanded:, match_ids:, reparentable: false, search_active: false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/lcp_ruby/tree_helper.rb', line 3

def render_tree_rows(roots, children_map, columns, depth:, default_expanded:, match_ids:,
                     reparentable: false, search_active: false)
  return "".html_safe if roots.blank?

  # Pre-compute terminal field_def + owning model_name per column once,
  # then pass through recursive calls. Avoids O(rows × cols × fields)
  # linear scans on deep trees (the inline lookup in render_tree_row used
  # to do this per (row × column) without memoization).
  @_tree_col_field_defs ||= {}
  @_tree_col_model_names ||= {}
  columns.each do |col|
    path = col["field"]
    next unless path
    next if @_tree_col_field_defs.key?(path)
    fd, mn = LcpRuby::Metadata::FieldPath.terminal(current_model_definition, path, through_collections: true)
    @_tree_col_field_defs[path] = fd
    @_tree_col_model_names[path] = mn || current_model_name
  end

  roots.each_with_index.map { |record, idx|
    render_tree_row(record, children_map, columns, depth: depth,
      default_expanded: default_expanded, match_ids: match_ids,
      reparentable: reparentable, search_active: search_active,
      guides: [], is_last: idx == roots.size - 1)
  }.join.html_safe
end