Module: KontoAPI::ActiveRecordExtension::ClassMethods

Defined in:
lib/kontoapi-rails/orm/active_record_extension.rb

Instance Method Summary collapse

Instance Method Details

#autocomplete_bank_name(options = {}) ⇒ Object



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
# File 'lib/kontoapi-rails/orm/active_record_extension.rb', line 20

def autocomplete_bank_name(options={})
  options.symbolize_keys!
  options.reverse_merge!(
    :bank_code_field  => :bank_code,
    :bank_name_field  => :bank_name,
    :always_overwrite => false
  )
  define_method :autocomplete_bank_name do
    current_value = send(:"#{options[:bank_name_field]}")
    blz           = send(:"#{options[:bank_code_field]}")
    blz_changed   = (respond_to?(:"#{options[:bank_code_field]}_changed?") && send(:"#{options[:bank_code_field]}_changed?")) || (respond_to?(:"encrypted_#{options[:bank_code_field]}_changed?") && send(:"encrypted_#{options[:bank_code_field]}_changed?"))
    begin
      self.send(:"#{options[:bank_name_field]}=", KontoAPI::bank_name(blz)) if (options[:always_overwrite] || current_value.blank?) && blz_changed
      return true
    rescue Timeout::Error => ex
      case options[:on_timeout]
      when String
        self.send(:"#{options[:bank_name_field]}=", options[:on_timeout])
      when nil, :ignore
        # nop
      when :retry
        raise 'not implemented yet'
      end
    end
  end
  before_save :autocomplete_bank_name
end

#validates_bank_account(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/kontoapi-rails/orm/active_record_extension.rb', line 9

def (options={})
  options.symbolize_keys!
  options.reverse_merge!(
    :account_number_field => :account_number,
    :bank_code_field      => :bank_code,
    :allow_nil            => true,
    :on_timeout           => :ignore
  )
  validates_with KontoAPI::BankAccountValidator, options
end

#validates_bic(field, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kontoapi-rails/orm/active_record_extension.rb', line 70

def validates_bic(field, options={})
  options.symbolize_keys!
  options.reverse_merge!( :allow_nil => true, :on_timeout => :ignore )
  define_method :bic_validation do
    value = send(field)
    return true if respond_to?(:"#{field}_changed?") && !send(:"#{field}_changed?")
    return true if respond_to?(:"encrypted_#{field}_changed?") && !send(:"encrypted_#{field}_changed?")
    return true if options[:allow_nil] && value.nil?
    begin
      errors.add(field, :invalid) unless KontoAPI::valid?( :bic => send(field) )
    rescue Timeout::Error => ex
      case options[:on_timeout]
      when :fail
        errors.add(field, :timeout)
      when :ignore
        # nop
      end
    end
  end
  validate :bic_validation
end

#validates_iban(field, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kontoapi-rails/orm/active_record_extension.rb', line 48

def validates_iban(field, options={})
  options.symbolize_keys!
  options.reverse_merge!( :allow_nil => true, :on_timeout => :ignore )
  define_method :iban_validation do
    value = send(field)
    return true if respond_to?(:"#{field}_changed?") && !send(:"#{field}_changed?")
    return true if respond_to?(:"encrypted_#{field}_changed?") && !send(:"encrypted_#{field}_changed?")
    return true if options[:allow_nil] && value.nil?
    begin
      errors.add(field, :invalid) unless KontoAPI::valid?( :iban => value )
    rescue Timeout::Error => ex
      case options[:on_timeout]
      when :fail
        errors.add(field, :timeout)
      when :ignore
        # nop
      end
    end
  end
  validate :iban_validation
end