Skip to content

Commit

Permalink
Add ability to get all siblings, including the current child, with ac…
Browse files Browse the repository at this point in the history
…ts_as_tree. Recloses #2140.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2507 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
Marcel Molina committed Oct 9, 2005
1 parent 5ef931a commit c8e364a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
7 changes: 6 additions & 1 deletion activerecord/lib/active_record/acts/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ def self.root
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
Expand Down
9 changes: 9 additions & 0 deletions activerecord/test/mixin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,15 @@ def test_siblings
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
Expand Down

0 comments on commit c8e364a

Please sign in to comment.