Fix updating translatables without current locale

The current locale wasn't being marked for destruction and so saving
the record tried to create a new translation for the current locale.
This commit is contained in:
Javi Martín
2018-10-15 16:23:18 +02:00
parent 21cf39d5ed
commit 34b5a88564
2 changed files with 30 additions and 1 deletions

View File

@@ -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

View File

@@ -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