has_attachments.rb 877 B

123456789101112131415161718192021222324252627282930313233343536
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module Taskbar::HasAttachments
  3. extend ActiveSupport::Concern
  4. included do
  5. scope :with_form_id, -> { where("state LIKE '%form_id%'") }
  6. after_destroy :clear_attachments
  7. end
  8. # form_id is saved directly in a new ticket, but inside of the article when updating an existing ticket
  9. def persisted_form_id
  10. state&.dig(:form_id) || state&.dig(:article, :form_id)
  11. end
  12. private
  13. def attachments
  14. return [] if persisted_form_id.blank?
  15. UploadCache.new(persisted_form_id).attachments
  16. end
  17. def add_attachments_to_attributes(attributes)
  18. attributes.tap do |result|
  19. result['attachments'] = attachments.map(&:attributes_for_display)
  20. end
  21. end
  22. def clear_attachments
  23. return if persisted_form_id.blank?
  24. UploadCache.new(persisted_form_id).destroy
  25. end
  26. end