diff --git a/app/helpers/globalize_helper.rb b/app/helpers/globalize_helper.rb index cff0b4ccd..c6170260a 100644 --- a/app/helpers/globalize_helper.rb +++ b/app/helpers/globalize_helper.rb @@ -23,7 +23,9 @@ module GlobalizeHelper end def enable_locale?(resource, locale) - resource.translated_locales.include?(locale) || locale == I18n.locale + # Use `map` instead of `pluck` in order to keep the `params` sent + # by the browser when there's invalid data + resource.translations.map(&:locale).include?(locale) || locale == I18n.locale end def highlight_current?(locale) diff --git a/app/helpers/translatable_form_helper.rb b/app/helpers/translatable_form_helper.rb index 7795cbc12..7b4be72cc 100644 --- a/app/helpers/translatable_form_helper.rb +++ b/app/helpers/translatable_form_helper.rb @@ -25,7 +25,6 @@ module TranslatableFormHelper fields_for(:translations, translation_for(locale), builder: TranslationsFieldsBuilder) do |translations_form| @template.concat translations_form.hidden_field( :_destroy, - value: !@template.enable_locale?(@object, locale), class: "destroy_locale", data: { locale: locale }) @@ -48,7 +47,9 @@ module TranslatableFormHelper end def new_translation_for(locale) - @object.translations.new(locale: locale) + @object.translations.new(locale: locale).tap do |translation| + translation.mark_for_destruction unless locale == I18n.locale + end end end diff --git a/spec/shared/features/translatable.rb b/spec/shared/features/translatable.rb index 81deb8640..4c7144511 100644 --- a/spec/shared/features/translatable.rb +++ b/spec/shared/features/translatable.rb @@ -60,6 +60,23 @@ shared_examples "translatable" do |factory_name, path_name, fields| expect(page).to have_field(field_for(field, :fr), with: text_for(field, :fr)) end + scenario "Add an invalid translation", :js do + skip("can't have invalid translations") if required_fields.empty? + + field = required_fields.sample + + visit path + select "Français", from: "translation_locale" + fill_in field_for(field, :fr), with: "" + click_button update_button_text + + expect(page).to have_css "#error_explanation" + + click_link "Français" + + expect(page).to have_field(field_for(field, :fr), with: "") + end + scenario "Update a translation", :js do visit path