- 
                Notifications
    You must be signed in to change notification settings 
- Fork 31
Description
Processors::Product finds or initializes a new product based on the @data["Handle"]. Then, later on, it assigns the slug again to the @data["Handle"]. This is normally not a problem. In a recent import, however, we came across a handle/slug data issue that was difficult to debug. We think this is due to how Solidus generates valid slugs from invalid input.
Reproduction steps
Here's how you can reproduce:
- In your product import CSV, add a Handlewith an invalid character. i.e.arts-&-crafts-box
- Run the product import.
- Check the slug of the created product. (It should be arts-crafts-box.)
- Run the import again.
Current behaviour
A new product is initialized for arts-&-crafts-box, then the slug is re-generated to be arts-crafts-box, and we end up with a ActiveRecord::InvalidRecord Slug has already been taken error.
Expected behaviour
The arts-crafts-box product is found and loaded. No new product is initialized.
Solution
I am not sure what the best solution would be. Perhaps there is a more nuanced problem in Solidus where Product.find_or_initialize_by(slug: slug) is not the best way to find or initialize a product.
I think at the very least we could validate that the slug input in the CSV would be a valid slug as written. In this example, the first import would complete successfully, but the user would expect their product to be at /product/arts-&-crafts-box and they might be confused and think their import didn't work. The user's second import would fail, and it would be hard for them to figure out why.