You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tree.rb 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # frozen_string_literal: true
  2. module ActiveRecord
  3. module Acts
  4. module Tree
  5. def self.included(base)
  6. base.extend(ClassMethods)
  7. end
  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 < ApplicationRecord
  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. belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
  46. has_many :children, lambda {order(configuration[:order])}, :class_name => name, :foreign_key => configuration[:foreign_key], :dependent => configuration[:dependent]
  47. scope :roots, lambda {
  48. where("#{configuration[:foreign_key]} IS NULL").
  49. order(configuration[:order])
  50. }
  51. send :include, ActiveRecord::Acts::Tree::InstanceMethods
  52. end
  53. end
  54. module InstanceMethods
  55. # Returns list of ancestors, starting from parent until root.
  56. #
  57. # subchild1.ancestors # => [child1, root]
  58. def ancestors
  59. node, nodes = self, []
  60. nodes << node = node.parent while node.parent
  61. nodes
  62. end
  63. # Returns list of descendants.
  64. #
  65. # root.descendants # => [child1, subchild1, subchild2]
  66. def descendants(depth=nil)
  67. depth ||= 0
  68. result = children.dup
  69. unless depth == 1
  70. result += children.collect {|child| child.descendants(depth-1)}.flatten
  71. end
  72. result
  73. end
  74. # Returns list of descendants and a reference to the current node.
  75. #
  76. # root.self_and_descendants # => [root, child1, subchild1, subchild2]
  77. def self_and_descendants(depth=nil)
  78. [self] + descendants(depth)
  79. end
  80. # Returns the root node of the tree.
  81. def root
  82. node = self
  83. node = node.parent while node.parent
  84. node
  85. end
  86. # Returns all siblings of the current node.
  87. #
  88. # subchild1.siblings # => [subchild2]
  89. def siblings
  90. self_and_siblings - [self]
  91. end
  92. # Returns all siblings and a reference to the current node.
  93. #
  94. # subchild1.self_and_siblings # => [subchild1, subchild2]
  95. def self_and_siblings
  96. parent ? parent.children : self.class.roots
  97. end
  98. end
  99. end
  100. end
  101. end