Skip to content

Commit 82b9224

Browse files
committed
added instance.root
1 parent 4bafb2b commit 82b9224

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

lib/closure_tree/acts_as_tree.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ def root?
6969
parent_id.nil?
7070
end
7171

72+
# Returns self if +root?+ or the root ancestor
73+
def root
74+
root? ? self : ancestors.last
75+
end
76+
7277
# Returns true if this node has no children.
7378
def leaf?
7479
children.empty?

lib/closure_tree/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ClosureTree
2-
VERSION = "1.0.0.beta3" unless defined?(::ClosureTree::VERSION)
2+
VERSION = "1.0.0.beta4" unless defined?(::ClosureTree::VERSION)
33
end

test/dummy/test/unit/tag_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,11 @@ def validate_city_tag city
100100
assert_equal [tags(:california), tags(:united_states), tags(:places)], city.ancestors
101101
assert_equal [city, tags(:california), tags(:united_states), tags(:places)], city.self_and_ancestors
102102
end
103+
104+
def test_root
105+
assert_equal tags(:grandparent), tags(:grandparent).root
106+
assert_equal tags(:grandparent), tags(:parent).root
107+
assert_equal tags(:grandparent), tags(:child).root
108+
end
103109
end
104110

0 commit comments

Comments
 (0)