answer.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Copyright (C) 2012-2024 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. scope :sorted_by_published, lambda {
  16. reorder(Arel.sql('GREATEST(knowledge_base_answers.published_at, knowledge_base_answers.updated_at) DESC'))
  17. .published
  18. }
  19. scope :sorted_by_internally_published, lambda {
  20. case ActiveRecord::Base.connection_db_config.configuration_hash[:adapter]
  21. when 'mysql2'
  22. reorder(Arel.sql('GREATEST(LEAST(IFNULL(knowledge_base_answers.internal_at,1), IFNULL(knowledge_base_answers.published_at, 1)), knowledge_base_answers.updated_at) DESC'))
  23. else
  24. reorder(Arel.sql('GREATEST(LEAST(knowledge_base_answers.internal_at, knowledge_base_answers.published_at), knowledge_base_answers.updated_at) DESC'))
  25. end
  26. .internal
  27. }
  28. acts_as_list scope: :category, top_of_list: 0
  29. # provide consistent naming with KB category
  30. alias_attribute :parent, :category
  31. alias assets_essential assets
  32. def attributes_with_association_ids
  33. attrs = super
  34. attrs[:attachments] = attachments_sorted.map { |elem| self.class.attachment_to_hash(elem) }
  35. attrs[:tags] = tag_list
  36. attrs
  37. end
  38. def assets(data = {})
  39. return data if assets_added_to?(data)
  40. data = super(data)
  41. data = category.assets(data)
  42. ApplicationModel::CanAssets.reduce(translations, data)
  43. end
  44. attachments_cleanup!
  45. def attachments_sorted
  46. attachments.sort_by { |elem| elem.filename.downcase }
  47. end
  48. def add_attachment(file)
  49. filename = file.try(:original_filename) || File.basename(file.path)
  50. content_type = file.try(:content_type) || MIME::Types.type_for(filename).first&.content_type || 'application/octet-stream'
  51. Store.create!(
  52. object: self.class.name,
  53. o_id: id,
  54. data: file.read,
  55. filename: filename,
  56. preferences: { 'Content-Type': content_type }
  57. )
  58. touch # rubocop:disable Rails/SkipsModelValidations
  59. translations.each(&:touch)
  60. true
  61. end
  62. def remove_attachment(attachment_id)
  63. attachment = attachments.find { |elem| elem.id == attachment_id.to_i }
  64. raise ActiveRecord::RecordNotFound if attachment.nil?
  65. Store.remove_item(attachment.id)
  66. touch # rubocop:disable Rails/SkipsModelValidations
  67. translations.each(&:touch)
  68. true
  69. end
  70. def api_url
  71. Rails.application.routes.url_helpers.knowledge_base_answer_path(category.knowledge_base, self)
  72. end
  73. # required by CanCloneAttachments
  74. def content_type
  75. 'text/html'
  76. end
  77. private
  78. def touch_translations
  79. translations
  80. .reject(&:destroyed?)
  81. .each(&:touch) # touch each translation separately to trigger after_commit callbacks
  82. end
  83. after_touch :touch_translations
  84. class << self
  85. def attachment_to_hash(attachment)
  86. url = Rails.application.routes.url_helpers.attachment_path(attachment.id)
  87. {
  88. id: attachment.id,
  89. url: url,
  90. preview_url: "#{url}?preview=1",
  91. filename: attachment.filename,
  92. size: attachment.size,
  93. preferences: attachment.preferences
  94. }
  95. end
  96. end
  97. end