A tree structure for Mongoid documents using the materialized path pattern
mongoid (>= 2.0.0.beta.14)
To install mongoid_tree, simply add it to your Gemfile:
gem'mongoid-tree'
In order to get the latest development version of mongoid-tree:
gem'mongoid-tree', :git=>'git://github.com/benedikt/mongoid-tree'
You might want to add the :require => 'mongoid/tree' option as well and finally run
bundleinstall
Read the API documentation at benedikt.github.com/mongoid-tree and take a look at the Mongoid::Tree module
require'mongoid/tree'classNodeincludeMongoid::DocumentincludeMongoid::Treeend
There are several utility methods that help getting to other related documents in the tree:
Node.rootNode.rootsNode.leavesnode.rootnode.parentnode.childrennode.ancestorsnode.ancestors_and_selfnode.descendantsnode.descendants_and_selfnode.siblingsnode.siblings_and_selfnode.leaves
In addition it’s possible to check certain aspects of the documents position in the tree:
node.root?node.leaf?node.depthnode.ancestor_of?(other) node.descendant_of?(other)
See Mongoid::Tree for more information on these methods.
It’s possible to traverse the tree using different traversal methods. See Mongoid::Tree::Traversal for details
node.traverse(:breadth_first) do|n|# Do something with Node nend
There are two callbacks that are called before and after the rearranging process. This enables you to do additional computations after the documents position in the tree is updated. See Mongoid::Tree for details.
See github.com/benedikt/mongoid-tree/issues
See github.com/benedikt/mongoid-tree and feel free to fork it!
Copyright © 2010 Benedikt Deicke. See LICENSE for details.