diff --git a/README.markdown b/README.markdown index 1aee7f6..f1fa5c3 100644 --- a/README.markdown +++ b/README.markdown @@ -30,17 +30,38 @@ Then, in one of the models you want to validate bank account data with: # account data validation # (really just a shortcut for `validates_with KontoAPI::BankAccountValidator`) - # takes any of the following options (the defaults are shown here): + # Takes any of the following options (the defaults are shown here): # :account_number_field => :account_number, # :bank_code_field => :bank_code, # :allow_nil => true, <-- don't validate if one of them is nil # :on_timeout => :ignore <-- do nothing if a timeout occurs, others: # :fail <-- throw a validation error # :retry <-- (not supported yet) + # + # If validations fails, an error within the standard ActiveRecord I18n scopes + # will be added to the :account_number_field: + # :invalid <-- if it is invalid + # :timeout <-- if :on_timeout is set to :fail and the api call timed out validates_account_data end +And if you want to autocomplete the bank name: + + class PaymentData < ActiveRecord::Base + + # bank name autocompletion + # takes any of the following options (the defaults are shown here): + # :bank_code_field => :bank_code, + # :bank_name_field => :bank_name, + # :always_overwrite => false, <-- autocomplete even if bank name already present + # :on_timeout => :ignore <-- do nothing if a timeout occurs, others: + # :retry <-- (not supported yet) + # 'any string' <-- use this string as the value + autocomplete_bank_name + + end + Copyright --------- diff --git a/lib/kontoapi-rails/orm/active_record_extension.rb b/lib/kontoapi-rails/orm/active_record_extension.rb index 8bd44b6..32b9ea0 100644 --- a/lib/kontoapi-rails/orm/active_record_extension.rb +++ b/lib/kontoapi-rails/orm/active_record_extension.rb @@ -8,6 +8,12 @@ def self.included(base) module ClassMethods def validates_bank_account(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 @@ -15,13 +21,24 @@ def autocomplete_bank_name(options={}) options.symbolize_keys! options.reverse_merge!( :bank_code_field => :bank_code, - :bank_name_field => :bank_name + :bank_name_field => :bank_name, + :always_overwrite => false ) - #write_inheritable_attribute(:autocomplete_bank_name_options, options) define_method :autocomplete_bank_name do current_value = send(:"#{options[:bank_name_field]}") blz = send(:"#{options[:bank_code_field]}") - self.send(:"#{options[:bank_name_field]}=", KontoAPI::bank_name(blz)) if current_value.blank? && blz.present? + begin + self.send(:"#{options[:bank_name_field]}=", KontoAPI::bank_name(blz)) if (options[:always_overwrite] || current_value.blank?) && blz.present? + 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 diff --git a/lib/kontoapi-rails/validators/bank_account_validator.rb b/lib/kontoapi-rails/validators/bank_account_validator.rb index 03c4508..cd78b04 100644 --- a/lib/kontoapi-rails/validators/bank_account_validator.rb +++ b/lib/kontoapi-rails/validators/bank_account_validator.rb @@ -3,22 +3,17 @@ module KontoAPI class BankAccountValidator < ActiveModel::Validator - DEFAULTS = { - :account_number_field => :account_number, - :bank_code_field => :bank_code, - :allow_nil => true, - :on_timeout => :ignore - } + DEFAULTS = def validate(record) - record_options = options.reverse_merge(DEFAULTS) - account_number = record.send(:"#{record_options[:account_number_field]}") - bank_code = record.send(:"#{record_options[:bank_code_field]}") - record.errors.add(:"#{record_options[:account_number_field]}", :invalid) unless KontoAPI::valid?(account_number, bank_code) + account_number = record.send(:"#{options[:account_number_field]}") + bank_code = record.send(:"#{options[:bank_code_field]}") + return true if options[:allow_nil] && (account_number.nil? || bank_code.nil?) + record.errors.add(:"#{options[:account_number_field]}", :invalid) unless KontoAPI::valid?(account_number, bank_code) rescue Timeout::Error => ex - case record_options[:on_timeout] + case options[:on_timeout] when :fail - record.errors.add(:"#{record_options[:account_number_field]}", :timeout) + record.errors.add(:"#{options[:account_number_field]}", :timeout) when :ignore # nop when :retry