summaryrefslogtreecommitdiff
path: root/app/models/fax_account.rb
blob: 683447a0f3404cc769e2c6c521b4e3031157f1b7 (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
# encoding: UTF-8

class FaxAccount < ActiveRecord::Base
  attr_accessible :name, :email, :station_id, :days_till_auto_delete, :phone_numbers_attributes, :retries

  # Validations:
  #
  validates_presence_of :fax_accountable_type, :fax_accountable_id
  validates_presence_of :fax_accountable
  validates_presence_of :name
  validates_presence_of :tenant_id
  validates_presence_of :tenant
  
  validates_numericality_of :days_till_auto_delete, :allow_nil => true
  validates_numericality_of :retries, :only_integer => true, :greater_than_or_equal_to => 0
  
  validates_uniqueness_of :name, :scope => [:fax_accountable_type, :fax_accountable_id]
    
  # Associations:
  #
  has_many :phone_numbers, :as => :phone_numberable, :dependent => :destroy
  has_many :fax_documents, :dependent => :destroy
  
  belongs_to :fax_accountable, :polymorphic => true
  belongs_to :tenant

  accepts_nested_attributes_for :phone_numbers
  
  # Hooks
  #
  before_validation :find_and_set_tenant_id
  before_validation :convert_umlauts
  
  def to_s
    name
  end
  
  private
  def require_at_least_one_phone_number
    if self.phone_numbers.count < 1
      errors.add(:base, 'needs at least one valid phone number')
    end  
  end
  
  def find_and_set_tenant_id
      if self.new_record? and self.tenant_id != nil
        return        
      else
        tenant = case self.fax_accountable_type
      	  when 'UserGroup' ; fax_accountable.tenant
      	  when 'User'      ; fax_accountable.current_tenant || fax_accountable.tenants.last
      	  else nil
      	end
        self.tenant_id = tenant.id if tenant != nil
      end
  end

  def convert_umlauts
    self.name = self.name.sub(/ä/,'ae').
                            sub(/Ä/,'Ae').
                            sub(/ü/,'ue').
                            sub(/Ü/,'Ue').
                            sub(/ö/,'oe').
                            sub(/Ö/,'Oe').
                            sub(/ß/,'ss')
    self.name = self.name.gsub(/[^a-zA-Z0-9\-\,\:\.\+ ]/,'_')
    self.station_id = self.station_id.sub(/ä/,'ae').
                            sub(/Ä/,'Ae').
                            sub(/ü/,'ue').
                            sub(/Ü/,'Ue').
                            sub(/ö/,'oe').
                            sub(/Ö/,'Oe').
                            sub(/ß/,'ss')
    self.station_id = self.station_id.gsub(/[^a-zA-Z0-9\-\,\:\.\+ ]/,'_')
  end
  
end