Module: ActiveRecord::Acts::TreeWithDottedIds::ClassMethods

Defined in:
lib/active_record/acts/tree_with_dotted_ids.rb

Overview

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 on subchild1)

  • self_and_siblings - Returns all the children of the parent, including the current node ([subchild1, subchild2] when called on subchild1)

  • ancestors - Returns all the ancestors of the current node ([child1, root] when called on subchild2)

  • self_and_ancestors - Returns all the ancestors of the current node ([subchild2, child1, root] when called on subchild2)

  • root - Returns the root of the current node (root when called on subchild2)

  • 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 existing acts_as_tree model to acts_as_tree_with_dotted_ids

Instance Method Summary collapse

Instance Method Details

#acts_as_tree_with_dotted_ids(options = {}, &b) ⇒ Object

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).



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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 "    include ActiveRecord::Acts::TreeWithDottedIds::InstanceMethods\n\n    def self.roots\n      res = where(\"\#{configuration[:foreign_key]} IS NULL\").order(\#{configuration[:order].nil? ? \"nil\" : %Q{\"\#{configuration[:order]}\"}})\n\n    end\n\n    def self.root\n      where(\"\#{configuration[:foreign_key]} IS NULL\").order(\#{configuration[:order].nil? ? \"nil\" : %Q{\"\#{configuration[:order]}\"}}).first\n    end\n\n    def parent_foreign_key_changed?\n      \#{configuration[:foreign_key]}_changed?\n    end\n\n  EOV\nend\n"

#rebuild_dotted_ids!Object

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.



92
93
94
95
96
# 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, &block) ⇒ Object

Performs a depth-first traversal of the tree, yielding each node to the given block



82
83
84
85
86
87
88
# 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