From cb068077017e5d664fe413048dfcfcb102efb44f Mon Sep 17 00:00:00 2001 From: Braulio Bhavamitra Date: Wed, 6 Feb 2013 19:21:00 +0000 Subject: [PATCH] Speed up build_ancestry --- lib/acts_as_filesystem.rb | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/acts_as_filesystem.rb b/lib/acts_as_filesystem.rb index 19f8997..f0e2f02 100644 --- a/lib/acts_as_filesystem.rb +++ b/lib/acts_as_filesystem.rb @@ -30,13 +30,17 @@ module ActsAsFileSystem module ClassMethods def build_ancestry(parent_id = nil, ancestry = '') - self.base_class.all(:conditions => {:parent_id => parent_id}).each do |node| - node.ancestry = ancestry - node.save :run_callbacks => false + ActiveRecord::Base.transaction do + self.base_class.all(:conditions => {:parent_id => parent_id}).each do |node| + node.ancestry = ancestry + node.save :run_callbacks => false - build_ancestry node.id, (ancestry.empty? ? "#{node.formatted_ancestry_id}" : - "#{ancestry}#{node.ancestry_sep}#{node.formatted_ancestry_id}") + build_ancestry node.id, (ancestry.empty? ? "#{node.formatted_ancestry_id}" : + "#{ancestry}#{node.ancestry_sep}#{node.formatted_ancestry_id}") + end end + + #raise "Couldn't reach and set ancestry on every record" if self.base_class.count(:conditions => ['ancestry is null']) != 0 end end @@ -129,10 +133,18 @@ module ActsAsFileSystem end def top_ancestor - self.hierarchy.first + if has_ancestry? and !ancestry.nil? + self.class.base_class.find_by_id self.top_ancestor_id + else + self.hierarchy.first + end end def top_ancestor_id - self.ancestor_ids.first + if has_ancestry? and !ancestry.nil? + self.ancestor_ids.first + else + self.hierarchy.first.id + end end # returns the full hierarchy from the top-level item to this one. For -- libgit2 0.21.2