Modular reimplementation of open/closed tickets counter.

This commit is contained in:
Martin Edenhofer 2012-04-16 17:06:48 +02:00
parent 1035723797
commit 621f8ce0f0
3 changed files with 29 additions and 18 deletions

View file

@ -1,8 +1,5 @@
class Ticket < ActiveRecord::Base
before_create :number_generate, :check_defaults
after_create :user_ticket_counter_update
after_update :user_ticket_counter_update
after_destroy :user_ticket_counter_update
belongs_to :group
has_many :articles
@ -94,21 +91,6 @@ class Ticket < ActiveRecord::Base
return subject
end
def user_ticket_counter_update
return if !self.customer_id
ticket_state_list_open = Ticket::State.where( :ticket_state_type_id => Ticket::StateType.where(:name => ['new','open', 'pending remidner', 'pending action']) )
ticket_state_list_closed = Ticket::State.where( :ticket_state_type_id => Ticket::StateType.where(:name => ['closed'] ) )
tickets_open = Ticket.where( :customer_id => self.customer_id, :ticket_state_id => ticket_state_list_open ).count()
tickets_closed = Ticket.where( :customer_id => self.customer_id, :ticket_state_id => ticket_state_list_closed ).count()
customer = User.find( self.customer_id )
customer[:preferences][:tickets_open] = tickets_open
customer[:preferences][:tickets_closed] = tickets_closed
customer.save
end
private
def number_generate
Ticket.new.number_adapter = Setting.get('ticket_number')

View file

@ -0,0 +1,28 @@
class Ticket::Observer::UserTicketCounter < ActiveRecord::Observer
observe 'ticket'
def after_create(record)
user_ticket_counter_update(record)
end
def after_update(record)
user_ticket_counter_update(record)
end
def after_update(record)
user_ticket_counter_update(record)
end
def user_ticket_counter_update(record)
return if !record.customer_id
ticket_state_list_open = Ticket::State.where( :ticket_state_type_id => Ticket::StateType.where(:name => ['new','open', 'pending remidner', 'pending action']) )
ticket_state_list_closed = Ticket::State.where( :ticket_state_type_id => Ticket::StateType.where(:name => ['closed'] ) )
tickets_open = Ticket.where( :customer_id => record.customer_id, :ticket_state_id => ticket_state_list_open ).count()
tickets_closed = Ticket.where( :customer_id => record.customer_id, :ticket_state_id => ticket_state_list_closed ).count()
customer = User.find( record.customer_id )
customer[:preferences][:tickets_open] = tickets_open
customer[:preferences][:tickets_closed] = tickets_closed
customer.save
end
end

View file

@ -29,6 +29,7 @@ module Zammad
'ticket::_observer::_first_response',
'ticket::_observer::_last_contact',
'ticket::_observer::_close_time',
'ticket::_observer::_user_ticket_counter',
'ticket::_observer::_notification'
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.