9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/parentry/class_methods.rb', line 9
def arrange(options = {})
scope =
if (order = options.delete(:order))
self.base_class.order_by_parentry.order(order)
else
self.base_class.order_by_parentry
end
scope.where(options).each_with_object(Hash.new { |h, k| h[k] = {} }) do |node, memo|
insert_node = node.ancestor_ids.reduce(memo) do |subtree, ancestor_id|
match = subtree.find { |parent, _children| parent.id == ancestor_id }
match ? match[1] : subtree
end
insert_node[node] = {}
end
end
|