summaryrefslogtreecommitdiff
path: root/engines/support/app/models
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2015-04-30 00:32:33 -0700
committerelijah <elijah@riseup.net>2015-04-30 00:32:33 -0700
commit63871baf6061668b162972193c55b5a8f7490797 (patch)
treeca8cd5fbab18cbe59b728a123f450140ed98f519 /engines/support/app/models
parentc3b133cb6f02003ab934e5008e108f489ace4158 (diff)
added support for email notifications of ticket changes
Diffstat (limited to 'engines/support/app/models')
-rw-r--r--engines/support/app/models/ticket.rb43
-rw-r--r--engines/support/app/models/ticket_comment.rb40
2 files changed, 41 insertions, 42 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
diff --git a/engines/support/app/models/ticket_comment.rb b/engines/support/app/models/ticket_comment.rb
index 2c5df41..1c04a1f 100644
--- a/engines/support/app/models/ticket_comment.rb
+++ b/engines/support/app/models/ticket_comment.rb
@@ -1,22 +1,17 @@
+#
+# elijah: the property 'posted_by' should have been a belongs_to() association
+# or at least 'posted_by_id', but I am leaving it as is because I am
+# not sure how to change it.
+#
class TicketComment
include CouchRest::Model::Embeddable
- #belongs_to :ticket #is this best way to do it? will want to access all of a tickets comments, so maybe this isn't the way?
- property :posted_by, String#, :protected => true #Integer#this should be current_user if that is set, meaning the user is logged in #cannot have it be protected and set via comments_attributes=. also, if it is protected and we set in the tickets_controller, it gets unset. TODO---is this okay to have it not protected and manually check it? We do not users to be able to set this.
- # if the current user is not set, then we could just say the comment comes from an 'unauthenticated user', which would be somebody with the secret URL
- property :posted_at, Time#, :protected => true
- #property :posted_verified, TrueClass, :protected => true #should be true if current_user is set when the comment is created
+ property :posted_by, String
+ property :posted_at, Time
property :body, String
- property :private, TrueClass # private comments are only viewable by admins #this is checked when set, to make sure it was set by an admin
+ property :private, TrueClass
- # ? timestamps!
validates :body, :presence => true
- #before_validation :set_time#, :set_posted_by
-
- #design do
- # view :by_posted_at
- # view :by_body
- #end
# translations are in the same scope as those of a "proper" couchrest model
def self.i18n_scope
@@ -28,21 +23,10 @@ class TicketComment
end
def posted_by_user
- User.find(posted_by) if posted_by
- end
-
-=begin
- #TODO.
- #this is resetting all comments associated with the ticket:
- def set_time
- self.posted_at = Time.now
- end
-=end
-
-=begin
- def set_posted_by
- self.posted_by = User.current if User.current
+ if posted_by
+ @_posted_by_user ||= User.find(posted_by)
+ end
end
-=end
+ alias user posted_by_user
end