Module: HasHierarchy::ClassMethods
- Defined in:
- lib/has_hierarchy.rb
Instance Method Summary collapse
Instance Method Details
#flat_tree(tree_hash = nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/has_hierarchy.rb', line 61 def flat_tree(tree_hash = nil) tree_hash ||= tree list = [] tree_hash.each do |node, children| list << node list += flat_tree(children) unless children.empty? end list end |
#roots ⇒ Object
46 47 48 |
# File 'lib/has_hierarchy.rb', line 46 def roots where(parent_id: nil) end |
#tree ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/has_hierarchy.rb', line 50 def tree nodes = all tree_hash = {} index = {} nodes.each{ |n| index[n.id] = {} } nodes.each{ |n| (index[n.parent_id] || tree_hash)[n] = index[n.id] } tree_hash end |