Chris@1296
|
1 module ActiveRecord
|
Chris@1296
|
2 module Acts
|
Chris@1296
|
3 module Tree
|
Chris@1296
|
4 def self.included(base)
|
Chris@1296
|
5 base.extend(ClassMethods)
|
Chris@1296
|
6 end
|
Chris@1296
|
7
|
Chris@1296
|
8 # Specify this +acts_as+ extension if you want to model a tree structure by providing a parent association and a children
|
Chris@1296
|
9 # association. This requires that you have a foreign key column, which by default is called +parent_id+.
|
Chris@1296
|
10 #
|
Chris@1296
|
11 # class Category < ActiveRecord::Base
|
Chris@1296
|
12 # acts_as_tree :order => "name"
|
Chris@1296
|
13 # end
|
Chris@1296
|
14 #
|
Chris@1296
|
15 # Example:
|
Chris@1296
|
16 # root
|
Chris@1296
|
17 # \_ child1
|
Chris@1296
|
18 # \_ subchild1
|
Chris@1296
|
19 # \_ subchild2
|
Chris@1296
|
20 #
|
Chris@1296
|
21 # root = Category.create("name" => "root")
|
Chris@1296
|
22 # child1 = root.children.create("name" => "child1")
|
Chris@1296
|
23 # subchild1 = child1.children.create("name" => "subchild1")
|
Chris@1296
|
24 #
|
Chris@1296
|
25 # root.parent # => nil
|
Chris@1296
|
26 # child1.parent # => root
|
Chris@1296
|
27 # root.children # => [child1]
|
Chris@1296
|
28 # root.children.first.children.first # => subchild1
|
Chris@1296
|
29 #
|
Chris@1296
|
30 # In addition to the parent and children associations, the following instance methods are added to the class
|
Chris@1296
|
31 # after calling <tt>acts_as_tree</tt>:
|
Chris@1296
|
32 # * <tt>siblings</tt> - Returns all the children of the parent, excluding the current node (<tt>[subchild2]</tt> when called on <tt>subchild1</tt>)
|
Chris@1296
|
33 # * <tt>self_and_siblings</tt> - Returns all the children of the parent, including the current node (<tt>[subchild1, subchild2]</tt> when called on <tt>subchild1</tt>)
|
Chris@1296
|
34 # * <tt>ancestors</tt> - Returns all the ancestors of the current node (<tt>[child1, root]</tt> when called on <tt>subchild2</tt>)
|
Chris@1296
|
35 # * <tt>root</tt> - Returns the root of the current node (<tt>root</tt> when called on <tt>subchild2</tt>)
|
Chris@1296
|
36 module ClassMethods
|
Chris@1296
|
37 # Configuration options are:
|
Chris@1296
|
38 #
|
Chris@1296
|
39 # * <tt>foreign_key</tt> - specifies the column name to use for tracking of the tree (default: +parent_id+)
|
Chris@1296
|
40 # * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet.
|
Chris@1296
|
41 # * <tt>counter_cache</tt> - keeps a count in a +children_count+ column if set to +true+ (default: +false+).
|
Chris@1296
|
42 def acts_as_tree(options = {})
|
Chris@1296
|
43 configuration = { :foreign_key => "parent_id", :dependent => :destroy, :order => nil, :counter_cache => nil }
|
Chris@1296
|
44 configuration.update(options) if options.is_a?(Hash)
|
Chris@1296
|
45
|
Chris@1296
|
46 belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
|
Chris@1296
|
47 has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => configuration[:dependent]
|
Chris@1296
|
48
|
Chris@1296
|
49 class_eval <<-EOV
|
Chris@1296
|
50 include ActiveRecord::Acts::Tree::InstanceMethods
|
Chris@1296
|
51
|
Chris@1296
|
52 def self.roots
|
Chris@1296
|
53 find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
|
Chris@1296
|
54 end
|
Chris@1296
|
55
|
Chris@1296
|
56 def self.root
|
Chris@1296
|
57 find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
|
Chris@1296
|
58 end
|
Chris@1296
|
59 EOV
|
Chris@1296
|
60 end
|
Chris@1296
|
61 end
|
Chris@1296
|
62
|
Chris@1296
|
63 module InstanceMethods
|
Chris@1296
|
64 # Returns list of ancestors, starting from parent until root.
|
Chris@1296
|
65 #
|
Chris@1296
|
66 # subchild1.ancestors # => [child1, root]
|
Chris@1296
|
67 def ancestors
|
Chris@1296
|
68 node, nodes = self, []
|
Chris@1296
|
69 nodes << node = node.parent while node.parent
|
Chris@1296
|
70 nodes
|
Chris@1296
|
71 end
|
Chris@1296
|
72
|
Chris@1296
|
73 # Returns list of descendants.
|
Chris@1296
|
74 #
|
Chris@1296
|
75 # root.descendants # => [child1, subchild1, subchild2]
|
Chris@1296
|
76 def descendants(depth=nil)
|
Chris@1296
|
77 depth ||= 0
|
Chris@1296
|
78 result = children.dup
|
Chris@1296
|
79 unless depth == 1
|
Chris@1296
|
80 result += children.collect {|child| child.descendants(depth-1)}.flatten
|
Chris@1296
|
81 end
|
Chris@1296
|
82 result
|
Chris@1296
|
83 end
|
Chris@1296
|
84
|
Chris@1296
|
85 # Returns list of descendants and a reference to the current node.
|
Chris@1296
|
86 #
|
Chris@1296
|
87 # root.self_and_descendants # => [root, child1, subchild1, subchild2]
|
Chris@1296
|
88 def self_and_descendants(depth=nil)
|
Chris@1296
|
89 [self] + descendants(depth)
|
Chris@1296
|
90 end
|
Chris@1296
|
91
|
Chris@1296
|
92 # Returns the root node of the tree.
|
Chris@1296
|
93 def root
|
Chris@1296
|
94 node = self
|
Chris@1296
|
95 node = node.parent while node.parent
|
Chris@1296
|
96 node
|
Chris@1296
|
97 end
|
Chris@1296
|
98
|
Chris@1296
|
99 # Returns all siblings of the current node.
|
Chris@1296
|
100 #
|
Chris@1296
|
101 # subchild1.siblings # => [subchild2]
|
Chris@1296
|
102 def siblings
|
Chris@1296
|
103 self_and_siblings - [self]
|
Chris@1296
|
104 end
|
Chris@1296
|
105
|
Chris@1296
|
106 # Returns all siblings and a reference to the current node.
|
Chris@1296
|
107 #
|
Chris@1296
|
108 # subchild1.self_and_siblings # => [subchild1, subchild2]
|
Chris@1296
|
109 def self_and_siblings
|
Chris@1296
|
110 parent ? parent.children : self.class.roots
|
Chris@1296
|
111 end
|
Chris@1296
|
112 end
|
Chris@1296
|
113 end
|
Chris@1296
|
114 end
|
Chris@1296
|
115 end
|