Skip to content
This repository
Browse code

method and test case modified to be able to rootify the subtree,

 incase the deleted node is a root.

Updated the README.rdoc
  • Loading branch information...
commit 3838cfd36474bcf9cdb48ac96a19b7c72e42d0ae 1 parent 6c2876e
unknown authored
1  README.rdoc
Source Rendered
@@ -74,6 +74,7 @@ The has_ancestry methods supports the following options:
74 74 :destroy All children are destroyed as well (default)
75 75 :rootify The children of the destroyed node become root nodes
76 76 :restrict An AncestryException is raised if any children exist
  77 + :parentify The orphan subtree is added to the parent of the deleted node.If the deleted node is Root, then rootify the orphan subtree.
77 78 :cache_depth Cache the depth of each node in the 'ancestry_depth' column (default: false)
78 79 If you turn depth_caching on for an existing model:
79 80 - Migrate: add_column [table], :ancestry_depth, :integer, :default => 0
3  lib/ancestry/instance_methods.rb
@@ -52,7 +52,8 @@ def apply_orphan_strategy
52 52 elsif self.base_class.orphan_strategy == :parentify
53 53 descendants.all.each do |descendant|
54 54 descendant.without_ancestry_callbacks do
55   - descendant.update_attribute descendant.class.ancestry_column, descendant.ancestor_ids.delete_if { |x| x == self.id }.join("/")
  55 + new_ancestry = descendant.ancestor_ids.delete_if { |x| x == self.id }.join("/")
  56 + descendant.update_attribute descendant.class.ancestry_column, new_ancestry || nil
56 57 end
57 58 end
58 59 # ... throw an exception if it has children and orphan strategy is restrict
20 test/has_ancestry_test.rb
@@ -326,16 +326,18 @@ def test_orphan_restrict_strategy
326 326
327 327 def test_orphan_parentify_strategy
328 328 AncestryTestDatabase.with_model do |model|
329   - model.orphan_strategy = :parentify
330   - n1 = model.create!
331   - n2 = model.create!(:parent => n1)
332   - n3 = model.create!(:parent => n2)
333   - n4 = model.create!(:parent => n2)
334   - n5 = model.create!(:parent => n4)
335   - # delete a node with desecendants
336   - n2.destroy
337   - assert_equal(model.find(n5.id).ancestor_ids,[n1.id,n4.id], "ancestry integrity not maintained")
  329 + model.orphan_strategy = :parentify # set the orphan strategy as paerntify
  330 + n1 = model.create! #create a root node
  331 + n2 = model.create!(:parent => n1) #create child with parent=root
  332 + n3 = model.create!(:parent => n2) #create child with parent=n2, depth = 2
  333 + n4 = model.create!(:parent => n2) #create child with parent=n2, depth = 2
  334 + n5 = model.create!(:parent => n4) #create child with parent=n4, depth = 3
  335 + n2.destroy # delete a node with desecendants
338 336 assert_equal(model.find(n3.id).parent,n1, "orphan's not parentified" )
  337 + assert_equal(model.find(n5.id).ancestor_ids,[n1.id,n4.id], "ancestry integrity not maintained")
  338 + n1.destroy # delete a root node with desecendants
  339 + assert_equal(model.find(n3.id).parent_id,nil," Children of the deleted root not rootfied")
  340 + assert_equal(model.find(n5.id).ancestor_ids,[n4.id],"ancestry integrity not maintained")
339 341 end
340 342 end
341 343

0 comments on commit 3838cfd

Please sign in to comment.
Something went wrong with that request. Please try again.