Skip to content

Commit

Permalink
support for attr_encrypted fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmartin committed Oct 23, 2012
1 parent fa51770 commit 1bf5499
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 5 additions & 3 deletions lib/kontoapi-rails/orm/active_record_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def autocomplete_bank_name(options={})
define_method :autocomplete_bank_name do
current_value = send(:"#{options[:bank_name_field]}")
blz = send(:"#{options[:bank_code_field]}")
blz_changed = send(:"#{options[:bank_code_field]}_changed?")
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
Expand All @@ -50,7 +50,8 @@ def validates_iban(field, options={})
options.reverse_merge!( :allow_nil => true, :on_timeout => :ignore )
define_method :iban_validation do
value = send(field)
return true unless send(:"#{field}_changed?")
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 )
Expand All @@ -71,7 +72,8 @@ def validates_bic(field, options={})
options.reverse_merge!( :allow_nil => true, :on_timeout => :ignore )
define_method :bic_validation do
value = send(field)
return true unless send(:"#{field}_changed?")
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) )
Expand Down
10 changes: 9 additions & 1 deletion lib/kontoapi-rails/validators/bank_account_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class BankAccountValidator < ActiveModel::Validator
def validate(record)
account_number = record.send(:"#{options[:account_number_field]}")
bank_code = record.send(:"#{options[:bank_code_field]}")
return true unless record.send(:"#{options[:account_number_field]}_changed?") || record.send(:"#{options[:bank_code_field]}_changed?")
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?)
record.errors.add(:"#{options[:account_number_field]}", :invalid) unless KontoAPI::valid?( :ktn => account_number, :blz => bank_code )
rescue Timeout::Error => ex
Expand All @@ -20,5 +22,11 @@ def validate(record)
end
end

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

end
end

0 comments on commit 1bf5499

Please sign in to comment.