2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
class Ticket::Priority < ApplicationModel
|
2019-01-28 06:04:05 +00:00
|
|
|
include CanBeImported
|
2020-02-20 13:34:03 +00:00
|
|
|
include HasCollectionUpdate
|
2021-01-27 12:55:22 +00:00
|
|
|
include HasSearchIndexBackend
|
2020-02-20 13:34:03 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
self.table_name = 'ticket_priorities'
|
2016-01-15 17:22:57 +00:00
|
|
|
validates :name, presence: true
|
2017-02-12 17:21:03 +00:00
|
|
|
|
|
|
|
after_create :ensure_defaults
|
|
|
|
after_update :ensure_defaults
|
|
|
|
after_destroy :ensure_defaults
|
|
|
|
|
|
|
|
attr_accessor :callback_loop
|
|
|
|
|
|
|
|
def ensure_defaults
|
2017-11-23 08:09:44 +00:00
|
|
|
return true if callback_loop
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-02-12 17:21:03 +00:00
|
|
|
priorities_with_default = Ticket::Priority.where(default_create: true)
|
2017-11-23 08:09:44 +00:00
|
|
|
return true if priorities_with_default.count == 1
|
2017-02-12 17:21:03 +00:00
|
|
|
|
|
|
|
if priorities_with_default.count.zero?
|
|
|
|
priority = Ticket::Priority.where(active: true).order(id: :asc).first
|
|
|
|
priority.default_create = true
|
|
|
|
priority.callback_loop = true
|
|
|
|
priority.save!
|
2017-11-23 08:09:44 +00:00
|
|
|
return true
|
2017-02-12 17:21:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if priorities_with_default.count > 1
|
2017-10-01 12:25:52 +00:00
|
|
|
Ticket::Priority.all.each do |local_priority|
|
2017-02-12 17:21:03 +00:00
|
|
|
next if local_priority.id == id
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-02-12 17:21:03 +00:00
|
|
|
local_priority.default_create = false
|
|
|
|
local_priority.callback_loop = true
|
|
|
|
local_priority.save!
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-02-12 17:21:03 +00:00
|
|
|
end
|
2017-11-23 08:09:44 +00:00
|
|
|
true
|
2017-02-12 17:21:03 +00:00
|
|
|
end
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|