Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/closure_tree/numeric_deterministic_ordering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ def _ct_reorder_prior_siblings_if_parent_changed
def _ct_reorder_siblings(minimum_sort_order_value = nil)
scope_conditions = _ct.scope_values_from_instance(self)
_ct.reorder_with_parent_id(_ct_parent_id, minimum_sort_order_value, scope_conditions)
reload unless destroyed?
return if destroyed?

# Only the order column can have been changed by the reorder, so read it back
# rather than reloading, which would discard the record's dirty tracking.
self.order_value = self.class.where(self.class.primary_key => _ct_id).pick(_ct.order_column_sym)
clear_attribute_change(_ct.order_column_sym)
end

def _ct_reorder_children(minimum_sort_order_value = nil)
Expand Down
18 changes: 18 additions & 0 deletions test/closure_tree/label_order_value_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ def setup
assert_equal 1, c.reload.order_value
end

test 'should keep dirty tracking when a node is moved to another location for Label' do
root = Label.create(name: 'root')
a = root.children.create(name: 'a')
b = root.children.create(name: 'b')
c = root.children.create(name: 'c')

root2 = Label.create(name: 'root2')
b.update!(parent: root2, name: 'b2')

assert b.saved_change_to_name?
assert_equal %w[b b2], b.saved_change_to_name
refute b.changed?

assert_equal 0, b.order_value
assert_equal 0, a.reload.order_value
assert_equal 1, c.reload.order_value
end

test 'prepend_child should produce 0-based order values when reordering within the same parent' do
root = Label.create(name: 'root')
a = root.children.create(name: 'a')
Expand Down