Keep invalid translation params through requests

We were reloading the values from the database and ignoring the
parameters sent by the browser.
This commit is contained in:
Javi Martín
2018-10-08 11:13:27 +02:00
committed by decabeza
parent 1874480ff0
commit 6fc3389587
2 changed files with 17 additions and 2 deletions

View File

@@ -22,7 +22,7 @@ module TranslatableFormHelper
def translatable_fields(&block)
@object.globalize_locales.map do |locale|
Globalize.with_locale(locale) do
fields_for(:translations, @object.translations.where(locale: locale).first_or_initialize, builder: TranslationsFieldsBuilder) do |translations_form|
fields_for(:translations, translation_for(locale), builder: TranslationsFieldsBuilder) do |translations_form|
@template.concat translations_form.hidden_field(
:_destroy,
value: !@template.enable_locale?(@object, locale),
@@ -37,6 +37,19 @@ module TranslatableFormHelper
end.join.html_safe
end
def translation_for(locale)
existing_translation_for(locale) || new_translation_for(locale)
end
def existing_translation_for(locale)
# Use `select` because `where` uses the database and so ignores
# the `params` sent by the browser
@object.translations.select { |translation| translation.locale == locale }.first
end
def new_translation_for(locale)
@object.translations.new(locale: locale)
end
end
class TranslationsFieldsBuilder < FoundationRailsHelper::FormBuilder

View File

@@ -95,7 +95,9 @@ shared_examples "translatable" do |factory_name, path_name, fields|
expect(page).to have_css "#error_explanation"
# TODO: check the field is now blank.
click_link "Español"
expect(page).to have_field(field_for(field, :es), with: "")
end
scenario "Remove a translation", :js do