From 2311d1c39364aac10bd6fb86bd2e06b750e0e0c7 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 23 Sep 2015 16:25:06 +0200 Subject: [PATCH] Fixed ticket list in user and org profile. --- app/controllers/tickets_controller.rb | 16 +++++------ app/models/ticket.rb | 38 ++++++++++++++++++++++----- app/models/ticket/escalation.rb | 2 +- app/models/ticket/overviews.rb | 6 ++--- app/models/ticket/search.rb | 2 +- 5 files changed, 45 insertions(+), 19 deletions(-) diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index f05d81a54..67d8a02d5 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -389,11 +389,11 @@ class TicketsController < ApplicationController if params[:user_id] user = User.find( params[:user_id] ) condition = { - 'tickets.state_id' => { + 'ticket.state_id' => { operator: 'is', value: Ticket::State.by_category('open').map(&:id), }, - 'tickets.customer_id' => { + 'ticket.customer_id' => { operator: 'is', value: user.id, }, @@ -407,11 +407,11 @@ class TicketsController < ApplicationController # lookup closed user tickets condition = { - 'tickets.state_id' => { + 'ticket.state_id' => { operator: 'is', value: Ticket::State.by_category('closed').map(&:id), }, - 'tickets.customer_id' => { + 'ticket.customer_id' => { operator: 'is', value: user.id, }, @@ -463,11 +463,11 @@ class TicketsController < ApplicationController if params[:organization_id] && !params[:organization_id].empty? condition = { - 'tickets.state_id' => { + 'ticket.state_id' => { operator: 'is', value: Ticket::State.by_category('open').map(&:id), }, - 'tickets.organization_id' => { + 'ticket.organization_id' => { operator: 'is', value: params[:organization_id], }, @@ -481,11 +481,11 @@ class TicketsController < ApplicationController # lookup closed org tickets condition = { - 'tickets.state_id' => { + 'ticket.state_id' => { operator: 'is', value: Ticket::State.by_category('closed').map(&:id), }, - 'tickets.organization_id' => { + 'ticket.organization_id' => { operator: 'is', value: params[:organization_id], }, diff --git a/app/models/ticket.rb b/app/models/ticket.rb index a8f3ecc88..3bce2afca 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -285,16 +285,41 @@ returns false end +=begin + +get count of tickets and tickets which match on selector + + ticket_count, tickets = Ticket.selectors(params[:condition], 6) + +=end + def self.selectors(selectors, limit = 10) return if !selectors - query, bind_params, tables = _selectors(selectors) + query, bind_params, tables = selector2sql(selectors) return [] if !query ticket_count = Ticket.where(query, *bind_params).joins(tables).count tickets = Ticket.where(query, *bind_params).joins(tables).limit(limit) [ticket_count, tickets] end - def self._selectors(selectors) +=begin + +generate condition query to search for tickets based on condition + + query_condition, bind_condition = selector2sql(params[:condition]) + +condition example + + { + 'ticket.state_id' => { + operator: 'is', + value: [1,2,5] + } + } + +=end + + def self.selector2sql(selectors) return if !selectors query = '' bind_params = [] @@ -308,13 +333,14 @@ returns tables.push selector[0].to_sym } - selectors.each {|attribute, selector| + selectors.each {|attribute, selector_raw| if query != '' query += ' AND ' end - next if !selector - next if !selector.respond_to?(:key?) - next if !selector['operator'] + selector = selector_raw.stringify_keys + fail "Invalid selector #{selector.inspect}" if !selector + fail "Invalid selector #{selector.inspect}" if !selector.respond_to?(:key?) + fail "Invalid selector, operator missing #{selector.inspect}" if !selector['operator'] return nil if !selector['value'] return nil if selector['value'].respond_to?(:empty?) && selector['value'].empty? attributes = attribute.split(/\./) diff --git a/app/models/ticket/escalation.rb b/app/models/ticket/escalation.rb index a7e94d551..2a5bbfc7d 100644 --- a/app/models/ticket/escalation.rb +++ b/app/models/ticket/escalation.rb @@ -206,7 +206,7 @@ returns if !sla.condition || sla.condition.empty? sla_selected = sla elsif sla.condition - query_condition, bind_condition = Ticket._selectors(sla.condition) + query_condition, bind_condition = Ticket.selector2sql(sla.condition) ticket = Ticket.where( query_condition, *bind_condition ).find_by(id: id) next if !ticket sla_selected = sla diff --git a/app/models/ticket/overviews.rb b/app/models/ticket/overviews.rb index c9b7eca7d..e5eb059ce 100644 --- a/app/models/ticket/overviews.rb +++ b/app/models/ticket/overviews.rb @@ -111,7 +111,7 @@ returns result = [] overviews.each { |overview| - query_condition, bind_condition = Ticket._selectors(overview.condition) + query_condition, bind_condition = Ticket.selector2sql(overview.condition) # get count count = Ticket.where( access_condition ).where( query_condition, *bind_condition ).count() @@ -136,7 +136,7 @@ returns order_by = overview_selected.group_by + '_id, ' + order_by end - query_condition, bind_condition = Ticket._selectors(overview_selected.condition) + query_condition, bind_condition = Ticket.selector2sql(overview_selected.condition) tickets = Ticket.select('id') .where( access_condition ) @@ -160,7 +160,7 @@ returns # get tickets for overview data[:start_page] ||= 1 - query_condition, bind_condition = Ticket._selectors(overview_selected.condition) + query_condition, bind_condition = Ticket.selector2sql(overview_selected.condition) tickets = Ticket.where( access_condition ) .where( query_condition, *bind_condition ) .order( overview_selected[:order][:by].to_s + ' ' + overview_selected[:order][:direction].to_s ) diff --git a/app/models/ticket/search.rb b/app/models/ticket/search.rb index b46848a48..0fc1943c3 100644 --- a/app/models/ticket/search.rb +++ b/app/models/ticket/search.rb @@ -160,7 +160,7 @@ returns .order('`tickets`.`created_at` DESC') .limit(limit) else - query_condition, bind_condition = _selectors(params[:condition]) + query_condition, bind_condition = selector2sql(params[:condition]) tickets_all = Ticket.select('DISTINCT(tickets.id)') .where(access_condition) .where(query_condition, *bind_condition)