Skip to content

Commit 31de226

Browse files
committed
don't rebuild in the middle of prepending siblings.
1 parent 96b4091 commit 31de226

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

.rspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--format documentation --color
1+
--format documentation --color --order random

lib/closure_tree/hierarchy_maintenance.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ def _ct_skip_cycle_detection!
1515
@_ct_skip_cycle_detection = true
1616
end
1717

18+
def _ct_skip_hierarchy_maintenance!
19+
@_ct_skip_hierarchy_maintenance = true
20+
end
21+
1822
def _ct_validate
1923
if !@_ct_skip_cycle_detection &&
2024
!new_record? && # don't validate for cycles if we're a new record
@@ -32,7 +36,7 @@ def _ct_before_save
3236

3337
def _ct_after_save
3438
if changes[_ct.parent_column_name] || @was_new_record
35-
rebuild!
39+
rebuild! unless @_ct_skip_hierarchy_maintenance
3640
end
3741
if changes[_ct.parent_column_name] && !@was_new_record
3842
# Resetting the ancestral collections addresses
@@ -41,6 +45,7 @@ def _ct_after_save
4145
self_and_ancestors.reload
4246
end
4347
@was_new_record = false # we aren't new anymore.
48+
@_ct_skip_hierarchy_maintenance = false # only skip once.
4449
true # don't cancel anything.
4550
end
4651

lib/closure_tree/numeric_deterministic_ordering.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ def prepend_sibling(sibling_node)
8585
def add_sibling(sibling, add_after = true)
8686
fail "can't add self as sibling" if self == sibling
8787

88-
# Make sure self or sibling isn't dirty, because we're going to call reload:
88+
# Make sure self isn't dirty, because we're going to call reload:
8989
save
90-
sibling.save
9190

9291
_ct.with_advisory_lock do
9392
prior_sibling_parent = sibling.parent
@@ -97,8 +96,9 @@ def add_sibling(sibling, add_after = true)
9796
self.order_value
9897
end
9998

100-
sibling.update_order_value(self.order_value)
99+
sibling.order_value = self.order_value
101100
sibling.parent = self.parent
101+
sibling._ct_skip_hierarchy_maintenance!
102102
sibling.save # may be a no-op
103103

104104
_ct_reorder_siblings(reorder_from_value)

0 commit comments

Comments
 (0)