number.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class Ticket::Number
  3. include ApplicationLib
  4. =begin
  5. generate new ticket number
  6. result = Ticket::Number.generate
  7. returns
  8. result = "1234556" # new ticket number
  9. =end
  10. def self.generate
  11. # generate number
  12. (1..50_000).each { |i|
  13. number = adapter.generate
  14. ticket = Ticket.where( :number => number ).first
  15. return number if !ticket
  16. }
  17. raise "Can't generate new ticket number!"
  18. end
  19. =begin
  20. check if string contrains a valid ticket number
  21. result = Ticket::Number.check('some string [Ticket#123456]')
  22. returns
  23. result = ticket # Ticket model of ticket with matching ticket number
  24. =end
  25. def self.check(string)
  26. adapter.check(string)
  27. end
  28. def self.adapter
  29. # load backend based on config
  30. adapter_name = Setting.get('ticket_number')
  31. if !adapter_name
  32. raise "Missing ticket_number setting option"
  33. end
  34. adapter = load_adapter(adapter_name)
  35. if !adapter
  36. raise "Can't load ticket_number adapter '#{adapter_name}'"
  37. end
  38. adapter
  39. end
  40. end