swanandp/acts_as_list — pull request #38 by dubroe (2012-06-02 17:55:43+00:00)
Title: Added ability to set item positions in a form

I'm working on an e-commerce site and one aspect of the site is that vendors are able to manipulate their menus. Here is an example of a test menu: https://skitch.com/dubroe/86en1/rhubs-menu-creator-test.

When a menu item is clicked, the vendor is presented with a form in which he can update the menu item attributes, including the item's position in the list: https://skitch.com/dubroe/86ena/rhubs-edit-menu-item-test

Before this fork, doing so messed everything up. Say I had 8 items in the menu, and I wanted to move the item in position 6 to position 4, this would end up with the positions being: 1,2,3,4,4,5,7,8. Basically the other positions weren't updated as they should be.

I've added "after_update :update_positions" which checks to see if two items have the same position, and if they do, update the positions using the already existing "shuffle_positions_on_intermediate_items" function.

I had to make a small change to that function so that you can pass in an ID to ignore, such that the recently changed position doesn't get changed right after it was set.

Please let me know if you have any questions or issues with my code. I'm happy to make any modifications that you suggest. Overall though I think this is an important feature to add to this gem as without it lists are vulnerable to being corrupted if people manually update item positions.

Thanks,

~Elan Dubrofsky