From 66ce152d5124be52f31d51fc4171fd53ba3a915c Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 18 Dec 2012 13:45:49 -0800 Subject: Refactoring of code to filter/order tickets. --- help/app/models/ticket.rb | 80 ++++++++++++++--------------------------------- 1 file changed, 24 insertions(+), 56 deletions(-) (limited to 'help/app/models/ticket.rb') diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index 781216e..7192cb3 100644 --- a/help/app/models/ticket.rb +++ b/help/app/models/ticket.rb @@ -37,17 +37,19 @@ class Ticket < CouchRest::Model::Base design do #TODO--clean this all up - view :by_is_open - view :by_created_by + #view :by_is_open + #view :by_created_by view :by_updated_at view :by_created_at - view :by_is_open_and_created_by + #view :by_is_open_and_created_by view :by_is_open_and_created_at view :by_is_open_and_updated_at - view :includes_post_by, + + #TODO: This view is only used in tests--should we keep it? + view :by_includes_post_by, :map => "function(doc) { var arr = {} @@ -62,7 +64,7 @@ class Ticket < CouchRest::Model::Base } }", :reduce => "function(k,v,r) { return sum(v); }" - view :includes_post_by_and_open_status_and_updated_at, + view :by_includes_post_by_and_is_open_and_updated_at, :map => "function(doc) { var arr = {} @@ -77,7 +79,7 @@ class Ticket < CouchRest::Model::Base } }", :reduce => "function(k,v,r) { return sum(v); }" - view :includes_post_by_and_open_status_and_created_at, + view :by_includes_post_by_and_is_open_and_created_at, :map => "function(doc) { var arr = {} @@ -92,7 +94,7 @@ class Ticket < CouchRest::Model::Base } }", :reduce => "function(k,v,r) { return sum(v); }" - view :includes_post_by_and_updated_at, + view :by_includes_post_by_and_updated_at, :map => "function(doc) { var arr = {} @@ -108,7 +110,7 @@ class Ticket < CouchRest::Model::Base }", :reduce => "function(k,v,r) { return sum(v); }" - view :includes_post_by_and_created_at, + view :by_includes_post_by_and_created_at, :map => "function(doc) { var arr = {} @@ -144,60 +146,26 @@ class Ticket < CouchRest::Model::Base # 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 + options[:sort_order] = 'updated_at_desc' if !options[:sort_order] #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' - 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 - 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 + options[:user_id] = user.id + options[:is_admin] = is_admin + + @selection = TicketSelection.new(options) + + #TODO: can this be more succinct? + if @selection.order + @tickets = Ticket.send(@selection.finder_method).startkey(@selection.startkey).endkey(@selection.endkey).send(@selection.order) 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' - 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 - 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 + @tickets = Ticket.send(@selection.finder_method).startkey(@selection.startkey).endkey(@selection.endkey) end - end - def self.tickets_by_commenter(user_id)#, options = {}) - Ticket.includes_post_by_and_updated_at.startkey([user_id, 0]).endkey([user_id, Time.now]) end + #def self.tickets_by_commenter(user_id)#, options = {}) + # Ticket.includes_post_by_and_updated_at.startkey([user_id, 0]).endkey([user_id, Time.now]) + #end + def is_creator_validated? !!created_by end -- cgit v1.2.3 From d6c881294880a934424e4d399786561d5c107167 Mon Sep 17 00:00:00 2001 From: jessib Date: Wed, 19 Dec 2012 11:13:50 -0800 Subject: Some cleanup of code --- help/app/models/ticket.rb | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) (limited to 'help/app/models/ticket.rb') diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index 7192cb3..fa056b4 100644 --- a/help/app/models/ticket.rb +++ b/help/app/models/ticket.rb @@ -141,25 +141,13 @@ class Ticket < CouchRest::Model::Base def self.for_user(user, options = {}, is_admin = false) - # 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 - options[:sort_order] = 'updated_at_desc' if !options[:sort_order] #hacky. redo this when handling defaults correctly options[:user_id] = user.id options[:is_admin] = is_admin @selection = TicketSelection.new(options) - - #TODO: can this be more succinct? - if @selection.order - @tickets = Ticket.send(@selection.finder_method).startkey(@selection.startkey).endkey(@selection.endkey).send(@selection.order) - else - @tickets = Ticket.send(@selection.finder_method).startkey(@selection.startkey).endkey(@selection.endkey) - end - + @selection.tickets end #def self.tickets_by_commenter(user_id)#, options = {}) -- cgit v1.2.3