Skip to content

Commit

Permalink
more documentation, more options
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmartin committed Apr 20, 2011
1 parent 85888fe commit 7959e8a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
23 changes: 22 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------

Expand Down
23 changes: 20 additions & 3 deletions lib/kontoapi-rails/orm/active_record_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,37 @@ 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

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
Expand Down
19 changes: 7 additions & 12 deletions lib/kontoapi-rails/validators/bank_account_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7959e8a

Please sign in to comment.