diff --git a/lib/kontoapi-rails/orm/active_record_extension.rb b/lib/kontoapi-rails/orm/active_record_extension.rb index 82b9454..c53cd53 100644 --- a/lib/kontoapi-rails/orm/active_record_extension.rb +++ b/lib/kontoapi-rails/orm/active_record_extension.rb @@ -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 @@ -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 ) @@ -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) ) diff --git a/lib/kontoapi-rails/validators/bank_account_validator.rb b/lib/kontoapi-rails/validators/bank_account_validator.rb index 0b7ee06..1a36b6e 100644 --- a/lib/kontoapi-rails/validators/bank_account_validator.rb +++ b/lib/kontoapi-rails/validators/bank_account_validator.rb @@ -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 @@ -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 \ No newline at end of file