Removed duplicate code, moved logic to model (Ticket::State.by_category), thanks to Roy!

This commit is contained in:
Martin Edenhofer 2013-08-06 11:23:25 +02:00
parent 0bf90f17b6
commit c139ea0e1f
4 changed files with 79 additions and 60 deletions

View file

@ -110,32 +110,13 @@ class TicketsController < ApplicationController
# GET /api/tickets_customer
def ticket_customer
# get closed/open states
ticket_state_list_open = Ticket::State.where(
:state_type_id => Ticket::StateType.where( :name => ['new','open', 'pending reminder', 'pending action'] )
)
ticket_state_list_closed = Ticket::State.where(
:state_type_id => Ticket::StateType.where( :name => ['closed'] )
)
# get tickets
tickets_open = Ticket.where(
:customer_id => params[:customer_id],
:ticket_state_id => ticket_state_list_open
).limit(15).order('created_at DESC')
tickets_closed = Ticket.where(
:customer_id => params[:customer_id],
:ticket_state_id => ticket_state_list_closed
).limit(15).order('created_at DESC')
# return result
result = Ticket.list_by_customer(
:customer_id => params[:customer_id],
:limit => 15,
)
render :json => {
:tickets => {
:open => tickets_open,
:closed => tickets_closed
}
# :users => users,
:tickets => result
}
end
@ -212,12 +193,11 @@ class TicketsController < ApplicationController
# GET /api/ticket_merge_list/1
def ticket_merge_list
# get closed/open states
ticket_states = Ticket::State.where(
:state_type_id => Ticket::StateType.where( :name => ['new','open', 'pending reminder', 'pending action', 'closed'] )
)
ticket = Ticket.find( params[:ticket_id] )
ticket_list = Ticket.where( :customer_id => ticket.customer_id, :ticket_state_id => ticket_states )
ticket_list = Ticket.where(
:customer_id => ticket.customer_id,
:ticket_state_id => Ticket::State.by_category( 'open' )
)
.where( 'id != ?', [ ticket.id ] )
.order('created_at DESC')
.limit(6)

View file

@ -14,25 +14,17 @@ class Observer::Ticket::UserTicketCounter < ActiveRecord::Observer
return if !record.customer_id
# open ticket count
ticket_state_open_ids = Cache.get( 'ticket::state_ids::open' )
if !ticket_state_open_ids
ticket_state_open_ids = self.state_ids( ['new','open', 'pending reminder', 'pending action'] )
Cache.write( 'ticket::state_ids::open', ticket_state_open_ids, { :expires_in => 1.hour } )
end
ticket_state_open = Ticket::State.by_category( 'open' )
tickets_open = Ticket.where(
:customer_id => record.customer_id,
:ticket_state_id => ticket_state_open_ids,
:ticket_state_id => ticket_state_open,
).count()
# closed ticket count
ticket_state_closed_ids = Cache.get( 'ticket::state_ids::closed' )
if !ticket_state_closed_ids
ticket_state_closed_ids = self.state_ids( ['closed'] )
Cache.write( 'ticket::state_ids::closed', ticket_state_closed_ids, { :expires_in => 1.hour } )
end
ticket_state_closed = Ticket::State.by_category( 'closed' )
tickets_closed = Ticket.where(
:customer_id => record.customer_id,
:ticket_state_id => ticket_state_closed_ids,
:ticket_state_id => ticket_state_closed,
).count()
# check if update is needed
@ -51,17 +43,5 @@ class Observer::Ticket::UserTicketCounter < ActiveRecord::Observer
end
end
def state_ids(ticket_state_types)
ticket_state_types = Ticket::StateType.where(
:name => ticket_state_types,
)
ticket_states = Ticket::State.where( :state_type_id => ticket_state_types )
ticket_state_ids = []
ticket_states.each {|ticket_state|
ticket_state_ids.push ticket_state.id
}
ticket_state_ids
end
end

View file

@ -520,11 +520,8 @@ class Ticket < ApplicationModel
end
def self.escalation_calculation_rebuild
ticket_state_list_open = Ticket::State.where(
:state_type_id => Ticket::StateType.where(
:name => ['new','open', 'pending reminder', 'pending action']
)
)
ticket_state_list_open = Ticket::State.by_category( 'open' )
tickets = Ticket.where( :ticket_state_id => ticket_state_list_open )
tickets.each {|ticket|
ticket.escalation_calculation
@ -693,6 +690,47 @@ class Ticket < ApplicationModel
self.save
end
=begin
list tickets by customer groupd in state categroie open and closed
result = Ticket.list_by_customer(
:customer_id => 123,
:limit => 15, # optional, default 15
)
returns
result = {
:open => tickets_open,
:closed => tickets_closed,
}
=end
def self.list_by_customer(data)
# get closed/open states
ticket_state_list_open = Ticket::State.by_category( 'open' )
ticket_state_list_closed = Ticket::State.by_category( 'closed' )
# get tickets
tickets_open = Ticket.where(
:customer_id => data[:customer_id],
:ticket_state_id => ticket_state_list_open
).limit( data[:limit] || 15 ).order('created_at DESC')
tickets_closed = Ticket.where(
:customer_id => data[:customer_id],
:ticket_state_id => ticket_state_list_closed
).limit( data[:limit] || 15 ).order('created_at DESC')
return {
:open => tickets_open,
:closed => tickets_closed,
}
end
private
def number_generate

View file

@ -3,4 +3,25 @@
class Ticket::State < ApplicationModel
belongs_to :state_type, :class_name => 'Ticket::StateType'
validates :name, :presence => true
=begin
list tickets by customer
states = Ticket::State.by_category('open') # open|closed
=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'] )
)
elsif category == 'closed'
return Ticket::State.where(
:state_type_id => Ticket::StateType.where( :name => ['closed'] )
)
end
raise "Unknown category '#{category}'"
end
end