priority.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Ticket::Priority < ApplicationModel
  3. include HasDefaultModelUserRelations
  4. include CanBeImported
  5. include ChecksHtmlSanitized
  6. include HasCollectionUpdate
  7. include HasSearchIndexBackend
  8. self.table_name = 'ticket_priorities'
  9. validates :name, presence: true, uniqueness: { case_sensitive: false }
  10. after_create :ensure_defaults
  11. after_update :ensure_defaults
  12. after_destroy :ensure_defaults
  13. validates :note, length: { maximum: 250 }
  14. sanitized_html :note
  15. attr_accessor :callback_loop
  16. def ensure_defaults
  17. return true if callback_loop
  18. priorities_with_default = Ticket::Priority.where(default_create: true)
  19. return true if priorities_with_default.count == 1
  20. if priorities_with_default.count.zero?
  21. priority = Ticket::Priority.where(active: true).reorder(id: :asc).first
  22. priority.default_create = true
  23. priority.callback_loop = true
  24. priority.save!
  25. return true
  26. end
  27. if priorities_with_default.count > 1
  28. Ticket::Priority.all.each do |local_priority|
  29. next if local_priority.id == id
  30. local_priority.default_create = false
  31. local_priority.callback_loop = true
  32. local_priority.save!
  33. end
  34. end
  35. true
  36. end
  37. end