content.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase::Answer::Translation::Content < ApplicationModel
  3. include HasAgentAllowedParams
  4. include HasRichText
  5. AGENT_ALLOWED_ATTRIBUTES = %i[body].freeze
  6. has_one :translation, class_name: 'KnowledgeBase::Answer::Translation', inverse_of: :content, dependent: :nullify
  7. has_rich_text :body
  8. attachments_cleanup!
  9. def visible?
  10. translation.answer.visible?
  11. end
  12. def visible_internally?
  13. translation.answer.visible_internally?
  14. end
  15. delegate :created_by_id, to: :translation
  16. def attributes_with_association_ids
  17. attrs = super
  18. add_attachments_to_attributes(attrs)
  19. end
  20. def attributes_with_association_names(empty_keys: false)
  21. attrs = super
  22. add_attachments_to_attributes(attrs)
  23. end
  24. def add_attachments_to_attributes(attributes)
  25. attributes['attachments'] = attachments
  26. .reject { |file| HasRichText.attachment_inline?(file) }
  27. .map(&:attributes_for_display)
  28. attributes
  29. end
  30. def search_index_attribute_lookup(include_references: true)
  31. attrs = super
  32. attrs['body'] = ActionController::Base.helpers.strip_tags attrs['body']
  33. attrs
  34. end
  35. private
  36. def touch_translation
  37. translation&.touch # rubocop:disable Rails/SkipsModelValidations
  38. end
  39. before_save :sanitize_body
  40. after_save :touch_translation
  41. after_touch :touch_translation
  42. def sanitize_body
  43. self.body = HtmlSanitizer.dynamic_image_size(body)
  44. end
  45. end