From 648eb5ab584b4176e63114512464dcc7f9cc812d Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 28 Nov 2012 10:46:26 +0100 Subject: [PATCH] Load the ticket number generator at runtime (to support generator change via config at runtime). --- app/models/ticket.rb | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/app/models/ticket.rb b/app/models/ticket.rb index e9e47242b..f8d6a6a3f 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -16,26 +16,8 @@ class Ticket < ApplicationModel after_update :cache_delete after_destroy :cache_delete - @@number_adapter = nil - - def number_adapter - return @@number_adapter - end - - def number_adapter=(adapter_name) - return @@number_adapter if @@number_adapter - case adapter_name - when Symbol, String - require "ticket/number/#{adapter_name.to_s.downcase}" - @@number_adapter = Ticket::Number.const_get("#{adapter_name.to_s.capitalize}") - else - raise "Missing number_adapter '#{adapter_name}'" - end - end - def self.number_check (string) - Ticket.new.number_adapter = Setting.get('ticket_number') - @@number_adapter.number_check_item(string) + number_adapter.number_check_item(string) end def agent_of_group @@ -384,13 +366,30 @@ class Ticket < ApplicationModel end private + + def number_adapter + + # load backend based on config + adapter_name = Setting.get('ticket_number') + adapter = nil + case adapter_name + when Symbol, String + require "ticket/number/#{adapter_name.to_s.downcase}" + adapter = Ticket::Number.const_get("#{adapter_name.to_s.capitalize}") + else + raise "Missing number_adapter '#{adapter_name}'" + end + return adapter + end + def number_generate - Ticket.new.number_adapter = Setting.get('ticket_number') + + # generate number (1..25_000).each do |i| - number = @@number_adapter.number_generate_item() + number = number_adapter.number_generate_item() ticket = Ticket.where( :number => number ).first if ticket != nil - number = @@number_adapter.number_generate_item() + number = number_adapter.number_generate_item() else self.number = number return number