Skip to content

Commit

Permalink
Condense arrange_nodes
Browse files Browse the repository at this point in the history
calculating the depths is not necessary.
Just checking parent nodes works well enough

OrderedHash are no longer necessary
  • Loading branch information
kbrock committed Nov 2, 2018
1 parent d32eb3f commit 64ebdd7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
16 changes: 5 additions & 11 deletions lib/ancestry/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,16 @@ def arrange options = {}

# Arrange array of nodes into a nested hash of the form
# {node => children}, where children = {} if the node has no children
# If a node's parent is not included, the node will be included as if it is a top level node
def arrange_nodes(nodes)
arranged = ActiveSupport::OrderedHash.new
nodes_ids = nodes.map(&:id)
min_depth = nodes.map(&:depth).min
index = Hash.new { |h, k| h[k] = ActiveSupport::OrderedHash.new }
node_ids = Set.new(nodes.map(&:id))
index = Hash.new { |h, k| h[k] = {} }

nodes.each do |node|
nodes.each_with_object({}) do |node, arranged|
children = index[node.id]
index[node.parent_id][node] = children

if node.depth == min_depth || !nodes_ids.include?(node.parent_id)
arranged[node] = children
end
arranged[node] = children unless node_ids.include?(node.parent_id)
end

arranged
end

# Arrangement to nested array
Expand Down
3 changes: 1 addition & 2 deletions test/concerns/arrangement_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ def test_arrangement_nesting
end
end

# TODO: don't drop parentless nodes
def xtest_arrange_partial
def test_arrange_partial
AncestryTestDatabase.with_model do |model|
# - n1
# - n2
Expand Down
9 changes: 3 additions & 6 deletions test/concerns/sort_by_ancestry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def test_sort_by_ancestry_no_parents_siblings
end
end

# TODO: don't drop parentless nodes
def xtest_sort_by_ancestry_no_parents_same_level
def test_sort_by_ancestry_no_parents_same_level
AncestryTestDatabase.with_model do |model|
n1, n2, n3, n4, n5, n6 = build_tree(model)

Expand All @@ -58,8 +57,7 @@ def test_sort_by_ancestry_partial_tree
end
end

# TODO: don't drop parentless nodes
def xtest_sort_by_ancestry_missing_parent_middle_of_tree
def test_sort_by_ancestry_missing_parent_middle_of_tree
AncestryTestDatabase.with_model do |model|
n1, n2, n3, n4, n5, n6 = build_tree(model)

Expand Down Expand Up @@ -128,7 +126,6 @@ def test_sort_by_ancestry_with_block_no_parents_all_children
end
end

# TODO: don't drop parentless nodes
# TODO: nodes need to follow original ordering
# NOTE: even for partial trees, if the input records are ranked, the output works
def xtest_sort_by_ancestry_with_sql_sort_paginated_missing_parents_and_children
Expand All @@ -139,7 +136,7 @@ def xtest_sort_by_ancestry_with_sql_sort_paginated_missing_parents_and_children
end
end

# TODO: don't drop parentless nodes
# TODO: find a way to rank missing nodes
def xtest_sort_by_ancestry_with_block_paginated_missing_parents_and_children
AncestryTestDatabase.with_model :extra_columns => {:rank => :integer} do |model|
n1, n2, n3, n4, n5, n6 = build_ranked_tree(model)
Expand Down

0 comments on commit 64ebdd7

Please sign in to comment.