Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
View
1  README.rdoc
@@ -74,6 +74,7 @@ The has_ancestry methods supports the following options:
:destroy All children are destroyed as well (default)
:rootify The children of the destroyed node become root nodes
:restrict An AncestryException is raised if any children exist
+ :parentify The orphan subtree is added to the parent of the deleted node.If the deleted node is Root, then rootify the orphan subtree.
:cache_depth Cache the depth of each node in the 'ancestry_depth' column (default: false)
If you turn depth_caching on for an existing model:
- Migrate: add_column [table], :ancestry_depth, :integer, :default => 0
View
3  lib/ancestry/instance_methods.rb
@@ -52,7 +52,8 @@ def apply_orphan_strategy
elsif self.base_class.orphan_strategy == :parentify
descendants.all.each do |descendant|
descendant.without_ancestry_callbacks do
- descendant.update_attribute descendant.class.ancestry_column, descendant.ancestor_ids.delete_if { |x| x == self.id }.join("/")
+ new_ancestry = descendant.ancestor_ids.delete_if { |x| x == self.id }.join("/")
+ descendant.update_attribute descendant.class.ancestry_column, new_ancestry || nil
end
end
# ... throw an exception if it has children and orphan strategy is restrict
View
20 test/has_ancestry_test.rb
@@ -326,16 +326,18 @@ def test_orphan_restrict_strategy
def test_orphan_parentify_strategy
AncestryTestDatabase.with_model do |model|
- model.orphan_strategy = :parentify
- n1 = model.create!
- n2 = model.create!(:parent => n1)
- n3 = model.create!(:parent => n2)
- n4 = model.create!(:parent => n2)
- n5 = model.create!(:parent => n4)
- # delete a node with desecendants
- n2.destroy
- assert_equal(model.find(n5.id).ancestor_ids,[n1.id,n4.id], "ancestry integrity not maintained")
+ model.orphan_strategy = :parentify # set the orphan strategy as paerntify
+ n1 = model.create! #create a root node
+ n2 = model.create!(:parent => n1) #create child with parent=root
+ n3 = model.create!(:parent => n2) #create child with parent=n2, depth = 2
+ n4 = model.create!(:parent => n2) #create child with parent=n2, depth = 2
+ n5 = model.create!(:parent => n4) #create child with parent=n4, depth = 3
+ n2.destroy # delete a node with desecendants
assert_equal(model.find(n3.id).parent,n1, "orphan's not parentified" )
+ assert_equal(model.find(n5.id).ancestor_ids,[n1.id,n4.id], "ancestry integrity not maintained")
+ n1.destroy # delete a root node with desecendants
+ assert_equal(model.find(n3.id).parent_id,nil," Children of the deleted root not rootfied")
+ assert_equal(model.find(n5.id).ancestor_ids,[n4.id],"ancestry integrity not maintained")
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.