Don't disable new invalid translations

After adding a new translation with invalid data and sending the form,
we were disabling the new translation when displaying the form again to
the user, which was confusing.
This commit is contained in:
Javi Martín
2018-10-08 14:15:45 +02:00
committed by decabeza
parent 6fc3389587
commit a8f8a7bc4f
3 changed files with 23 additions and 3 deletions

View File

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

View File

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

View File

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