trabajo-afectivo/app/models/ticket/state.rb

106 lines
2.7 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Ticket::State < ApplicationModel
include LatestChangeObserved
after_create :ensure_defaults
after_update :ensure_defaults
after_destroy :ensure_defaults
belongs_to :state_type, class_name: 'Ticket::StateType'
belongs_to :next_state, class_name: 'Ticket::State'
validates :name, presence: true
attr_accessor :callback_loop
=begin
list tickets by customer
2017-01-09 14:00:03 +00:00
states = Ticket::State.by_category('open') # open|closed|work_on|work_on_all|pending_reminder|pending_action|merged
returns:
state objects
=end
def self.by_category(category)
if category == 'open'
return Ticket::State.where(
state_type_id: Ticket::StateType.where(name: ['new', 'open', 'pending reminder', 'pending action'])
)
2015-09-06 23:54:47 +00:00
elsif category == 'pending_reminder'
return Ticket::State.where(
state_type_id: Ticket::StateType.where(name: ['pending reminder'])
2015-09-06 23:54:47 +00:00
)
2015-09-08 07:49:15 +00:00
elsif category == 'pending_action'
return Ticket::State.where(
state_type_id: Ticket::StateType.where(name: ['pending action'])
2015-09-08 07:49:15 +00:00
)
2015-09-06 23:54:47 +00:00
elsif category == 'work_on'
return Ticket::State.where(
state_type_id: Ticket::StateType.where(name: %w(new open))
2015-09-06 23:54:47 +00:00
)
elsif category == 'work_on_all'
return Ticket::State.where(
state_type_id: Ticket::StateType.where(name: ['new', 'open', 'pending reminder'])
)
elsif category == 'closed'
return Ticket::State.where(
state_type_id: Ticket::StateType.where(name: %w(closed))
)
2017-01-09 14:00:03 +00:00
elsif category == 'merged'
return Ticket::State.where(
state_type_id: Ticket::StateType.where(name: %w(merged))
)
2014-02-03 19:23:00 +00:00
end
2016-03-01 14:26:46 +00:00
raise "Unknown category '#{category}'"
end
=begin
check if state is ignored for escalation
state = Ticket::State.lookup(name: 'state name')
result = state.ignore_escalation?
returns:
true/false
=end
def ignore_escalation?
return true if ignore_escalation
false
end
def ensure_defaults
return if callback_loop
%w(default_create default_follow_up).each do |default_field|
states_with_default = Ticket::State.where(default_field => true)
next if states_with_default.count == 1
if states_with_default.count.zero?
state = Ticket::State.where(active: true).order(id: :asc).first
state[default_field] = true
state.callback_loop = true
state.save!
next
end
Ticket::State.all.each { |local_state|
next if local_state.id == id
next if local_state[default_field] == false
local_state[default_field] = false
local_state.callback_loop = true
local_state.save!
next
}
end
end
end