Added ability to set item positions directly (e.g. In a form) #38
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
https://skitch.com/dubroe/86edt/screen-shot-2012-06-02-at-1.58.03-pm
Before this patch, 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 an "after_update :update_positions" callback 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, so 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