Validate translations in banners

This change forces us to use nested attributes for translations, instead
of using the more convenient `:"title_#{locale}"` methods.

On the other hand, we can use Rails' native `_destroy` attribute to
remove existing translations, so we don't have to use our custom
`delete_translations`, which was a bit buggy since it didn't consider
failed updates.
This commit is contained in:
Javi Martín
2018-10-07 20:03:41 +02:00
parent 7646b9f166
commit 71601bd3f8
8 changed files with 101 additions and 87 deletions

View File

@@ -1,29 +1,13 @@
module Translatable
extend ActiveSupport::Concern
included do
before_action :delete_translations, only: [:update]
end
private
def translation_params(resource_model)
return [] unless params[:enabled_translations]
resource_model.translated_attribute_names.product(enabled_translations).map do |attr_name, loc|
resource_model.localized_attr_name_for(attr_name, loc)
end
end
def delete_translations
locales = resource.translated_locales
.select { |l| params.dig(:enabled_translations, l) == "0" }
locales.each do |l|
Globalize.with_locale(l) do
resource.translation.destroy
end
end
{
translations_attributes: [:id, :_destroy, :locale] +
resource_model.translated_attribute_names
}
end
def enabled_translations