Skip to content

Instantly share code, notes, and snippets.

@ariejan
Created November 6, 2012 10:55
Show Gist options
  • Save ariejan/4024019 to your computer and use it in GitHub Desktop.
Save ariejan/4024019 to your computer and use it in GitHub Desktop.
class BankAccount < ActiveRecord::Base
include AttributeEncryption
belongs_to :billable, polymorphic: true
encrypted_attribute :encrypted_number, hash: true
validates :number,
format: { with: /[pP]?\d*/ }
# encrypted_attribute overrides #number= in order
# to perform some encryption magic, hence a
# before_validation hook does not work to strip
# the account number from unwanted characters.
alias_method :orig_number=, :number=
def number=(value)
value = value.gsub(/[ -\.]/, '') unless value.nil?
send(:orig_number=, value)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment