aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/acts/tree.rb7
-rw-r--r--activerecord/test/mixin_test.rb9
3 files changed, 17 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 1022638d31..809c567127 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add ability to get all siblings, including the current child, with acts_as_tree. Recloses #2140. [Michael Schuerig <michael@schuerig.de>]
+
* Add geometric type for postgresql adapter. #2233 [akaspick@gmail.com]
* Add option (true by default) to generate reader methods for each attribute of a record to avoid the overhead of calling method missing. In partial fullfilment of #1236. [skaes@web.de]
diff --git a/activerecord/lib/active_record/acts/tree.rb b/activerecord/lib/active_record/acts/tree.rb
index 5fc7ddf41c..9cf5c3f732 100644
--- a/activerecord/lib/active_record/acts/tree.rb
+++ b/activerecord/lib/active_record/acts/tree.rb
@@ -71,8 +71,13 @@ module ActiveRecord
end
define_method(:siblings) do
- ( has_parent? ? parent.children : self.class.roots ) - [self]
+ self_and_siblings - [self]
end
+
+ define_method(:self_and_siblings) do
+ has_parent? ? parent.children : self.class.roots
+ end
+
end
end
end
diff --git a/activerecord/test/mixin_test.rb b/activerecord/test/mixin_test.rb
index 4fcc6b6fba..b551fb4402 100644
--- a/activerecord/test/mixin_test.rb
+++ b/activerecord/test/mixin_test.rb
@@ -280,6 +280,15 @@ class TreeTest < Test::Unit::TestCase
assert_equal [mixins(:tree_1), mixins(:tree3_1)], mixins(:tree2_1).siblings
assert_equal [mixins(:tree_1), mixins(:tree2_1)], mixins(:tree3_1).siblings
end
+
+ def test_self_and_siblings
+ assert_equal [mixins(:tree_1), mixins(:tree2_1), mixins(:tree3_1)], mixins(:tree_1).self_and_siblings
+ assert_equal [mixins(:tree_2), mixins(:tree_4)], mixins(:tree_2).self_and_siblings
+ assert_equal [mixins(:tree_3)], mixins(:tree_3).self_and_siblings
+ assert_equal [mixins(:tree_2), mixins(:tree_4)], mixins(:tree_4).self_and_siblings
+ assert_equal [mixins(:tree_1), mixins(:tree2_1), mixins(:tree3_1)], mixins(:tree2_1).self_and_siblings
+ assert_equal [mixins(:tree_1), mixins(:tree2_1), mixins(:tree3_1)], mixins(:tree3_1).self_and_siblings
+ end
end
class TouchTest < Test::Unit::TestCase