checklist.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Checklist < ApplicationModel
  3. include HasDefaultModelUserRelations
  4. include ChecksClientNotification
  5. include HasHistory
  6. include Checklist::TriggersSubscriptions
  7. include Checklist::Assets
  8. include CanChecklistSortedItems
  9. belongs_to :ticket, optional: true
  10. has_many :items, inverse_of: :checklist, dependent: :destroy
  11. scope :for_user, ->(user) { joins(:ticket).where(ticket: { group: user.group_ids_access('read') }) }
  12. after_update :update_ticket
  13. after_destroy :update_ticket
  14. validates :name, presence: { allow_blank: true }
  15. validates :ticket_id, presence: true, uniqueness: { allow_nil: true }
  16. history_attributes_ignored :sorted_item_ids
  17. def history_log_attributes
  18. {
  19. related_o_id: ticket_id,
  20. related_history_object: 'Ticket',
  21. }
  22. end
  23. def history_create
  24. history_log('created', created_by_id, { value_to: name })
  25. end
  26. def history_destroy
  27. history_log('removed', updated_by_id, { value_to: name })
  28. end
  29. def notify_clients_data_attributes
  30. {
  31. id: id,
  32. ticket_id: ticket_id,
  33. updated_at: updated_at,
  34. updated_by_id: updated_by_id,
  35. }
  36. end
  37. def completed?
  38. incomplete.zero?
  39. end
  40. def incomplete
  41. Auth::RequestCache.fetch_value("Checklist/#{id}/incomplete") do
  42. items.count(&:incomplete?)
  43. end
  44. end
  45. def update_ticket
  46. ticket.updated_at = Time.zone.now
  47. ticket.updated_by_id = UserInfo.current_user_id || updated_by_id
  48. ticket.save!
  49. end
  50. end