Skip to content

Commit

Permalink
Add option on how to handle orphans when arranging nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Mar 29, 2023
1 parent 8f06902 commit 0fcf74e
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/ancestry/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 0fcf74e

Please sign in to comment.