Skip to content
This repository
Browse code

Fix AR callbacks sample code to actually make sense

  • Loading branch information...
commit dedb9d85d8dc404020d5fcb17e6d0faac5d4b5f0 1 parent 98f1c4c
Mike Gunderloy ffmike authored

Showing 1 changed file with 33 additions and 5 deletions. Show diff stats Hide diff stats

  1. +33 5 activerecord/lib/active_record/callbacks.rb
38 activerecord/lib/active_record/callbacks.rb
@@ -104,6 +104,37 @@ module ActiveRecord
104 104 # The callback objects have methods named after the callback called with the record as the only parameter, such as:
105 105 #
106 106 # class BankAccount < ActiveRecord::Base
  107 + # before_save EncryptionWrapper.new
  108 + # after_save EncryptionWrapper.new
  109 + # after_initialize EncryptionWrapper.new
  110 + # end
  111 + #
  112 + # class EncryptionWrapper
  113 + # def before_save(record)
  114 + # record.credit_card_number = encrypt(record.credit_card_number)
  115 + # end
  116 + #
  117 + # def after_save(record)
  118 + # record.credit_card_number = decrypt(record.credit_card_number)
  119 + # end
  120 + #
  121 + # alias_method :after_find, :after_save
  122 + #
  123 + # private
  124 + # def encrypt(value)
  125 + # # Secrecy is committed
  126 + # end
  127 + #
  128 + # def decrypt(value)
  129 + # # Secrecy is unveiled
  130 + # end
  131 + # end
  132 + #
  133 + # So you specify the object you want messaged on a given callback. When that callback is triggered, the object has
  134 + # a method by the name of the callback messaged. You can make these callbacks more flexible by passing in other
  135 + # initialization data such as the name of the attribute to work with:
  136 + #
  137 + # class BankAccount < ActiveRecord::Base
107 138 # before_save EncryptionWrapper.new("credit_card_number")
108 139 # after_save EncryptionWrapper.new("credit_card_number")
109 140 # after_initialize EncryptionWrapper.new("credit_card_number")
@@ -115,11 +146,11 @@ module ActiveRecord
115 146 # end
116 147 #
117 148 # def before_save(record)
118   - # record.credit_card_number = encrypt(record.credit_card_number)
  149 + # record.send("#{@attribute}=", encrypt(record.send("#{@attribute}")))
119 150 # end
120 151 #
121 152 # def after_save(record)
122   - # record.credit_card_number = decrypt(record.credit_card_number)
  153 + # record.send("#{@attribute}=", decrypt(record.send("#{@attribute}")))
123 154 # end
124 155 #
125 156 # alias_method :after_find, :after_save
@@ -134,9 +165,6 @@ module ActiveRecord
134 165 # end
135 166 # end
136 167 #
137   - # So you specify the object you want messaged on a given callback. When that callback is triggered, the object has
138   - # a method by the name of the callback messaged.
139   - #
140 168 # The callback macros usually accept a symbol for the method they're supposed to run, but you can also pass a "method string",
141 169 # which will then be evaluated within the binding of the callback. Example:
142 170 #

0 comments on commit dedb9d8

Please sign in to comment.
Something went wrong with that request. Please try again.