summaryrefslogtreecommitdiff
path: root/help/app/models/ticket.rb
blob: 784d7ef399fb07d3f16fee3f11df6b2d6b2c245f (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
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, Integer #nil unless user was authenticated for ticket creation, #THIS should not be changed after being set
  property :regarding_user, 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
  property :is_open, TrueClass, :default => true
  property :comments, [TicketComment]

  timestamps!
  
  before_validation :set_created_by, :set_code, :on => :create

  design do
    view :by_title
  end


  validates :email, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/, :if => :email #email address is optional
  
  def set_created_by
    self.created_by = User.current if User.current
  end
  
  def is_creator_validated?
    !!created_by
  end

  def set_code
    # ruby 1.9 provides url-safe option---this is not necessarily url-safe
    self.code = SecureRandom.hex(8) if !is_creator_validated?
  end

  def close
    self.is_open = false
    save
  end

  def reopen
    self.is_open = true
    save
  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