checks_html_sanitized.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module ChecksHtmlSanitized
  3. extend ActiveSupport::Concern
  4. included do
  5. before_create :sanitized_html_attributes
  6. before_update :sanitized_html_attributes
  7. end
  8. def sanitized_html_attributes
  9. html_attributes = self.class.instance_variable_get(:@sanitized_html) || []
  10. return true if html_attributes.blank?
  11. html_attributes.each do |attribute|
  12. value = send(attribute)
  13. next if value.blank?
  14. next if !sanitizeable?(attribute, value)
  15. send("#{attribute}=".to_sym, HtmlSanitizer.strict(value))
  16. end
  17. true
  18. end
  19. def sanitizeable?(_attribute, _value)
  20. true
  21. end
  22. # methods defined here are going to extend the class, not the instance of it
  23. class_methods do
  24. =begin
  25. serve method to mark HTML attributes that need to get sanitized
  26. class Model < ApplicationModel
  27. include Sanitized
  28. sanitized_html :body
  29. end
  30. =end
  31. def sanitized_html(*attributes)
  32. @sanitized_html = attributes
  33. end
  34. end
  35. end