From d14a8130233e2ad99acfed4542ecb0bdab340511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Adamczyk?= Date: Wed, 29 May 2024 17:57:47 +0200 Subject: [PATCH] Fix updating translations (#319) --- .../concerns/spree/admin/translatable.rb | 21 +++++-- .../admin/products/edit/translations_spec.rb | 56 +++++++++++++++++-- 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/app/controllers/concerns/spree/admin/translatable.rb b/app/controllers/concerns/spree/admin/translatable.rb index 87ce89f294..81bdd02433 100644 --- a/app/controllers/concerns/spree/admin/translatable.rb +++ b/app/controllers/concerns/spree/admin/translatable.rb @@ -20,13 +20,26 @@ def save_translation_values locales_to_update = current_store_locales & params_locales locales_to_update.each do |locale| - translation = @object.translations.find_or_initialize_by(locale: locale) - translation_params.each do |attribute, translations| - translation.public_send("#{attribute}=", translations[locale]) + if update_translation_value?(locale) + translation = @object.translations.find_or_initialize_by(locale: locale) + translation_params.each do |attribute, translations| + translation.public_send("#{attribute}=", translations[locale]) + end + translation.save! + else + Mobility.with_locale(locale) do + translation_params.each do |attribute, translations| + @object.public_send("#{attribute}=", translations[locale]) + end + @object.save! + end end - translation.save! end end + + def update_translation_value?(locale) + Spree.always_use_translations? || I18n.default_locale.to_s != locale + end end end end diff --git a/spec/features/admin/products/edit/translations_spec.rb b/spec/features/admin/products/edit/translations_spec.rb index ac658cf83e..05943c8dd4 100644 --- a/spec/features/admin/products/edit/translations_spec.rb +++ b/spec/features/admin/products/edit/translations_spec.rb @@ -4,7 +4,7 @@ stub_authorization! let(:store) { Spree::Store.default } - let(:product) { create(:product, stores: [store]) } + let(:product) { create(:product, stores: [store], name: 'Old Product Name EN') } context 'managing translations' do context 'when there is more than one locale configured for a store' do @@ -12,6 +12,54 @@ store.update!(default_locale: 'en', supported_locales: 'en,fr') end + it 'allows an admin to update all translations' do + visit spree.admin_product_path(product) + + click_link 'Translations' + + fill_in 'translation_name_en', with: 'Product Name EN', fill_options: { clear: :backspace } + fill_in 'translation_name_fr', with: 'Product Name FR' + click_button 'Update' + + wait_for_turbo + + expect(product.translations.count).to eq(1) + + translation_fr = product.translations.find_by!(locale: 'fr') + expect(translation_fr.name).to eq('Product Name FR') + expect(translation_fr.slug).to eq('product-name-fr') + + expect(product.reload.name).to eq('Product Name EN') + expect(product.name_en).to eq('Product Name EN') + end + + context 'when changing translations after switching store default locale' do + before do + store.update!(default_locale: 'fr') + end + + it 'allows an admin to update all translations' do + visit spree.admin_product_path(product.id) + + click_link 'Translations' + + fill_in 'translation_name_en', with: 'Product Name EN', fill_options: { clear: :backspace } + fill_in 'translation_name_fr', with: 'Product Name FR' + click_button 'Update' + + wait_for_turbo + + expect(product.translations.count).to eq(1) + + translation_fr = product.translations.find_by!(locale: 'fr') + expect(translation_fr.name).to eq('Product Name FR') + expect(translation_fr.slug).to eq('product-name-fr') + + expect(product.reload.name).to eq('Product Name EN') + expect(product.name_en).to eq('Product Name EN') + end + end + context 'when there are no translations for a given language' do let(:new_translation) { 'This is a test French translation' } let(:expected_new_slug) { 'this-is-a-test-french-translation' } @@ -25,7 +73,7 @@ wait_for_turbo - expect(product.translations.count).to eq(2) + expect(product.translations.count).to eq(1) translation_fr = product.translations.find_by!(locale: 'fr') expect(translation_fr.name).to eq(new_translation) @@ -55,7 +103,7 @@ wait_for_turbo - expect(product.translations.count).to eq(2) + expect(product.translations.count).to eq(1) translation_fr = product.translations.find_by!(locale: 'fr') expect(translation_fr.name).to eq(new_name_translation) @@ -75,7 +123,7 @@ wait_for_turbo - expect(product.translations.count).to eq(2) + expect(product.translations.count).to eq(1) translation_fr = product.translations.find_by!(locale: 'fr') expect(translation_fr.name).to eq(new_name_translation)