acts_as_sane_tree

(Building trees with a dash of sanity)

This is a drop in replacement for acts_as_tree on systems with Postgresql >= 8.4

What this provides

A fast way to build trees.

Requirements

  • PostgreSQL version >= 8.4

  • ActiveRecord

Configuration

Same as acts_as_tree. Basically: Specify a parent_id or the column that holds the parent ID information.

Extras

A few extras are provided. Of note are:

  • #depth -> depth from root of the current node

  • #descendents -> all descendents of the current node (provided in either flat array or nested hash)

  • nodes_within? -> true if any of the nodes provided in the first parameter are found within the selfs or descendents of the second parameter

  • nodes_within -> same as #nodes_within? but returns array of found nodes

Thanks

  • Thanks to David Hansson for the original

  • Thanks to PostgreSQL for providing tools for sanity

License

A large majority of this was copied directly from the original acts as tree, with the original license:

  • Copyright © 2007 David Heinemeier Hansson, released under the MIT license

The new additions continue:

  • Copyright © 2010 Chris Roberts, released under the MIT license