spree-contrib / spree_globalize

Adds support for model translations (multi-language stores) using Globalize gem
https://spreecommerce.org
BSD 3-Clause "New" or "Revised" License
24 stars 102 forks source link

Fix generated slugs being overwritten by globalize stash #80

Closed kshalot closed 3 years ago

kshalot commented 3 years ago

Issue

The globalize stash contains values of the duplicated product's slug translations and thus overwrites re-generated slugs of the copy.

Outcome

Creating a copy of a product results in a duplicate with the default slug translation being nil and the rest of them being an exact copy of the original's. This makes it impossible to access or remove the copy without removing the original first since slugs of both the original product and the copy are the same.

Description

Spree Globalize injects custom behaviour into Spree's product duplication: https://github.com/spree-contrib/spree_globalize/blob/c7e9e5fff9a5e4bc51d53293f679c5954be1a229/app/models/spree/product_decorator.rb#L35-L37

https://github.com/spree-contrib/spree_globalize/blob/c7e9e5fff9a5e4bc51d53293f679c5954be1a229/app/models/spree/product_decorator.rb#L45-L49

translation.dup (permalink) populates the Globalize stash with the original's translations. This is the cause of the problem since the slugs are translatable but we don't want to make an exact copy of them.

After saving the duplicate, the save_translations! method (permalink) in Globalize Adapter that is called. Since it runs after we already saved our copy with slugs generated by slug_candidates, the slugs we generated get overwritten by exact copies of the original product.

One of the slugs is nil probably because of how product.dup in ProductDuplicator and works.

Steps to reproduce

  1. Create a product
  2. For every locale, assign a slug to the product
  3. Click "Copy" in the product's row

You should see a "success" notification and be redirected to the original product's page.