lib/ancestry/instance_methods.rb
def apply_orphan_strategy
      if !ancestry_callbacks_disabled? && !new_record?
        case self.ancestry_base_class.orphan_strategy
        when :rootify # make all children root if orphan strategy is rootify
          unscoped_descendants.each do |descendant|
            descendant.without_ancestry_callbacks do
              new_ancestry = if descendant.ancestry == child_ancestry
                nil
              else
                # child_ancestry did not change so child_ancestry_was will work here
                descendant.ancestry.gsub(/^#{child_ancestry}\//, '')
              end
              descendant.update_attribute descendant.class.ancestry_column, new_ancestry
            end
          end
        when :destroy # destroy all descendants if orphan strategy is destroy
          unscoped_descendants.each do |descendant|
            descendant.without_ancestry_callbacks do
              descendant.destroy
            end
          end
        when :adopt # make child elements of this node, child of its parent
          descendants.each do |descendant|
            descendant.without_ancestry_callbacks do
              new_ancestry = descendant.ancestor_ids.delete_if { |x| x == self.id }.join("/")
              # check for empty string if it's then set to nil
              new_ancestry = nil if new_ancestry.empty?
              descendant.update_attribute descendant.class.ancestry_column, new_ancestry || nil
            end
          end
        when :restrict # throw an exception if it has children
          raise Ancestry::AncestryException.new('Cannot delete record because it has descendants.') unless is_childless?
        end
      end
    end
def apply_orphan_strategy
      if !ancestry_callbacks_disabled? && !new_record?
        case self.ancestry_base_class.orphan_strategy
        when :rootify # make all children root if orphan strategy is rootify
          unscoped_descendants.each do |descendant|
            descendant.without_ancestry_callbacks do
              new_ancestry = if descendant.ancestry == child_ancestry
                nil
              else
                # child_ancestry did not change so child_ancestry_was will work here
                descendant.ancestry.gsub(/^#{child_ancestry}\//, '')
              end
              descendant.update_attribute descendant.class.ancestry_column, new_ancestry
            end
          end
        when :destroy # destroy all descendants if orphan strategy is destroy
          unscoped_descendants.each do |descendant|
            descendant.without_ancestry_callbacks do
              descendant.destroy
            end
          end
        when :adopt # make child elements of this node, child of its parent
          descendants.each do |descendant|
            descendant.without_ancestry_callbacks do
              new_ancestry = descendant.ancestor_ids.delete_if { |x| x == self.id }.join("/")
              # check for empty string if it's then set to nil
              new_ancestry = nil if new_ancestry.empty?
              descendant.update_attribute descendant.class.ancestry_column, new_ancestry || nil
            end
          end
        when :restrict # throw an exception if it has children
          raise Ancestry::AncestryException.new('Cannot delete record because it has descendants.') unless is_childless?
        end
      end
    end
Apply orphan strategy (before destroy - no changes)