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