From 0fcf74e1c5e5f5321971ef32386fb56148787e7d Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Tue, 28 Mar 2023 22:21:17 -0400 Subject: [PATCH] Add option on how to handle orphans when arranging nodes --- lib/ancestry/class_methods.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ancestry/class_methods.rb b/lib/ancestry/class_methods.rb index cf41c9e1..6074c37c 100644 --- a/lib/ancestry/class_methods.rb +++ b/lib/ancestry/class_methods.rb @@ -40,14 +40,14 @@ def arrange options = {} # @param nodes [Array[Node]] nodes to be arranged # @returns Hash{Node => {Node => {}, Node => {}}} # 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) + def arrange_nodes(nodes, add_orphans: true) node_ids = Set.new(nodes.map(&:id)) index = Hash.new { |h, k| h[k] = {} } nodes.each_with_object({}) do |node, arranged| children = index[node.id] index[node.parent_id][node] = children - arranged[node] = children unless node_ids.include?(node.parent_id) + arranged[node] = children if node.parent_id.nil? || (add_orphans && !node_ids.include?(node.parent_id)) end end