Permalink
Browse files

Performance optimizations for rebuilding large trees.

  • Loading branch information...
1 parent 469fe34 commit c3d2274221e8b85fe3273aee86c7c0978be30153 @kueda kueda committed Nov 26, 2010
Showing with 5 additions and 5 deletions.
  1. +5 −5 lib/ancestry/class_methods.rb
@@ -52,7 +52,7 @@ def check_ancestry_integrity! options = {}
parents = {}
exceptions = [] if options[:report] == :list
# For each node ...
- self.base_class.all.each do |node|
+ self.base_class.find_each do |node|
begin
# ... check validity of ancestry column
if !node.valid? and !node.errors[node.class.ancestry_column].blank?
@@ -86,7 +86,7 @@ def check_ancestry_integrity! options = {}
def restore_ancestry_integrity!
parents = {}
# For each node ...
- self.base_class.all.each do |node|
+ self.base_class.find_each do |node|
# ... set its ancestry to nil if invalid
if node.errors[node.class.ancestry_column].blank?
node.without_ancestry_callbacks do
@@ -104,7 +104,7 @@ def restore_ancestry_integrity!
parents[node.id] = nil if parent == node.id
end
# For each node ...
- self.base_class.all.each do |node|
+ self.base_class.find_each do |node|
# ... rebuild ancestry from parents array
ancestry, parent = nil, parents[node.id]
until parent.nil?
@@ -118,7 +118,7 @@ def restore_ancestry_integrity!
# Build ancestry from parent id's for migration purposes
def build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
- self.base_class.all(:conditions => {:parent_id => parent_id}).each do |node|
+ self.base_class.find_each(:conditions => {:parent_id => parent_id}) do |node|
node.without_ancestry_callbacks do
node.update_attribute ancestry_column, ancestry
end
@@ -129,7 +129,7 @@ def build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
# Rebuild depth cache if it got corrupted or if depth caching was just turned on
def rebuild_depth_cache!
raise Ancestry::AncestryException.new("Cannot rebuild depth cache for model without depth caching.") unless respond_to? :depth_cache_column
- self.base_class.all.each do |node|
+ self.base_class.find_each do |node|
node.update_attribute depth_cache_column, node.depth
end
end

0 comments on commit c3d2274

Please sign in to comment.