Permalink
Browse files

Add attribute query method for parent id

Based on #97 by sj26
closes #97
  • Loading branch information...
1 parent 4c38dd1 commit 8650a52d322f006275b022d2432f69155c6579e3 Stefan Henzen committed May 7, 2013
Showing with 7 additions and 0 deletions.
  1. +4 −0 lib/ancestry/instance_methods.rb
  2. +3 −0 test/has_ancestry_test.rb
View
4 lib/ancestry/instance_methods.rb
@@ -122,6 +122,10 @@ def parent
if parent_id.blank? then nil else unscoped_find(parent_id) end
end
+ def parent_id?
+ parent_id.present?
+ end
+
# Root
def root_id
if ancestor_ids.empty? then id else ancestor_ids.first end
View
3 test/has_ancestry_test.rb
@@ -107,6 +107,7 @@ def test_tree_navigation
# Parent assertions
assert_equal nil, lvl0_node.parent_id
assert_equal nil, lvl0_node.parent
+ refute lvl0_node.parent_id?
# Root assertions
assert_equal lvl0_node.id, lvl0_node.root_id
assert_equal lvl0_node, lvl0_node.root
@@ -139,6 +140,7 @@ def test_tree_navigation
# Parent assertions
assert_equal lvl0_node.id, lvl1_node.parent_id
assert_equal lvl0_node, lvl1_node.parent
+ assert lvl1_node.parent_id?
# Root assertions
assert_equal lvl0_node.id, lvl1_node.root_id
assert_equal lvl0_node, lvl1_node.root
@@ -171,6 +173,7 @@ def test_tree_navigation
# Parent assertions
assert_equal lvl1_node.id, lvl2_node.parent_id
assert_equal lvl1_node, lvl2_node.parent
+ assert lvl2_node.parent_id?
# Root assertions
assert_equal lvl0_node.id, lvl2_node.root_id
assert_equal lvl0_node, lvl2_node.root

0 comments on commit 8650a52

Please sign in to comment.