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