Skip to content

Add parent query methods #97

Closed
wants to merge 1 commit into from
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?
+ 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
+ assert !lvl0_node.parent?
# 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?
# 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?
# Root assertions
assert_equal lvl0_node.id, lvl2_node.root_id
assert_equal lvl0_node, lvl2_node.root
Something went wrong with that request. Please try again.