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