Module SymetrieCom::Acts::NestedSet::ClassMethods
In: lib/better_nested_set.rb

better_nested_set ehances the core nested_set tree functionality provided in ruby_on_rails.

This acts provides Nested Set functionality. Nested Set is a smart way to implement an ordered tree, with the added feature that you can select the children and all of their descendents with a single query. The drawback is that insertion or move need some complex sql queries. But everything is done here by this module!

Nested sets are appropriate each time you want either an orderd tree (menus, commercial categories) or an efficient way of querying big trees (threaded posts).

API

Methods names are aligned on Tree’s ones as much as possible, to make replacment from one by another easier, except for the creation:

in acts_as_tree:

  item.children.create(:name => "child1")

in acts_as_nested_set:

  # adds a new item at the "end" of the tree, i.e. with child.left = max(tree.right)+1
  child = MyClass.new(:name => "child1")
  child.save
  # now move the item to its right place
  child.move_to_child_of my_item

You can use:

  • move_to_child_of
  • move_to_right_of
  • move_to_left_of

and pass them an id or an object.

Other methods added by this mixin are:

  • root - root item of the tree (the one that has a nil parent; should have left_column = 1 too)
  • roots - root items, in case of multiple roots (the ones that have a nil parent)
  • level - number indicating the level, a root being level 0
  • ancestors - array of all parents, with root as first item
  • self_and_ancestors - array of all parents and self
  • siblings - array of all siblings, that are the items sharing the same parent and level
  • self_and_siblings - array of itself and all siblings
  • children_count - count of all immediate children
  • children - array of all immediate childrens
  • all_children - array of all children and nested children
  • full_set - array of itself and all children and nested children

These should not be useful, except if you want to write direct SQL:

  • left_col_name - name of the left column passed on the declaration line
  • right_col_name - name of the right column passed on the declaration line
  • parent_col_name - name of the parent column passed on the declaration line

recommandations: Don‘t name your left and right columns ‘left’ and ‘right’: these names are reserved on most of dbs. Usage is to name them ‘lft’ and ‘rgt’ for instance.

Methods

Included Modules

SymetrieCom::Acts::NestedSet::InstanceMethods

Public Instance methods

Configuration options are:

  • parent_column - specifies the column name to use for keeping the position integer (default: parent_id)
  • left_column - column name for left boundry data, default "lft"
  • right_column - column name for right boundry data, default "rgt"
  • text_column - column name for the title field (optional). Used as default in the {your-class}_options_for_select helper method. If empty, will use the first string field of your model class.
  • scope - restricts what is to be considered a list. Given a symbol, it’ll attach "_id" (if that hasn’t been already) and use that as the foreign key restriction. It’s also possible to give it an entire string that is interpolated if you need a tighter scope than just a foreign key. Example: acts_as_list :scope => ‘todo_list_id = #{todo_list_id} AND completed = 0’

Returns the single root

Returns roots when multiple roots (or virtual root, which is the same)

[Validate]