diff --git a/app/helpers/translatable_form_helper.rb b/app/helpers/translatable_form_helper.rb index 977c73628..0232131fb 100644 --- a/app/helpers/translatable_form_helper.rb +++ b/app/helpers/translatable_form_helper.rb @@ -39,7 +39,9 @@ module TranslatableFormHelper def new_translation_for(locale) @object.translations.new(locale: locale).tap do |translation| - translation.mark_for_destruction unless locale == I18n.locale + unless locale == I18n.locale && no_other_translations?(translation) + translation.mark_for_destruction + end end end @@ -50,6 +52,10 @@ module TranslatableFormHelper data: { locale: locale } } end + + def no_other_translations?(translation) + (@object.translations - [translation]).reject(&:_destroy).empty? + end end class TranslationsFieldsBuilder < FoundationRailsHelper::FormBuilder diff --git a/spec/shared/features/translatable.rb b/spec/shared/features/translatable.rb index ba5f5f2d5..8e1334bbd 100644 --- a/spec/shared/features/translatable.rb +++ b/spec/shared/features/translatable.rb @@ -119,6 +119,29 @@ shared_examples "translatable" do |factory_name, path_name, input_fields, textar expect_page_to_have_translatable_field field, :es, with: "" end + scenario "Update a translation not having the current locale", :js do + translatable.translations.destroy_all + + translatable.translations.create( + fields.map { |field| [field, text_for(field, :fr)] }.to_h.merge(locale: :fr) + ) + + visit path + + expect(page).not_to have_link "English" + expect(page).to have_link "Français" + + click_button update_button_text + + expect(page).not_to have_css "#error_explanation" + expect(page).not_to have_link "English" + + visit path + + expect(page).not_to have_link "English" + expect(page).to have_link "Français" + end + scenario "Remove a translation", :js do visit path