Skip to content

Commit

Permalink
Fix arrange method dropping nodes without parents
Browse files Browse the repository at this point in the history
Addresses #362
  • Loading branch information
trafium authored and kbrock committed Nov 2, 2018
1 parent c5e2e48 commit d32eb3f
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/ancestry/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,17 @@ def arrange options = {}
# {node => children}, where children = {} if the node has no children
def arrange_nodes(nodes)
arranged = ActiveSupport::OrderedHash.new
min_depth = Float::INFINITY
nodes_ids = nodes.map(&:id)
min_depth = nodes.map(&:depth).min
index = Hash.new { |h, k| h[k] = ActiveSupport::OrderedHash.new }

nodes.each do |node|
children = index[node.id]
index[node.parent_id][node] = children

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

arranged
Expand Down

0 comments on commit d32eb3f

Please sign in to comment.