Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added a before_save callback for cache_depth #74

Merged
merged 1 commit into from

4 participants

@scottatron

Hey there, here's my attempt at addressing issue #64.

Added a before_save callback for cache_depth as well as the existing before_validation callback. This ensures that all descendants in a subtree have their depth cache updated when the subtree is moved.

@scottatron scottatron Added a before_save callback for cache_depth as well as the existing …
…before_validation callback. This ensures that all descendants in a subtree have their depth cache updated when the subtree is moved.
47c331e
@holstvoogd
Collaborator

This seems to be the resolution to issue #64.
Shouldn't we maybe try to only update when the ancestry is actually changed? For efficiency sake?

@mguinada

Will this be merged?

+1

@StefanH StefanH merged commit eab6357 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 13, 2011
  1. @scottatron

    Added a before_save callback for cache_depth as well as the existing …

    scottatron authored
    …before_validation callback. This ensures that all descendants in a subtree have their depth cache updated when the subtree is moved.
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 0 deletions.
  1. +1 −0  lib/ancestry/has_ancestry.rb
  2. +11 −0 test/has_ancestry_test.rb
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 }
Something went wrong with that request. Please try again.