From ebf5dddc39f09bac6640084edbaaefa9c9d48477 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 27 Nov 2012 12:37:18 +0100 Subject: moved ticket fetching logic from controller to model --- help/app/controllers/tickets_controller.rb | 43 ++---------------------------- 1 file changed, 2 insertions(+), 41 deletions(-) (limited to 'help/app/controllers/tickets_controller.rb') diff --git a/help/app/controllers/tickets_controller.rb b/help/app/controllers/tickets_controller.rb index 3ce3a8a..2e55c36 100644 --- a/help/app/controllers/tickets_controller.rb +++ b/help/app/controllers/tickets_controller.rb @@ -82,37 +82,13 @@ class TicketsController < ApplicationController def index #TODO: we will need pagination - if admin? - if params[:admin_status] == 'mine' - @tickets = tickets_by_admin(current_user.id) #returns Array so pagination does not work - elsif params[:open_status] == 'open' - @tickets = Ticket.by_updated_at_and_is_open - # @tickets = Ticket.by_is_open.key(true) #returns CouchRest::Model::Designs::View - elsif params[:open_status] == 'closed' - @tickets = Ticket.by_updated_at_and_is_closed - # @tickets = Ticket.by_is_open.key(false) #returns CouchRest::Model::Designs::View - else - # @tickets = Ticket.all #returns CouchRest::Model::Designs::View - @tickets = Ticket.by_updated_at - end + @tickets = Ticket.for_admin(current_user, params) else - #TODO---if, when logged in, user accessed unauthenticated ticket, then seems okay to list it in their list of tickets. Thus, include all tickets that the user has posted to, not just those that they created. - if params[:open_status] == 'open' - @tickets = Ticket.by_is_open_and_created_by.key([true, current_user.id]) - elsif params[:open_status] == 'closed' - @tickets = Ticket.by_is_open_and_created_by.key([false, current_user.id]) - else - @tickets = Ticket.by_created_by(:key => current_user.id) - end + @tickets = Ticket.for_user(current_user, params) end - - # todo. presumably quite inefficent. sorts by updated_at increasing. would also make it an array, so pagination wouldn't work - # @tickets = @tickets.sort{|x,y| x.updated_at <=> y.updated_at} - #below works if @tickets is a CouchRest::Model::Designs::View, but not if it is an Array @tickets = @tickets.page(params[:page]).per(10) #TEST - #respond_with(@tickets) end @@ -132,21 +108,6 @@ class TicketsController < ApplicationController access_denied unless ticket_access? end - def tickets_by_admin(id=current_user.id) #returns Array which doesn't work for pagination, as it is now. - admin_tickets = [] - tickets = Ticket.all - tickets.each do |ticket| - ticket.comments.each do |comment| - if comment.posted_by == id and (params[:open_status] != 'open' or ticket.is_open) and (params[:open_status] != 'closed' or !ticket.is_open) #limit based on whether the ticket is open if open_status is set to open or closed - admin_tickets << ticket - break - end - end - end - # TODO. is this inefficent?: - # this sorts by updated at increasing: - admin_tickets.sort{|x,y| x.updated_at <=> y.updated_at} - end def set_strings @post_reply_str = 'Post reply' #t :post_reply -- cgit v1.2.3