Files
grecia/app/controllers/concerns/translatable.rb
Marko Lovic 3aa53449c8 Fix Translatable when field values are changed to blank
If we ignore all params that are blank, there is no way to
"remove" an attribute (i.e. change its value to blank)

On the other hand, we don't want to create new translations
where all fields are empty, so the new code keeps only the
blank fields which belong to existing translations.
2018-09-10 17:16:42 +02:00

32 lines
869 B
Ruby

module Translatable
extend ActiveSupport::Concern
included do
before_action :delete_translations, only: [:update]
end
private
def translation_params(params)
resource_model
.globalize_attribute_names
.select { |k| params[k].present? ||
resource_model.translated_locales.include?(get_locale_from_attribute(k)) }
end
def delete_translations
locales = resource_model.translated_locales
.select { |l| params.dig(:delete_translations, l) == "1" }
locales.each do |l|
Globalize.with_locale(l) do
resource.translation.destroy
end
end
end
def get_locale_from_attribute(attribute_name)
locales = resource_model.globalize_locales
attribute_name.to_s.match(/(#{locales.join('|')})\Z/)&.captures&.first
end
end