def apply_orphan_strategy
if !ancestry_callbacks_disabled? && !new_record?
case self.ancestry_base_class.orphan_strategy
when :rootify
unscoped_descendants.each do |descendant|
descendant.without_ancestry_callbacks do
new_ancestry = if descendant.ancestry == child_ancestry
nil
else
descendant.ancestry.gsub(/^#{child_ancestry}\//, '')
end
descendant.update_attribute descendant.class.ancestry_column, new_ancestry
end
end
when :destroy
unscoped_descendants.each do |descendant|
descendant.without_ancestry_callbacks do
descendant.destroy
end
end
when :adopt
descendants.each do |descendant|
descendant.without_ancestry_callbacks do
new_ancestry = descendant.ancestor_ids.delete_if { |x| x == self.id }.join("/")
new_ancestry = nil if new_ancestry.empty?
descendant.update_attribute descendant.class.ancestry_column, new_ancestry || nil
end
end
when :restrict
raise Ancestry::AncestryException.new('Cannot delete record because it has descendants.') unless is_childless?
end
end
end