Added a before_save callback for cache_depth #74

Merged
merged 1 commit into from May 7, 2013
View
1 lib/ancestry/has_ancestry.rb
@@ -68,6 +68,7 @@ def has_ancestry options = {}
# Cache depth in depth cache column before save
before_validation :cache_depth
+ before_save :cache_depth
# Validate depth column
validates_numericality_of depth_cache_column, :greater_than_or_equal_to => 0, :only_integer => true, :allow_nil => false
View
11 test/has_ancestry_test.rb
@@ -472,6 +472,17 @@ def test_depth_caching
end
end
+ def test_depth_caching_after_subtree_movement
+ AncestryTestDatabase.with_model :depth => 6, :width => 1, :cache_depth => true, :depth_cache_column => :depth_cache do |model, roots|
+ node = model.at_depth(3).first
+ node.update_attributes(:parent => model.roots.first)
+ assert_equal(1, node.depth_cache)
+ node.descendants.each do |descendant|
+ assert_equal(descendant.depth, descendant.depth_cache)
+ end
+ end
+ end
+
def test_depth_scopes
AncestryTestDatabase.with_model :depth => 4, :width => 2, :cache_depth => true do |model, roots|
model.before_depth(2).all? { |node| assert node.depth < 2 }