Skip to content

A drop in replacement for acts_as_tree with super fast ancestors and subtree access

Notifications You must be signed in to change notification settings

metasoarous/forest_for_the_trees

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ForestForTheTrees

ForestForTheTrees is a gem designed for making tree and forest relationships easier to deal with. Situations where some parent model has associated with it tree nodes come up quite frequently (such as sections within a document, or nested tasks within a project), and such situations tend to beg for the same methods and helpers. Some of these fall on the side of the tree while others fall on the side of the forest. While other plugins seek to address the tree side of this issue, this plugin addresses both.

A Little History

This gem is a fork of a fork - it is based off of the good old acts_as_tree gem and the extensions made to it and rereleased under the name acts_as_tree_with_dotted_ids.

The second gem mentioned is designed to fix performance issues with the first by adding a column which stores the path of the node as a string of record IDs joined by dots, hence the name. This optimization solves performance issues related to in-database tree structure by allowing for direct O(1) ancestor/child verification and O(N) subtree access with one single query.

In yer classes

class TreeClass < ActiveRecord::Base
  acts_as_forest_tree :order => "name" #Order param optional
end

class ForestClass < ActiveRecord::Base acts_as_forest_floor :tree_class end

The rest of this doc is pending development and is basically still straight from the docs for acts_as_tree_with_dotted_ids.

How to use

root
 \_ child1
      \_ subchild1
      \_ subchild2

Usage:

root      = Category.create("name" => "root")
child1    = root.children.create("name" => "child1")
subchild1 = child1.children.create("name" => "subchild1")

root.parent   # => nil
child1.parent # => root
root.children # => [child1]
root.children.first.children.first # => subchild1
child1.ancestors_of?(subchild2)    # => true
subchild1.descendant_of?(root)     # => true

root.id              # 1
child1.id            # 2
subchild1.id         # 3
root.dotted_ids      # "1"
child1.dotted_ids    # "1.2"
subchild1.dotted_ids # "1.2.3"

Improvements

The plugin adds the following instance methods:

  • ancestor_of?(node)

  • self_and_ancestors

  • descendant_of?(node)

  • all_children

  • depth

The following methods of have been rewritten to take advantage of the dotted IDs:

  • root

  • ancestors

  • siblings

  • self_and_sibblings

Migration

If you already have an acts_as_tree model, you can easily upgrade it to take advantage of the dotted IDs.

  1. Just add the dotted_ids column to your table. In most case a string should be enough (it’s also better for the indexing) but if your tree is very deep you may want to use a text column.

  2. Call MyTreeModel.rebuild_dotted_ids! and you are ready to go.

Compatibility

Base acts_as_tree ested with Rails 2.x and MySQL 5.x as well as SQLite

TODO

  • Make it so that the foreign key is flexible in definition of roots for the ForestFloor inheriting class.

Thanks

Kudos to all the contributors to the original plugin.

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

Copyright © 2008 Xavier Defrang, released under the MIT license

Copyright © 2010 Christopher Small, released under the MIT license

About

A drop in replacement for acts_as_tree with super fast ancestors and subtree access

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%