brendon / acts_as_list

An ActiveRecord plugin for managing lists.
http://brendon.github.io/acts_as_list/
MIT License
2.04k stars 355 forks source link

Reorder based on scope #416

Closed chrisrp closed 1 year ago

chrisrp commented 1 year ago

So this is more of a question if there is a way to reorder the items or a better way to use the gem.. I will describe my scenario.

I have an hipotetical list of 3 items that were in different scope and then end up in the same scope with a gap in positions:

ElementA: 5 ElementB: 10 ElementC: 15

As the user tries to move ElementA to the end of the list, the front end will sent me position 3. The backend is using model#insert_at(3) to perform this.

And what happens is the ElementA is still at the top of the list.

Is there a way to come around this? I thought about implementing an reorder, but maybe there is a better way to do it.

Another way to do it would be to have ups and down arrows so frontent could send this info and I would use move_lower()/move_higher() methods. This would work but would be a little bit more complicate because UI guys might freak out about it.

brendon commented 1 year ago

Hi @chrisrp, the gem is designed to automatically put an item at the end of a scopes list if you change its scope. You can also change the list scope and define a new position in the list in the same update and the gem will move other items out of the way. You should check the position of the item you want to move your item in front of and set your items position to the same as that of the item you want to move your item in front of.

Within the same scope the same rules apply. If you set the position, the gem will update the positions of other items to get them out of the way.

It's best not to use insert_at. If I recall, this just overrides the position to be exactly what you say without moving anything out of the way. The normal way is just to .update(position: whatever).

Hope that helps :)