checklist.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. after_create :ensure_at_least_one_item
  10. belongs_to :ticket, optional: true
  11. has_many :items, inverse_of: :checklist, dependent: :destroy
  12. scope :for_user, ->(user) { joins(:ticket).where(ticket: { group: user.group_ids_access('read') }) }
  13. validates :name, presence: { allow_blank: true }
  14. validates :ticket_id, presence: true, uniqueness: { allow_nil: true }
  15. history_attributes_ignored :sorted_item_ids
  16. def history_log_attributes
  17. {
  18. related_o_id: ticket_id,
  19. related_history_object: 'Ticket',
  20. }
  21. end
  22. def history_create
  23. history_log('created', created_by_id, { value_to: name })
  24. end
  25. def history_destroy
  26. history_log('removed', updated_by_id, { value_to: name })
  27. end
  28. def notify_clients_data_attributes
  29. {
  30. id: id,
  31. ticket_id: ticket_id,
  32. updated_at: updated_at,
  33. updated_by: updated_by_id,
  34. }
  35. end
  36. def completed?
  37. !items.exists?(checked: false)
  38. end
  39. private
  40. def ensure_at_least_one_item
  41. return if items.any?
  42. items.create!(text: '', created_by: created_by, updated_by: updated_by)
  43. end
  44. end