summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2012-11-27 12:37:18 +0100
committerAzul <azul@leap.se>2012-11-27 12:58:23 +0100
commitebf5dddc39f09bac6640084edbaaefa9c9d48477 (patch)
treea5b31470f97c32eded6601a5389fe3a1b28b30d0
parentbc432bb9b266524fd97cfb8030987c6a3e8f126e (diff)
moved ticket fetching logic from controller to model
-rw-r--r--help/app/controllers/tickets_controller.rb43
-rw-r--r--help/app/models/ticket.rb71
2 files changed, 60 insertions, 54 deletions
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
diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb
index e8b004f..bc6ea59 100644
--- a/help/app/models/ticket.rb
+++ b/help/app/models/ticket.rb
@@ -20,7 +20,7 @@ class Ticket < CouchRest::Model::Base
#also, both created_by and regarding_user could be nil---say user forgets username, or has general question
property :title, String
property :email, String #verify
-
+
#property :user_verified, TrueClass, :default => false #will be true exactly when user is set
#admins
#property :code, String, :protected => true # only should be set if created_by is nil #instead we will just use couchdb ID
@@ -28,7 +28,7 @@ class Ticket < CouchRest::Model::Base
property :comments, [TicketComment]
timestamps!
-
+
#before_validation :set_created_by, :set_code, :set_email, :on => :create
before_validation :set_email, :on => :create
@@ -39,24 +39,24 @@ class Ticket < CouchRest::Model::Base
#TODO--clean this all up
view :by_is_open
view :by_created_by
-
+
view :by_updated_at #
view :by_title, #test
- :map =>
+ :map =>
"function(doc) {
emit(doc._id, doc);
}"
view :by_is_open_and_created_by
view :by_updated_at_and_is_open,
- :map =>
+ :map =>
"function(doc) {
if (doc['type'] == 'Ticket' && doc.is_open == true) {
emit(doc.updated_at, doc);
}
}"
view :by_updated_at_and_is_closed,
- :map =>
+ :map =>
"function(doc) {
if (doc['type'] == 'Ticket' && doc.is_open == false) {
emit(doc.updated_at, doc);
@@ -71,12 +71,57 @@ class Ticket < CouchRest::Model::Base
# html5 has built-in validation which isn't ideal, as it says 'please enter an email address' for invalid email addresses, which implies an email address is required, and it is not.
validates :email, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/, :if => :email #email address is optional
-
+
#TODO:
#def set_created_by
# self.created_by = User.current if User.current
#end
-
+
+ def self.for_user(user, options)
+ if options[:open_status] == 'open'
+ Ticket.by_is_open_and_created_by.key([true, user.id])
+ elsif options[:open_status] == 'closed'
+ Ticket.by_is_open_and_created_by.key([false, user.id])
+ else
+ Ticket.by_created_by(:key => user.id)
+ end
+ #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.
+ # 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}
+ end
+
+ def self.for_admin(user, options)
+ if options[:admin_status] == 'mine'
+ self.tickets_by_admin(user.id) #returns Array so pagination does not work
+ elsif options[:open_status] == 'open'
+ Ticket.by_updated_at_and_is_open
+ # Ticket.by_is_open.key(true) #returns CouchRest::Model::Designs::View
+ elsif options[:open_status] == 'closed'
+ Ticket.by_updated_at_and_is_closed
+ # Ticket.by_is_open.key(false) #returns CouchRest::Model::Designs::View
+ else
+ # Ticket.all #returns CouchRest::Model::Designs::View
+ Ticket.by_updated_at
+ end
+ end
+
+ #returns Array which doesn't work for pagination, as it is now.
+ def self.tickets_by_admin(id)
+ 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 is_creator_validated?
!!created_by
end
@@ -105,17 +150,17 @@ class Ticket < CouchRest::Model::Base
#save
end
- def commenters
+ def commenters
commenters = []
self.comments.each do |comment|
if comment.posted_by
- if user = User.find(comment.posted_by)
+ if user = User.find(comment.posted_by)
commenters << user.login if user and !commenters.include?(user.login)
else
- commenters << 'unknown user' if !commenters.include?('unknown user') #todo don't hardcode string 'unknown user'
+ commenters << 'unknown user' if !commenters.include?('unknown user') #todo don't hardcode string 'unknown user'
end
else
- commenters << 'unauthenticated user' if !commenters.include?('unauthenticated user') #todo don't hardcode string 'unauthenticated user'
+ commenters << 'unauthenticated user' if !commenters.include?('unauthenticated user') #todo don't hardcode string 'unauthenticated user'
end
end
commenters.join(', ')
@@ -137,5 +182,5 @@ class Ticket < CouchRest::Model::Base
errors.add 'email', 'contains an invalid address'
end
end
-=end
+=end
end