summaryrefslogtreecommitdiff
path: root/engines/support/app/models/ticket.rb
blob: cd2275821115808ac3f7e275e76f717ed50e3815 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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