Skip to content

Instantly share code, notes, and snippets.

@jasoares
Created March 29, 2014 05:24
Show Gist options
  • Select an option

  • Save jasoares/9848980 to your computer and use it in GitHub Desktop.

Select an option

Save jasoares/9848980 to your computer and use it in GitHub Desktop.
Interesting modular approach to callbacks
class BankAccount < ActiveRecord::Base
before_save EncryptionWrapper.new
after_save EncryptionWrapper.new
after_initialize EncryptionWrapper.new
end
class EncryptionWrapper
def before_save(record)
record.credit_card_number = encrypt(record.credit_card_number)
end
def after_save(record)
record.credit_card_number = decrypt(record.credit_card_number)
end
alias_method :after_initialize, :after_save
private
def encrypt(value)
# Secrecy is committed
end
def decrypt(value)
# Secrecy is unveiled
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment