Skip to content

Commit

Permalink
Fix unique index constraint failure on item destroy (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
yjukaku authored and brendon committed Jun 5, 2018
1 parent 0ba0540 commit 33851d6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/acts_as_list/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,14 @@ def decrement_positions_on_higher_items(position)
def decrement_positions_on_lower_items(position=nil)
return unless in_list?
position ||= send(position_column).to_i
acts_as_list_list.where("#{quoted_position_column_with_table_name} > ?", position).decrement_all

if sequential_updates?
acts_as_list_list.where("#{quoted_position_column_with_table_name} > ?", position).reorder(acts_as_list_order_argument(:asc)).each do |item|
item.decrement!(position_column)
end
else
acts_as_list_list.where("#{quoted_position_column_with_table_name} > ?", position).decrement_all
end
end

# Increments position (<tt>position_column</tt>) of all items in the list.
Expand Down
12 changes: 12 additions & 0 deletions test/test_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -888,4 +888,16 @@ def test_insert_at
new.insert_at(3)
assert_equal 3, new.pos
end

def test_destroy
new_item = SequentialUpdatesDefault.create
assert_equal 5, new_item.pos

new_item.insert_at(2)
assert_equal 2, new_item.pos

new_item.destroy
assert_equal [1,2,3,4], SequentialUpdatesDefault.all.map(&:pos).sort

end
end

0 comments on commit 33851d6

Please sign in to comment.