From a335b840359b17dd2f2e2e1a8a19adcd36b1283c Mon Sep 17 00:00:00 2001 From: jessib Date: Mon, 3 Dec 2012 16:06:17 -0800 Subject: Ticket sorting functionality. Needs to be refactored, but works. --- help/app/helpers/tickets_helper.rb | 30 ++++++++++- help/app/models/ticket.rb | 82 ++++++++++++++++++++++++++--- help/app/views/tickets/_admin-nav.html.haml | 4 +- help/app/views/tickets/_order-nav.html.haml | 10 ++-- 4 files changed, 110 insertions(+), 16 deletions(-) (limited to 'help') diff --git a/help/app/helpers/tickets_helper.rb b/help/app/helpers/tickets_helper.rb index c683be3..bd2c069 100644 --- a/help/app/helpers/tickets_helper.rb +++ b/help/app/helpers/tickets_helper.rb @@ -5,11 +5,39 @@ module TicketsHelper end def admin + # do we not want this set for non-admins? the param will be viewable in the url params[:admin_status] || 'all' end + def order + params[:sort_order] || 'updated_at_desc' + end + def link_to_status(new_status) label = new_status + ' issues' - link_to label, :open_status => new_status, :admin_status => admin + link_to label, :open_status => new_status, :admin_status => admin, :sort_order => order + end + + def link_to_order(order_field) + if order.start_with?(order_field) + # link for currently-filtered field. Link to other direction of this field. + if order.end_with? 'asc' + direction = 'desc' + icon_direction = 'up' + else + direction = 'asc' + icon_direction = 'down' + end + arrow = content_tag(:i, '', class: 'icon-arrow-'+ icon_direction) + else + # for not-currently-filtered field, don't display an arrow, and link to descending direction + arrow = '' + direction = 'desc' + end + + link_to :sort_order => order_field + '_at_' + direction, :open_status => status, :admin_status => admin do + arrow + order_field + ' at' + end end + end diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index e06180f..f9852a4 100644 --- a/help/app/models/ticket.rb +++ b/help/app/models/ticket.rb @@ -40,7 +40,8 @@ class Ticket < CouchRest::Model::Base view :by_is_open view :by_created_by - view :by_updated_at # + view :by_updated_at + view :by_created_at view :by_is_open_and_created_by view :by_is_open_and_created_at @@ -76,6 +77,22 @@ class Ticket < CouchRest::Model::Base } }" + + view :includes_post_by_and_open_status_and_created_at, + :map => + "function(doc) { + var arr = {} + if (doc['type'] == 'Ticket' && doc.comments) { + doc.comments.forEach(function(comment){ + if (comment.posted_by && !arr[comment.posted_by]) { + //don't add duplicates + arr[comment.posted_by] = true; + emit([comment.posted_by, doc.is_open, doc.created_at], doc); + } + }); + } + }" + view :includes_post_by_and_updated_at, :map => "function(doc) { @@ -92,6 +109,21 @@ class Ticket < CouchRest::Model::Base }" + view :includes_post_by_and_created_at, + :map => + "function(doc) { + var arr = {} + if (doc['type'] == 'Ticket' && doc.comments) { + doc.comments.forEach(function(comment){ + if (comment.posted_by && !arr[comment.posted_by]) { + //don't add duplicates + arr[comment.posted_by] = true; + emit([comment.posted_by, doc.created_at], doc); + } + }); + } + }" + end validates :title, :presence => true @@ -107,22 +139,58 @@ class Ticket < CouchRest::Model::Base #end def self.for_user(user, options = {}, is_admin = false) - # TODO: sorting - # TODO: do these correctly default to showing open? + + # TODO: This is obviously super tedious. we will refactor later. + # TODO: thought i should reverse keys for descending, but that didn't work. look into whether that should be tweaked, and whether it works okay with pagination (seems to now...) # TODO: Time.now + 2.days is to catch tickets created in future. shouldn't happen but does on my computer now, so this at least catches for now. + # TODO handle default values correctly: + options[:open_status] = 'open' if !options[:open_status] #hacky. redo this when handling defaults correctly + if (is_admin && (options[:admin_status] != 'mine')) # show all (selected) tickets to admin if options[:open_status] == 'all' - Ticket.by_updated_at + if options[:sort_order] == 'created_at_desc' + Ticket.by_created_at.startkey(0).endkey(Time.now + 2.days).descending + elsif options[:sort_order] == 'updated_at_asc' + Ticket.by_updated_at.startkey(0).endkey(Time.now + 2.days) + elsif options[:sort_order] == 'created_at_asc' + Ticket.by_created_at.startkey(0).endkey(Time.now + 2.days) + else + Ticket.by_updated_at.startkey(0).endkey(Time.now + 2.days).descending + end else - Ticket.by_is_open_and_updated_at.startkey([(options[:open_status] == 'open'), 0]).endkey([(options[:open_status] == 'open'), Time.now + 2.days]) + if options[:sort_order] == 'created_at_desc' + Ticket.by_is_open_and_created_at.startkey([(options[:open_status] == 'open'), 0]).endkey([(options[:open_status] == 'open'), Time.now + 2.days]).descending + elsif options[:sort_order] == 'updated_at_asc' + Ticket.by_is_open_and_updated_at.startkey([(options[:open_status] == 'open'), 0]).endkey([(options[:open_status] == 'open'), Time.now + 2.days]) + elsif options[:sort_order] == 'created_at_asc' + Ticket.by_is_open_and_created_at.startkey([(options[:open_status] == 'open'), 0]).endkey([(options[:open_status] == 'open'), Time.now + 2.days]) + else + Ticket.by_is_open_and_updated_at.startkey([(options[:open_status] == 'open'), 0]).endkey([(options[:open_status] == 'open'), Time.now + 2.days]).descending + end end else # only show tickets this user has commented on, as user is non-admin or admin viewing only their tickets if options[:open_status] == 'all' - Ticket.includes_post_by_and_updated_at.startkey([user.id, 0]).endkey([user.id, Time.now + 2.days]) + if options[:sort_order] == 'created_at_desc' + Ticket.includes_post_by_and_created_at.startkey([user.id, 0]).endkey([user.id, Time.now + 2.days]).descending + elsif options[:sort_order] == 'updated_at_asc' + Ticket.includes_post_by_and_updated_at.startkey([user.id, 0]).endkey([user.id, Time.now + 2.days]) + elsif options[:sort_order] == 'created_at_asc' + Ticket.includes_post_by_and_created_at.startkey([user.id, 0]).endkey([user.id, Time.now + 2.days]) + else + Ticket.includes_post_by_and_updated_at.startkey([user.id, 0]).endkey([user.id, Time.now + 2.days]).descending + end else - Ticket.includes_post_by_and_open_status_and_updated_at.startkey([user.id, (options[:open_status] == 'open'), 0]).endkey([user.id, (options[:open_status] == 'open'), Time.now + 2.days]) + if options[:sort_order] == 'created_at_desc' + Ticket.includes_post_by_and_open_status_and_created_at.startkey([user.id, (options[:open_status] == 'open'), 0]).endkey([user.id, (options[:open_status] == 'open'), Time.now + 2.days]).descending + elsif options[:sort_order] == 'updated_at_asc' + Ticket.includes_post_by_and_open_status_and_updated_at.startkey([user.id, (options[:open_status] == 'open'), 0]).endkey([user.id, (options[:open_status] == 'open'), Time.now + 2.days]) + elsif options[:sort_order] == 'created_at_asc' + Ticket.includes_post_by_and_open_status_and_created_at.startkey([user.id, (options[:open_status] == 'open'), 0]).endkey([user.id, (options[:open_status] == 'open'), Time.now + 2.days]) + else + Ticket.includes_post_by_and_open_status_and_updated_at.startkey([user.id, (options[:open_status] == 'open'), 0]).endkey([user.id, (options[:open_status] == 'open'), Time.now + 2.days]).descending + end end end end diff --git a/help/app/views/tickets/_admin-nav.html.haml b/help/app/views/tickets/_admin-nav.html.haml index 055485d..0e45c40 100644 --- a/help/app/views/tickets/_admin-nav.html.haml +++ b/help/app/views/tickets/_admin-nav.html.haml @@ -1,5 +1,5 @@ %ul.nav.nav-pills.nav-stacked %li{:class => ("active" if admin == 'mine')} - = link_to 'tickets i admin', {:admin_status => 'mine', :open_status => status} + = link_to 'tickets i admin', {:admin_status => 'mine', :open_status => status, :sort_order => order} %li{:class => ("active" if admin == 'all')} - = link_to 'all tickets', {:admin_status => 'all', :open_status => status} + = link_to 'all tickets', {:admin_status => 'all', :open_status => status, :sort_order => order} diff --git a/help/app/views/tickets/_order-nav.html.haml b/help/app/views/tickets/_order-nav.html.haml index 176acdc..9e8bcee 100644 --- a/help/app/views/tickets/_order-nav.html.haml +++ b/help/app/views/tickets/_order-nav.html.haml @@ -1,7 +1,5 @@ %ul.nav.nav-pills.pull-right - %li - = link_to 'created at' - %li{:class=> ("active" if true)} - = link_to do - %i.icon-arrow-up - updated at + %li{:class=> ("active" if order.start_with? 'created_at' )} + = link_to_order('created') + %li{:class=> ("active" if order.start_with? 'updated_at' )} + = link_to_order('updated') -- cgit v1.2.3