summaryrefslogtreecommitdiff
path: root/engines/support/app/models/ticket.rb
diff options
context:
space:
mode:
authorazul <azul@riseup.net>2014-04-17 10:12:05 +0200
committerazul <azul@riseup.net>2014-04-17 10:12:05 +0200
commit3513ad74f950b113af1ba1e3d06bc6a55c48fde5 (patch)
treedb49ebd4428053d5c8d720275b77594a531a1ad1 /engines/support/app/models/ticket.rb
parentcb6442c344d6bdaf52c3878b2de2fcf4d85f2648 (diff)
parent3d3688647fab7049e5b531c45b85c1e46a1d528f (diff)
Merge pull request #146 from azul/refactor/engines
Refactor/engines
Diffstat (limited to 'engines/support/app/models/ticket.rb')
-rw-r--r--engines/support/app/models/ticket.rb105
1 files changed, 105 insertions, 0 deletions
diff --git a/engines/support/app/models/ticket.rb b/engines/support/app/models/ticket.rb
new file mode 100644
index 0000000..cd22758
--- /dev/null
+++ b/engines/support/app/models/ticket.rb
@@ -0,0 +1,105 @@
+#
+# 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
+ use_database "tickets"
+
+ 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 :subject, String
+ property :email, String
+ property :is_open, TrueClass, :default => true
+ property :comments, [TicketComment]
+
+ timestamps!
+
+ before_validation :set_email, :set_regarding_user, :on => :create
+
+ design do
+ view :by_updated_at
+ view :by_created_at
+ view :by_created_by
+
+ view :by_is_open_and_created_at
+ view :by_is_open_and_updated_at
+
+ own_path = Pathname.new(File.dirname(__FILE__))
+ load_views(own_path.join('..', 'designs', 'ticket'))
+ end
+
+ validates :subject, :presence => true
+ validates :email, :allow_blank => true, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/
+
+ def self.search(options = {})
+ @selection = TicketSelection.new(options)
+ @selection.tickets
+ end
+
+ def self.destroy_all_from(user)
+ self.by_created_by.key(user.id).each do |ticket|
+ ticket.destroy
+ end
+ end
+
+ def is_creator_validated?
+ !!created_by
+ end
+
+ def set_email
+ self.email = nil if self.email == ""
+ end
+
+ def set_regarding_user
+ self.regarding_user = nil if self.regarding_user == ""
+ end
+
+ def close
+ self.is_open = false
+ end
+
+ def reopen
+ self.is_open = true
+ end
+
+ def commenters
+ commenters = []
+ 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')
+ end
+ else
+ 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
+ comment = TicketComment.new(attributes.values.first)
+ comment.posted_at = Time.now
+ comments << comment
+ end
+ end
+
+ def created_by_user
+ User.find(self.created_by)
+ end
+
+ def regarding_user_actual_user
+ User.find_by_login(self.regarding_user)
+ end
+
+end