Skip to content

Commit 5155483

Browse files
authored
Handle non-numeric order in _ct_after_save (#486)
Only run reorder/scope-change logic when the order column is numeric. The _ct_after_save flow was restructured to guard calls to _ct.scope_changed? and sibling reordering with _ct.order_is_numeric?, preventing errors when models use a non-numeric order column.
1 parent e2bc781 commit 5155483

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

lib/closure_tree/hierarchy_maintenance.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ def _ct_before_save
4141
end
4242

4343
def _ct_after_save
44-
scope_changed = _ct.order_is_numeric? && _ct.scope_changed?(self)
45-
4644
if saved_changes[_ct.parent_column_name] || @was_new_record
4745
rebuild!
48-
elsif scope_changed
49-
# Scope changed without parent change - reorder old scope's siblings
50-
_ct_reorder_prior_siblings_if_parent_changed
51-
_ct_reorder_siblings
52-
elsif _ct.order_option? && saved_changes[_ct.order_column_sym]
53-
_ct_reorder_siblings(saved_changes[_ct.order_column_sym].min)
46+
elsif _ct.order_is_numeric?
47+
if _ct.scope_changed?(self)
48+
# Scope changed without parent change - reorder old scope's siblings
49+
_ct_reorder_prior_siblings_if_parent_changed
50+
_ct_reorder_siblings
51+
elsif saved_changes[_ct.order_column_sym]
52+
_ct_reorder_siblings(saved_changes[_ct.order_column_sym].min)
53+
end
5454
end
5555
if saved_changes[_ct.parent_column_name] && !@was_new_record
5656
# Resetting the ancestral collections addresses

test/closure_tree/hierarchy_maintenance_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
end
1717
end
1818

19+
describe '#_ct_after_save' do
20+
it 'does not raise when updating a node with non-numeric order column' do
21+
tag = Tag.create!(name: 'Alpha')
22+
tag.update!(name: 'Beta')
23+
assert_equal 'Beta', tag.reload.name
24+
end
25+
end
26+
1927
describe '.cleanup!' do
2028
before do
2129
@parent = Metal.create(value: 'parent metal')

0 commit comments

Comments
 (0)