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:

The following class methods are added

Public Instance Methods

acts_as_tree_with_dotted_ids(options = {}, &b) click to toggle source

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 a children_count column if set to true (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
rebuild_dotted_ids!() click to toggle source

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
traverse(nodes = nil) { |node| ... } click to toggle source

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