spree/spree — pull request #4741 by jhawthorn (2014-05-26 06:48:17+00:00)
Title: [WIP] Improve shipping, add quantity to InventoryUnit

Marked WIP because this breaks `ReturnAuthorization`s (which are somewhat broken anyways, but going away soon)

This work was done with the goal of having a speedy checkout with an order having 50_000 of an item. Under the current master that results in 50_000 `InventoryUnit`s being inserted and then afterwards selected multiple times per request. This takes over a minute for some requests.

Existing InventoryUnits are migrated by setting quantity=1.

## Major Changes

`InventoryUnit` now has a `quantity`, which makes checking out any quantity of a line_item the same speed. This has resulted in a small amount of additional complexity, in the form of new `#remove` and `#fill_backorders` methods which each take a quantity.

`Shipment#to_package` now relies on `Shipment#manifest`, which fixes two issues with the previous implementation:
* It now correctly handles variant not matching line_item (which can be the case when using spree-product-assembly)
* It will now re-assemble the package with correct quantities. The existing implementation would have

Optimized `Shipment#manifest`. It now performs counting of inventory quantities in SQL without loading or instantiating InventoryUnits. Variants and line_items are then loaded as two `SELECT FROM ... WHERE id IN (?)` queries. It previously performed n+1 queries in order to select variants and line items.

Removed the `order` association on `InventoryUnit` and removed the `order_id` column. Order can always be determined from shipment, and it now delegates order to shipment.

As always, any feedback is appreciated.