From 0b3a3b97f7e6b9624ac359ef95b1f9495412eb79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Fri, 25 Jan 2019 10:55:27 +0100 Subject: [PATCH] Santitize description translations Sanitize all not marked for destruction translations when description is a translatable attribute. --- app/models/concerns/sanitizable.rb | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/sanitizable.rb b/app/models/concerns/sanitizable.rb index 4e3251fc1..b5b3fd431 100644 --- a/app/models/concerns/sanitizable.rb +++ b/app/models/concerns/sanitizable.rb @@ -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