Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unique index constraint failure on item destroy #313

Merged
merged 1 commit into from
Jun 5, 2018
Merged
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
Fix unique index constraint failure on item destroy
  • Loading branch information
yjukaku committed Jun 5, 2018
commit 49f24736a19adc3ef5fcb6e6631ba46edbdbb5ed
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