Skip to content

Commit

Permalink
Add attribute query method for parent id
Browse files Browse the repository at this point in the history
Based on #97 by sj26
closes #97
  • Loading branch information
Stefan Henzen committed May 7, 2013
1 parent 4c38dd1 commit 8650a52
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/ancestry/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions test/has_ancestry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8650a52

Please sign in to comment.