summaryrefslogtreecommitdiff
path: root/engines/support/app/models/ticket.rb
diff options
context:
space:
mode:
Diffstat (limited to 'engines/support/app/models/ticket.rb')
-rw-r--r--engines/support/app/models/ticket.rb43
1 files changed, 29 insertions, 14 deletions
diff --git a/engines/support/app/models/ticket.rb b/engines/support/app/models/ticket.rb
index 554fbd6..b1bdf8d 100644
--- a/engines/support/app/models/ticket.rb
+++ b/engines/support/app/models/ticket.rb
@@ -3,10 +3,10 @@
# 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'
#
+# TODO: this should use associations instead of non-standard created_by.
+#
class Ticket < CouchRest::Model::Base
use_database "tickets"
@@ -19,6 +19,8 @@ class Ticket < CouchRest::Model::Base
timestamps!
+ unique_id :generate_code
+
design do
view :by_updated_at
view :by_created_at
@@ -33,13 +35,11 @@ class Ticket < CouchRest::Model::Base
validates :subject, :presence => true
- # email can have three states:
- # * nil - prefilled with created_by's email
- # * "" - cleared
- # * valid email address
- validates :email, :allow_blank => true, :format => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/
-
- # validates :comments, presence: true
+ # email can be nil, "", or valid address.
+ # validation provided by 'valid_email' gem.
+ validates :email, :allow_blank => true,
+ :email => true,
+ :mx_with_fallback => true
def self.search(options = {})
@selection = TicketSelection.new(options)
@@ -74,17 +74,21 @@ class Ticket < CouchRest::Model::Base
self.is_open = true
end
- def commenters
+ def commenters(locale = I18n.locale)
commenters = []
+ unknown = false
+ anonymous = false
self.comments.each do |comment|
if 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')
+ elsif !unknown
+ unknown = true
+ commenters << I18n.t(:unknown, :locale => locale)
end
- else
- commenters << 'unauthenticated user' if !commenters.include?('unauthenticated user')
+ elsif !anonymous
+ anonymous = true
+ commenters << I18n.t(:anonymous, :locale => locale)
end
end
commenters.join(', ')
@@ -113,4 +117,15 @@ class Ticket < CouchRest::Model::Base
User.find_by_login(self.regarding_user)
end
+ # Generate a unique code for identifying this ticket.
+ # This will become the ID of the document. It is also used for
+ # tracking email replies. The code must be URL friendly.
+ def generate_code
+ while code = SecureRandom.urlsafe_base64.downcase.gsub(/[li1o0_-]/,'')[0..7]
+ if code.length == 8 && self.class.find(code).nil?
+ return code
+ end
+ end
+ end
+
end