def restore_ancestry_integrity!
parents = {}
self.base_class.transaction do
self.base_class.send(:with_exclusive_scope) do
self.base_class.find_each do |node|
if !node.valid? and !node.errors[node.class.ancestry_column].blank?
node.without_ancestry_callbacks do
node.update_attribute node.ancestry_column, nil
end
end
parents[node.id] = node.parent_id if exists? node.parent_id
parent = parents[node.id]
until parent.nil? || parent == node.id
parent = parents[parent]
end
parents[node.id] = nil if parent == node.id
end
self.base_class.find_each do |node|
ancestry, parent = nil, parents[node.id]
until parent.nil?
ancestry, parent = if ancestry.nil? then parent else "#{parent}/#{ancestry}" end, parents[parent]
end
node.without_ancestry_callbacks do
node.update_attribute node.ancestry_column, ancestry
end
end
end
end
end