From 3851be25b5667d3a6b84a3bfefb7fb34870f5296 Mon Sep 17 00:00:00 2001 From: Jan Schwenzien Date: Sun, 16 Jun 2013 07:34:10 +0200 Subject: [PATCH] added badge, reduced complexity --- README.markdown | 3 +++ .../validators/bank_account_validator.rb | 12 ++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.markdown b/README.markdown index 2f682a4..6e86525 100644 --- a/README.markdown +++ b/README.markdown @@ -1,6 +1,9 @@ Konto API Rails Validator ========================= +[![Gem Version](https://badge.fury.io/rb/kontoapi-rails.png)](http://badge.fury.io/rb/kontoapi-rails) + + This library is a wrapper for the Konto API (https://www.kontoapi.de/). It provides a validation method for ActiveRecord models that checks if a given account number and bank code represent a valid combination. diff --git a/lib/kontoapi-rails/validators/bank_account_validator.rb b/lib/kontoapi-rails/validators/bank_account_validator.rb index 1a36b6e..1689357 100644 --- a/lib/kontoapi-rails/validators/bank_account_validator.rb +++ b/lib/kontoapi-rails/validators/bank_account_validator.rb @@ -6,10 +6,7 @@ class BankAccountValidator < ActiveModel::Validator def validate(record) account_number = record.send(:"#{options[:account_number_field]}") bank_code = record.send(:"#{options[:bank_code_field]}") - account_number_changed = record.send( change_method(record, options[:account_number_field]) ) - bank_code_changed = record.send( change_method(record, options[:bank_code_field]) ) - return true unless account_number_changed || bank_code_changed - return true if options[:allow_nil] && (account_number.nil? || bank_code.nil?) + return true if stop?(record, account_number, bank_code, options) record.errors.add(:"#{options[:account_number_field]}", :invalid) unless KontoAPI::valid?( :ktn => account_number, :blz => bank_code ) rescue Timeout::Error => ex case options[:on_timeout] @@ -28,5 +25,12 @@ def change_method(record, field) end end + def stop?(record, account_number, bank_code, options) + account_number_changed = record.send( change_method(record, options[:account_number_field]) ) + bank_code_changed = record.send( change_method(record, options[:bank_code_field]) ) + return true unless account_number_changed || bank_code_changed + return true if options[:allow_nil] && (account_number.nil? || bank_code.nil?) + end + end end \ No newline at end of file