Created
March 29, 2014 05:24
-
-
Save jasoares/9848980 to your computer and use it in GitHub Desktop.
Interesting modular approach to callbacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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