Chris@1296: module ActiveRecord Chris@1296: module Acts Chris@1296: module Tree Chris@1296: def self.included(base) Chris@1296: base.extend(ClassMethods) Chris@1296: end Chris@1296: Chris@1296: # Specify this +acts_as+ extension if you want to model a tree structure by providing a parent association and a children Chris@1296: # association. This requires that you have a foreign key column, which by default is called +parent_id+. Chris@1296: # Chris@1296: # class Category < ActiveRecord::Base Chris@1296: # acts_as_tree :order => "name" Chris@1296: # end Chris@1296: # Chris@1296: # Example: Chris@1296: # root Chris@1296: # \_ child1 Chris@1296: # \_ subchild1 Chris@1296: # \_ subchild2 Chris@1296: # Chris@1296: # root = Category.create("name" => "root") Chris@1296: # child1 = root.children.create("name" => "child1") Chris@1296: # subchild1 = child1.children.create("name" => "subchild1") Chris@1296: # Chris@1296: # root.parent # => nil Chris@1296: # child1.parent # => root Chris@1296: # root.children # => [child1] Chris@1296: # root.children.first.children.first # => subchild1 Chris@1296: # Chris@1296: # In addition to the parent and children associations, the following instance methods are added to the class Chris@1296: # after calling acts_as_tree: Chris@1296: # * siblings - Returns all the children of the parent, excluding the current node ([subchild2] when called on subchild1) Chris@1296: # * self_and_siblings - Returns all the children of the parent, including the current node ([subchild1, subchild2] when called on subchild1) Chris@1296: # * ancestors - Returns all the ancestors of the current node ([child1, root] when called on subchild2) Chris@1296: # * root - Returns the root of the current node (root when called on subchild2) Chris@1296: module ClassMethods Chris@1296: # Configuration options are: Chris@1296: # Chris@1296: # * foreign_key - specifies the column name to use for tracking of the tree (default: +parent_id+) Chris@1296: # * order - makes it possible to sort the children according to this SQL snippet. Chris@1296: # * counter_cache - keeps a count in a +children_count+ column if set to +true+ (default: +false+). Chris@1296: def acts_as_tree(options = {}) Chris@1296: configuration = { :foreign_key => "parent_id", :dependent => :destroy, :order => nil, :counter_cache => nil } Chris@1296: configuration.update(options) if options.is_a?(Hash) Chris@1296: Chris@1296: belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache] Chris@1296: has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => configuration[:dependent] Chris@1296: Chris@1296: class_eval <<-EOV Chris@1296: include ActiveRecord::Acts::Tree::InstanceMethods Chris@1296: Chris@1296: def self.roots Chris@1296: find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}) Chris@1296: end Chris@1296: Chris@1296: def self.root Chris@1296: find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}) Chris@1296: end Chris@1296: EOV Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: module InstanceMethods Chris@1296: # Returns list of ancestors, starting from parent until root. Chris@1296: # Chris@1296: # subchild1.ancestors # => [child1, root] Chris@1296: def ancestors Chris@1296: node, nodes = self, [] Chris@1296: nodes << node = node.parent while node.parent Chris@1296: nodes Chris@1296: end Chris@1296: Chris@1296: # Returns list of descendants. Chris@1296: # Chris@1296: # root.descendants # => [child1, subchild1, subchild2] Chris@1296: def descendants(depth=nil) Chris@1296: depth ||= 0 Chris@1296: result = children.dup Chris@1296: unless depth == 1 Chris@1296: result += children.collect {|child| child.descendants(depth-1)}.flatten Chris@1296: end Chris@1296: result Chris@1296: end Chris@1296: Chris@1296: # Returns list of descendants and a reference to the current node. Chris@1296: # Chris@1296: # root.self_and_descendants # => [root, child1, subchild1, subchild2] Chris@1296: def self_and_descendants(depth=nil) Chris@1296: [self] + descendants(depth) Chris@1296: end Chris@1296: Chris@1296: # Returns the root node of the tree. Chris@1296: def root Chris@1296: node = self Chris@1296: node = node.parent while node.parent Chris@1296: node Chris@1296: end Chris@1296: Chris@1296: # Returns all siblings of the current node. Chris@1296: # Chris@1296: # subchild1.siblings # => [subchild2] Chris@1296: def siblings Chris@1296: self_and_siblings - [self] Chris@1296: end Chris@1296: Chris@1296: # Returns all siblings and a reference to the current node. Chris@1296: # Chris@1296: # subchild1.self_and_siblings # => [subchild1, subchild2] Chris@1296: def self_and_siblings Chris@1296: parent ? parent.children : self.class.roots Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: end