Class: KontoAPI::BankAccountValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/kontoapi-rails/validators/bank_account_validator.rb

Instance Method Summary collapse

Instance Method Details

#change_method(record, field) ⇒ Object



25
26
27
28
29
# File 'lib/kontoapi-rails/validators/bank_account_validator.rb', line 25

def change_method(record, field)
  ["#{field}_changed?", "encrypted_#{field}_changed?"].each do |m|
    return m.to_sym if record.respond_to?( m.to_sym )
  end
end

#validate(record) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kontoapi-rails/validators/bank_account_validator.rb', line 6

def validate(record)
    = record.send(:"#{options[:account_number_field]}")
  bank_code       = record.send(:"#{options[:bank_code_field]}")
   = record.send( change_method(record, options[:account_number_field]) )
  bank_code_changed      = record.send( change_method(record, options[:bank_code_field]) )
  return true unless  || bank_code_changed
  return true if options[:allow_nil] && (.nil? || bank_code.nil?)
  record.errors.add(:"#{options[:account_number_field]}", :invalid) unless KontoAPI::valid?( :ktn => , :blz => bank_code )
rescue Timeout::Error => ex
  case options[:on_timeout]
  when :fail
    record.errors.add(:"#{options[:account_number_field]}", :timeout)
  when :ignore
    # nop
  when :retry
    raise 'not implemented yet'
  end
end