36 lines
991 B
Ruby
36 lines
991 B
Ruby
module Translatable
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
before_action :delete_translations, only: [:update]
|
|
end
|
|
|
|
private
|
|
|
|
# TODO change method interface to remove unnecessary argument
|
|
def translation_params(_)
|
|
enabled_translations.flat_map do |locale|
|
|
resource_model.translated_attribute_names.map do |attr_name|
|
|
resource_model.localized_attr_name_for(attr_name, locale)
|
|
end
|
|
end.tap { |x| Rails.logger.debug "permitted translation params:"; p x}
|
|
end
|
|
|
|
# TODO change to resource
|
|
def delete_translations
|
|
locales = resource_model.translated_locales
|
|
.select { |l| params.dig(:enabled_translations, l) == "0" }
|
|
locales.each do |l|
|
|
Globalize.with_locale(l) do
|
|
resource.translation.destroy
|
|
end
|
|
end
|
|
end
|
|
|
|
def enabled_translations
|
|
params.fetch(:enabled_translations)
|
|
.select { |_, v| v == '1' }
|
|
.keys
|
|
end
|
|
end
|