From 621f8ce0f051209928a67337a8d599097f93aff8 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 16 Apr 2012 17:06:48 +0200 Subject: [PATCH] Modular reimplementation of open/closed tickets counter. --- app/models/ticket.rb | 18 ------------ .../ticket/observer/user_ticket_counter.rb | 28 +++++++++++++++++++ config/application.rb | 1 + 3 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 app/models/ticket/observer/user_ticket_counter.rb diff --git a/app/models/ticket.rb b/app/models/ticket.rb index c054bae0a..f9bd77767 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -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') diff --git a/app/models/ticket/observer/user_ticket_counter.rb b/app/models/ticket/observer/user_ticket_counter.rb new file mode 100644 index 000000000..1627dc86d --- /dev/null +++ b/app/models/ticket/observer/user_ticket_counter.rb @@ -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 \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index 6032f943a..df8269218 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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.