annotate .svn/pristine/28/28460f7863cf0e308a71ddfcfde8f112f06716f0.svn-base @ 1295:622f24f53b42 redmine-2.3

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