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