spree/spree — Issue #712: Prevent stale on_hand updates for variants.
Author: BDQ
Created: 2011-10-21 07:54:51+00:00
State: open

We should probably add optimistic locking on the Variant model to prevent on_hand values from being updated with stale values.

http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html

Original issue from spree-user:

We ran into an issue with how editing product works in admin that I thought should be brought to everyone's attention.

For our site, each of our products will have a robust write up and for various reasons our editors will occasionally go in and change copy. Our products are sold through our mailing list, and once a product is announced we can expect a steady stream of purchases.

The problem arises once an edit has to take place when the product is live. The editors will end up saving the product description with the last state of the product - including the stock count when the editor when the originally pulled up the product. So for example:

1. Editor pulls up a product. The number product on hand is 10.
2. 1 person come in to buy the product. Inventory count is now 9.
3. Editor finishes their edits and saves. Inventory count is now 10 again.

Our problem was much worst b/c our editor had the edit screen pulled up when we had lowered the price to test our gateway. HAving said that, if the price hadn't dropped from $35 to $2, I might not have noticed and we'd be wondering much later why our inventory numbers weren't accurate. I wouldn't be surprised if others have weird cases where they occasionally sell more of a product than they have on hand.

I'm not sure what the correct solution would be here. For us, we created an editor role and modified the edit form for them to not have an input field to the stock count. A more general solution might be to 'lock' the On Hand field on edit, making the admin explicitly unlock the field before it'll update the model.

I'm also going to change the controllers so that updating the product, images, product properties, etc. redirects to the product list page instead of the edit page - the way it is now is just asking for trouble. There's a good case that there should be a show page for products.