Santitize description translations

Sanitize all not marked for destruction translations when description
is a translatable attribute.
This commit is contained in:
Senén Rodero Rodríguez
2019-01-25 10:55:27 +01:00
committed by Julian Herrero
parent a68098eaed
commit 0b3a3b97f7

View File

@@ -9,11 +9,35 @@ module Sanitizable
protected
def sanitize_description
self.description = WYSIWYGSanitizer.new.sanitize(description)
if translatable_description?
sanitize_description_translations
else
self.description = WYSIWYGSanitizer.new.sanitize(description)
end
end
def sanitize_tag_list
self.tag_list = TagSanitizer.new.sanitize_tag_list(tag_list) if self.class.taggable?
end
def translatable_description?
self.class.included_modules.include?(Globalizable) && self.class.translated_attribute_names.include?(:description)
end
def sanitize_description_translations
# Sanitize description when using attribute accessor in place of nested translations.
# This is because Globalize gem create translations on after save callback
# https://github.com/globalize/globalize/blob/e37c471775d196cd4318e61954572c300c015467/lib/globalize/active_record/act_macro.rb#L105
if translations.empty?
Globalize.with_locale(I18n.locale) do
self.description = WYSIWYGSanitizer.new.sanitize(description)
end
end
translations.reject(&:_destroy).each do |translation|
Globalize.with_locale(translation.locale) do
self.description = WYSIWYGSanitizer.new.sanitize(description)
end
end
end
end