From d32eb3f3d98f2095cb9e6584a13eabd050783ae6 Mon Sep 17 00:00:00 2001 From: Trafium Date: Tue, 10 Oct 2017 11:05:54 +0300 Subject: [PATCH] Fix arrange method dropping nodes without parents Addresses https://github.com/stefankroes/ancestry/issues/362 --- lib/ancestry/class_methods.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/ancestry/class_methods.rb b/lib/ancestry/class_methods.rb index c8f92fa2..b1967840 100644 --- a/lib/ancestry/class_methods.rb +++ b/lib/ancestry/class_methods.rb @@ -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