aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/acts/tree.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-09-09 08:01:44 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-09-09 08:01:44 +0000
commitb0630673213ed2bee99883cb9197ddf6457a3fac (patch)
treec28f2571c88c42e07bf9548313cabe66c196d0a7 /activerecord/lib/active_record/acts/tree.rb
parentc8e0e10e28948bce93ea9f28524f917d9e0ea2da (diff)
downloadrails-b0630673213ed2bee99883cb9197ddf6457a3fac.tar.gz
rails-b0630673213ed2bee99883cb9197ddf6457a3fac.tar.bz2
rails-b0630673213ed2bee99883cb9197ddf6457a3fac.zip
Added the instance methods #root and #ancestors on acts_as_tree and fixed siblings to not include the current node #2142, #2140 [coffee2code]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2163 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/acts/tree.rb')
-rw-r--r--activerecord/lib/active_record/acts/tree.rb38
1 files changed, 28 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/acts/tree.rb b/activerecord/lib/active_record/acts/tree.rb
index 2463a1209a..5fc7ddf41c 100644
--- a/activerecord/lib/active_record/acts/tree.rb
+++ b/activerecord/lib/active_record/acts/tree.rb
@@ -16,16 +16,23 @@ module ActiveRecord
# Example :
# root
# \_ child1
- # \_ sub-child1
+ # \_ subchild1
+ # \_ subchild2
#
# root = Category.create("name" => "root")
- # child1 = root.children.create("name" => "child1")
- # subchild1 = child1.children.create("name" => "subchild1")
+ # child1 = root.children.create("name" => "child1")
+ # subchild1 = child1.children.create("name" => "subchild1")
#
- # root.parent # => nil
+ # root.parent # => nil
# child1.parent # => root
# root.children # => [child1]
# root.children.first.children.first # => subchild1
+ #
+ # In addition to the parent and children associations, the following instance methods are added to the class
+ # after specifying the act:
+ # * siblings: Return all the children of the parent excluding the current node ([ subchild2 ] when called from subchild1)
+ # * ancestors: Returns all the ancestors of the current node ([child1, root] when called from subchild2)
+ # * root: Returns the root of the current node (root when called from subchild2)
module ClassMethods
# Configuration options are:
#
@@ -48,15 +55,26 @@ module ActiveRecord
end
END
+ # Returns list of ancestors, starting from parent until root.
+ #
+ # subchild1.ancestors # => [child1, root]
+ define_method(:ancestors) do
+ node, nodes = self, []
+ nodes << node = node.parent until not node.has_parent?
+ nodes
+ end
+
+ define_method(:root) do
+ node = self
+ node = node.parent until not node.has_parent?
+ node
+ end
+
define_method(:siblings) do
- if parent
- self.class.find(:all, :conditions => [ "#{configuration[:foreign_key]} = ?", parent.id ], :order => configuration[:order])
- else
- self.class.roots
- end
+ ( has_parent? ? parent.children : self.class.roots ) - [self]
end
end
end
end
end
-end \ No newline at end of file
+end