diff options
Diffstat (limited to 'help/app/models')
-rw-r--r-- | help/app/models/ticket.rb | 106 | ||||
-rw-r--r-- | help/app/models/ticket_selection.rb | 41 |
2 files changed, 54 insertions, 93 deletions
diff --git a/help/app/models/ticket.rb b/help/app/models/ticket.rb index 738487a..8066d0d 100644 --- a/help/app/models/ticket.rb +++ b/help/app/models/ticket.rb @@ -1,49 +1,30 @@ +# +# 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: better validation of email +# +# TODO: don't hardcode strings 'unknown user' and 'unauthenticated user' +# class Ticket < CouchRest::Model::Base - #include ActiveModel::Validations - use_database "tickets" - #require 'securerandom' -=begin - title - created_at - updated_at - email_address - user - user_verified? - admins (list of admins who have commented on the ticket) - code (secret url) -=end - - #belongs_to :user #from leap_web_users. doesn't necessarily belong to a user though - property :created_by, String, :protected => true #Integer #nil unless user was authenticated for ticket creation, #THIS should not be changed after being set - property :regarding_user, String#Integer # form cannot be submitted if they type in a username w/out corresponding ID. this field can be nil. for authenticated ticket creation by non-admins, should this just automatically be set to be same as created_by? or maybe we don't use this field unless created_by is nil? - #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 - property :is_open, TrueClass, :default => true - property :comments, [TicketComment] + + property :created_by, String, :protected => true # nil for anonymous tickets, should never be changed + property :regarding_user, String # may be nil or valid username + property :title, String + property :email, String + property :is_open, TrueClass, :default => true + property :comments, [TicketComment] timestamps! - #before_validation :set_created_by, :set_code, :set_email, :on => :create before_validation :set_email, :set_regarding_user, :on => :create - - #named_scope :open, :conditions => {:is_open => true} #?? - design do - #TODO--clean this all up - #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_at view :by_is_open_and_updated_at @@ -52,63 +33,31 @@ class Ticket < CouchRest::Model::Base end validates :title, :presence => true - #validates :comments, :presence => true #do we want it like this? - - - # 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, :allow_blank => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/ - #TODO: - #def set_created_by - # self.created_by = User.current if User.current - #end - - def self.for_user(user, options = {}, is_admin = false) - - # 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...) - - options[:user_id] = user.id - options[:is_admin] = is_admin - + def self.search(options = {}) @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 -=begin - def set_code #let's not use this---can use same show url - # ruby 1.9 provides url-safe option---this is not necessarily url-safe - self.code = SecureRandom.hex(8) if !is_creator_validated? - end -=end - - def set_email self.email = nil if self.email == "" - # in controller set to be current users email if that exists end def set_regarding_user self.regarding_user = nil if self.regarding_user == "" end - #not saving with close and reopen, as we will save in update when they are called. - #TODO: not sure if we should bother with these: def close self.is_open = false - #save end def reopen self.is_open = true - #save end def commenters @@ -118,20 +67,21 @@ class Ticket < CouchRest::Model::Base 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') 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') end end commenters.join(', ') end + # + # update comments. User should be set by controller. + # def comments_attributes=(attributes) - if attributes # could be empty as we will empty if nothing was typed in - comment = TicketComment.new(attributes.values.first) #TicketComment.new(attributes) - #comment.posted_by = User.current.id if User.current #we want to avoid User.current, and current_user won't work here. instead will set in tickets_controller - # what about: comment.posted_by = self.updated_by (will need to add ticket.updated_by) + if attributes + comment = TicketComment.new(attributes.values.first) comment.posted_at = Time.now comments << comment end @@ -144,11 +94,5 @@ class Ticket < CouchRest::Model::Base def regarding_user_actual_user User.find_by_login(self.regarding_user) end -=begin - def validate - if email_address and not email_address.strip =~ RFC822::EmailAddress - errors.add 'email', 'contains an invalid address' - end - end -=end + end diff --git a/help/app/models/ticket_selection.rb b/help/app/models/ticket_selection.rb index bebe5fc..74d5b78 100644 --- a/help/app/models/ticket_selection.rb +++ b/help/app/models/ticket_selection.rb @@ -1,10 +1,20 @@ class TicketSelection + # + # supported options: + # + # user_id: id of the user (uuid string) + # open_status: open | closed | all + # sort_order: updated_at_desc | updated_at_asc | created_at_desc | created_at_asc + # admin_status: mine | all + # is_admin: true | false + # def initialize(options = {}) - @options = options - @options[:open_status] ||= 'open' - @options[:sort_order] ||= 'updated_at_desc' - + @user_id = options[:user_id].gsub /[^a-z0-9]/, '' + @open_status = allow options[:open_status], 'open', 'closed', 'all' + @sort_order = allow options[:sort_order], 'updated_at_desc', 'updated_at_asc', 'created_at_desc', 'created_at_asc' + @admin_status = allow options[:admin_status], 'mine', 'all' + @is_admin = allow options[:is_admin], false, true end def tickets @@ -13,25 +23,32 @@ class TicketSelection protected + def allow(source, *allowed) + if allowed.include?(source) + source + else + allowed.first + end + end 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$/, '') + method += 'is_open_and_' if @open_status != 'all' + method += @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 << @user_id if only_mine? + startkeys << (@open_status == 'open') if @open_status != 'all' startkeys << 0 - startkeys = startkeys.join if startkeys.length == 1 #want string not array if just one thing in array + 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 + endtime = Time.now + 2.days # TODO. this obviously isn't ideal if self.startkey.is_a?(Array) endkeys = self.startkey endkeys.pop @@ -43,12 +60,12 @@ class TicketSelection def order # we have defined the ascending method to return the view itself: - (@options[:sort_order].end_with? 'desc') ? 'descending' : 'ascending' + (@sort_order.end_with? 'desc') ? 'descending' : 'ascending' end def only_mine? - !@options[:is_admin] or (@options[:admin_status] == 'mine') + !@is_admin || @admin_status == 'mine' end end |