Module: ClosureTree::NumericDeterministicOrdering::ClassMethods

Defined in:
lib/closure_tree/numeric_deterministic_ordering.rb

Instance Method Summary collapse

Instance Method Details

#roots_and_descendants_preorderedObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/closure_tree/numeric_deterministic_ordering.rb', line 30

def roots_and_descendants_preordered
  h = _ct.connection.select_one("    SELECT\n      count(*) as total_descendants,\n      max(generations) as max_depth\n    FROM \#{_ct.quoted_hierarchy_table_name}\n  SQL\n  join_sql = <<-SQL\n    JOIN \#{_ct.quoted_hierarchy_table_name} anc_hier\n      ON anc_hier.descendant_id = \#{_ct.quoted_table_name}.id\n    JOIN \#{_ct.quoted_table_name} anc\n      ON anc.id = anc_hier.ancestor_id\n    JOIN (\n      SELECT descendant_id, max(generations) AS max_depth\n      FROM \#{_ct.quoted_hierarchy_table_name}\n      GROUP BY 1\n    ) AS depths ON depths.descendant_id = anc.id\n  SQL\n  node_score = \"(1 + anc.\#{_ct.quoted_order_column(false)}) * \" +\n    \"power(\#{h['total_descendants']}, \#{h['max_depth'].to_i + 1} - depths.max_depth)\"\n  order_by = \"sum(\#{node_score})\"\n  joins(join_sql).group(\"\#{_ct.quoted_table_name}.id\").reorder(order_by)\nend\n")