@@ -12,11 +12,16 @@ module NumericDeterministicOrdering
1212 end
1313
1414 def _ct_reorder_prior_siblings_if_parent_changed
15- return unless saved_change_to_attribute? ( _ct . parent_column_name ) && !@was_new_record
15+ return if @was_new_record
16+
17+ parent_changed = saved_change_to_attribute? ( _ct . parent_column_name )
18+ scope_changed = _ct . scope_changed? ( self )
19+
20+ return unless parent_changed || scope_changed
1621
1722 was_parent_id = attribute_before_last_save ( _ct . parent_column_name )
18- scope_conditions = _ct . scope_values_from_instance ( self )
19- _ct . reorder_with_parent_id ( was_parent_id , nil , scope_conditions )
23+ previous_scope_conditions = _ct . previous_scope_values_from_instance ( self )
24+ _ct . reorder_with_parent_id ( was_parent_id , nil , previous_scope_conditions )
2025 end
2126
2227 def _ct_reorder_siblings ( minimum_sort_order_value = nil )
@@ -132,9 +137,7 @@ def append_child(child_node)
132137 def prepend_child ( child_node )
133138 child_node . order_value = -1
134139 child_node . parent = self
135- child_node . _ct_skip_sort_order_maintenance!
136140 if child_node . save
137- _ct_reorder_children
138141 child_node . reload
139142 else
140143 child_node
@@ -161,19 +164,11 @@ def add_sibling(sibling, add_after = true)
161164
162165 _ct . with_advisory_lock do
163166 prior_sibling_parent = sibling . parent
164- reorder_from_value = if prior_sibling_parent == parent
165- [ order_value , sibling . order_value ] . compact . min
166- else
167- order_value
168- end
169167
170168 sibling . order_value = order_value
171169 sibling . parent = parent
172- sibling . _ct_skip_sort_order_maintenance!
173170 sibling . save # may be a no-op
174171
175- _ct_reorder_siblings ( reorder_from_value )
176-
177172 # The sort order should be correct now except for self and sibling, which may need to flip:
178173 sibling_is_after = reload . order_value < sibling . reload . order_value
179174 if add_after != sibling_is_after
0 commit comments