Module: ClosureTree::NumericDeterministicOrdering::ClassMethods
- Defined in:
- lib/closure_tree/numeric_deterministic_ordering.rb
Instance Method Summary collapse
-
#_ct_sum_order_by(node = nil) ⇒ Object
If node is nil, order the whole tree.
- #roots_and_descendants_preordered ⇒ Object
Instance Method Details
#_ct_sum_order_by(node = nil) ⇒ Object
If node is nil, order the whole tree.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/closure_tree/numeric_deterministic_ordering.rb', line 48 def _ct_sum_order_by(node = nil) stats_sql = <<-SQL.strip_heredoc SELECT count(*) as total_descendants, max(generations) as max_depth FROM #{_ct.quoted_hierarchy_table_name} SQL stats_sql += " WHERE ancestor_id = #{_ct.quote(node.id)}" if node h = _ct.connection.select_one(stats_sql) depth_column = node ? 'depths.generations' : 'depths.max_depth' node_score = "(1 + anc.#{_ct.quoted_order_column(false)}) * " + "power(#{h['total_descendants']}, #{h['max_depth'].to_i + 1} - #{depth_column})" "sum(#{node_score})" end |
#roots_and_descendants_preordered ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/closure_tree/numeric_deterministic_ordering.rb', line 66 def roots_and_descendants_preordered join_sql = <<-SQL.strip_heredoc JOIN #{_ct.quoted_hierarchy_table_name} anc_hier ON anc_hier.descendant_id = #{_ct.quoted_table_name}.#{_ct.quoted_id_column_name} JOIN #{_ct.quoted_table_name} anc ON anc.#{_ct.quoted_id_column_name} = anc_hier.ancestor_id JOIN ( SELECT descendant_id, max(generations) AS max_depth FROM #{_ct.quoted_hierarchy_table_name} GROUP BY descendant_id ) AS depths ON depths.descendant_id = anc.#{_ct.quoted_id_column_name} SQL joins(join_sql) .group("#{_ct.quoted_table_name}.#{_ct.quoted_id_column_name}") .reorder(_ct_sum_order_by) end |