Permalink
Browse files

Merge pull request #146 from ContinuityControl/guard-against-parent-n…

…il-in-compute-level

Association#loaded? does not imply that the target exists so check for a non nil target.
  • Loading branch information...
parndt committed Jul 5, 2012
2 parents 891c799 + 0a0b48b commit 9fdf179326091c8181d3696d4ef168501d5dfdb1
Showing with 16 additions and 5 deletions.
  1. +1 −1 lib/awesome_nested_set/awesome_nested_set.rb
  2. +15 −4 spec/awesome_nested_set_spec.rb
@@ -456,7 +456,7 @@ def to_text
protected
def compute_level
node, nesting = self, 0
- while (association = node.association(:parent)).loaded?
+ while (association = node.association(:parent)).loaded? && association.target
nesting += 1
node = node.parent
end
@@ -188,10 +188,21 @@
categories(:top_level).leaves.should == leaves
end
- it "level" do
- categories(:top_level).level.should == 0
- categories(:child_1).level.should == 1
- categories(:child_2_1).level.should == 2
+ describe "level" do
+ it "returns the correct level" do
+ categories(:top_level).level.should == 0
+ categories(:child_1).level.should == 1
+ categories(:child_2_1).level.should == 2
+ end
+
+ context "given parent associations are loaded" do
+ it "returns the correct level" do
+ child = categories(:child_1)
+ child.association(:parent).load_target
+ child.parent.association(:parent).load_target
+ child.level.should == 1
+ end
+ end
end
describe "depth" do

0 comments on commit 9fdf179

Please sign in to comment.