answer.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase::Answer < ApplicationModel
  3. include HasTranslations
  4. include HasAgentAllowedParams
  5. include HasTags
  6. include CanBePublished
  7. include ChecksKbClientNotification
  8. include ChecksKbClientVisibility
  9. include CanCloneAttachments
  10. AGENT_ALLOWED_ATTRIBUTES = %i[category_id promoted internal_note].freeze
  11. AGENT_ALLOWED_NESTED_RELATIONS = %i[translations].freeze
  12. belongs_to :category, class_name: 'KnowledgeBase::Category', inverse_of: :answers, touch: true
  13. scope :include_contents, -> { eager_load(translations: :content) }
  14. scope :sorted, -> { order(position: :asc) }
  15. acts_as_list scope: :category, top_of_list: 0
  16. # provide consistent naming with KB category
  17. alias_attribute :parent, :category
  18. alias assets_essential assets
  19. def attributes_with_association_ids
  20. attrs = super
  21. attrs[:attachments] = attachments_sorted.map { |elem| self.class.attachment_to_hash(elem) }
  22. attrs[:tags] = tag_list
  23. attrs
  24. end
  25. def assets(data = {})
  26. return data if assets_added_to?(data)
  27. data = super(data)
  28. data = category.assets(data)
  29. ApplicationModel::CanAssets.reduce(translations, data)
  30. end
  31. attachments_cleanup!
  32. def attachments_sorted
  33. attachments.sort_by { |elem| elem.filename.downcase }
  34. end
  35. def add_attachment(file)
  36. filename = file.try(:original_filename) || File.basename(file.path)
  37. content_type = file.try(:content_type) || MIME::Types.type_for(filename).first&.content_type || 'application/octet-stream'
  38. Store.create!(
  39. object: self.class.name,
  40. o_id: id,
  41. data: file.read,
  42. filename: filename,
  43. preferences: { 'Content-Type': content_type }
  44. )
  45. touch # rubocop:disable Rails/SkipsModelValidations
  46. translations.each(&:touch)
  47. true
  48. end
  49. def remove_attachment(attachment_id)
  50. attachment = attachments.find { |elem| elem.id == attachment_id.to_i }
  51. raise ActiveRecord::RecordNotFound if attachment.nil?
  52. Store.remove_item(attachment.id)
  53. touch # rubocop:disable Rails/SkipsModelValidations
  54. translations.each(&:touch)
  55. true
  56. end
  57. def api_url
  58. Rails.application.routes.url_helpers.knowledge_base_answer_path(category.knowledge_base, self)
  59. end
  60. # required by CanCloneAttachments
  61. def content_type
  62. 'text/html'
  63. end
  64. private
  65. def touch_translations
  66. translations.each(&:touch) # move to #touch_all when migrationg to Rails 6
  67. end
  68. after_touch :touch_translations
  69. class << self
  70. def attachment_to_hash(attachment)
  71. url = Rails.application.routes.url_helpers.attachment_path(attachment.id)
  72. {
  73. id: attachment.id,
  74. url: url,
  75. preview_url: "#{url}?preview=1",
  76. filename: attachment.filename,
  77. size: attachment.size,
  78. preferences: attachment.preferences
  79. }
  80. end
  81. end
  82. end