module ActiveRecord::Acts::TreeWithDottedIds::ClassMethods
Specify this acts_as
extension if you want to model a tree structure by providing a parent association and a children association. This requires that you have a foreign key column, which by default is called parent_id
and a string or text column called dotted_ids
which will be used to store the path to each node in the tree.
class Category < ActiveRecord::Base acts_as_tree_with_dotted_ids :order => "name" end Example: root \_ child1 \_ subchild1 \_ subchild2 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
In addition to the parent and children associations, the following instance methods are added to the class after calling acts_as_tree_with_dotted_ids
:
-
siblings
- Returns all the children of the parent, excluding the current node ([subchild2]
when called onsubchild1
) -
self_and_siblings
- Returns all the children of the parent, including the current node ([subchild1, subchild2]
when called onsubchild1
) -
ancestors
- Returns all the ancestors of the current node ([child1, root]
when called onsubchild2
) -
self_and_ancestors
- Returns all the ancestors of the current node ([subchild2, child1, root]
when called onsubchild2
) -
root
- Returns the root of the current node (root
when called onsubchild2
) -
depth
- Returns the depth of the current node starting from 0 as the depth of root nodes.
The following class methods are added
-
traverse
- depth-first traversal of the tree (warning: it does not rely on the dotted_ids as it is used to rebuild the tree) -
rebuild_dotted_ids!
- rebuilt the dotted IDs for the whole tree, use this once to migrate an existingacts_as_tree
model toacts_as_tree_with_dotted_ids
Public Instance Methods
Configuration options are:
-
foreign_key
- specifies the column name to use for tracking of the tree (default:parent_id
) -
order
- makes it possible to sort the children according to this SQL snippet. -
counter_cache
- keeps a count in achildren_count
column if set totrue
(default:false
).
# File lib/active_record/acts/tree_with_dotted_ids.rb, line 49 def acts_as_tree_with_dotted_ids(options = {}, &b) configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil } configuration.update(options) if options.is_a?(Hash) belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache] has_many :children, -> { order(configuration[:order]) }, :class_name => name, :foreign_key => configuration[:foreign_key], :dependent => :destroy, &b after_save :assign_dotted_ids after_validation :update_dotted_ids, :on => :update class_eval <<-EOV include ActiveRecord::Acts::TreeWithDottedIds::InstanceMethods def self.roots res = where("#{configuration[:foreign_key]} IS NULL").order(#{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}) end def self.root where("#{configuration[:foreign_key]} IS NULL").order(#{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}).first end def parent_foreign_key_changed? #{configuration[:foreign_key]}_changed? end EOV end
Traverse the whole tree from roots to leaves and rebuild the dotted_ids path Call it from your migration to upgrade an existing acts_as_tree model.
# File lib/active_record/acts/tree_with_dotted_ids.rb, line 92 def rebuild_dotted_ids! transaction do traverse { |node| node.dotted_ids = nil; node.save! } end end
Performs a depth-first traversal of the tree, yielding each node to the given block
# File lib/active_record/acts/tree_with_dotted_ids.rb, line 82 def traverse(nodes = nil, &block) nodes ||= self.roots nodes.each do |node| yield node traverse(node.children, &block) end end