diff options
author | Azul <azul@leap.se> | 2012-12-20 10:57:34 +0100 |
---|---|---|
committer | Azul <azul@leap.se> | 2012-12-20 10:57:34 +0100 |
commit | 5d61ba0a80399b9e725881f857fdfa5414e5ef3f (patch) | |
tree | 041d1938f02b8aa27f68e90f8c71672ec89ce810 /help/app/models | |
parent | 69a6c34998328b2168053184e1e487b60e1cc536 (diff) | |
parent | d6c881294880a934424e4d399786561d5c107167 (diff) |
Merge remote-tracking branch 'origin/feature/tickets-refactor'
Diffstat (limited to 'help/app/models')
-rw-r--r-- | help/app/models/ticket.rb | 82 | ||||
-rw-r--r-- | help/app/models/ticket_selection.rb | 58 |
2 files changed, 77 insertions, 63 deletions
diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index 781216e..fa056b4 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 = {} @@ -139,65 +141,19 @@ 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 - - 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 - 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 - 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]) + options[:user_id] = user.id + options[:is_admin] = is_admin + + @selection = TicketSelection.new(options) + @selection.tickets 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 diff --git a/help/app/models/ticket_selection.rb b/help/app/models/ticket_selection.rb new file mode 100644 index 0000000..cb20f5b --- /dev/null +++ b/help/app/models/ticket_selection.rb @@ -0,0 +1,58 @@ +class TicketSelection + + def initialize(options = {}) + @options = options + @options[:open_status] ||= 'open' + @options[:sort_order] ||= 'updated_at_desc' + + end + + def tickets + #TODO: can this be more succinct? + if order + Ticket.send(finder_method).startkey(startkey).endkey(endkey).send(order) + else + Ticket.send(finder_method).startkey(startkey).endkey(endkey) + end + end + + protected + + + def finder_method + method = 'by_' + method += 'includes_post_by_and_' if only_mine? + method += 'is_open_and_' if @options[:open_status] != 'all' + method += @options[:sort_order].sub(/_(de|a)sc$/, '') + end + + def startkey + startkeys = [] + startkeys << @options[:user_id] if only_mine? + startkeys << (@options[:open_status] == 'open') if @options[:open_status] != 'all' + startkeys << 0 + startkeys = startkeys.join if startkeys.length == 1 #want string not array if just one thing in array + startkeys + end + + def endkey + endtime = Time.now + 2.days #TODO. this obviously isn't ideal + if self.startkey.is_a?(Array) + endkeys = self.startkey + endkeys.pop + endkeys << endtime + else + endtime + end + end + + def order + 'descending' if @options[:sort_order].end_with? 'desc' + end + + + def only_mine? + !@options[:is_admin] or (@options[:admin_status] == 'mine') + end + +end |