Permalink
Browse files

Allow the callback when encryption occurs to be customized

  • Loading branch information...
1 parent 6e253fe commit 52e093f7ff6aa3d9b1ce514d0817c936bc1c71e7 Sergio Salvatore committed with obrie May 2, 2009
Showing with 24 additions and 19 deletions.
  1. +1 −0 CHANGELOG.rdoc
  2. +6 −2 lib/encrypted_attributes.rb
  3. +17 −17 test/unit/encrypted_attributes_test.rb
View
@@ -1,5 +1,6 @@
== master
+* Allow the callback when encryption occurs to be customized [Sergio Salvatore]
* Define source attribute accessor if different than target and not defined [Sergio Salvatore]
* Update tests to work with Rails 2.3
@@ -10,6 +10,9 @@ module MacroMethods
# EncryptedStrings for other possible modes.
# * <tt>:to</tt> - The attribute to write the encrypted value to. Default is
# the same attribute being encrypted.
+ # * <tt>:on</tt> - The ActiveRecord callback to use when triggering the
+ # encryption. By default, this will encrypt on <tt>before_validation</tt>.
+ # See ActiveRecord::Callbacks for a list of possible callbacks.
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
# if the encryption should occur. The method, proc or string should return
# or evaluate to a true or false value.
@@ -85,8 +88,9 @@ def encrypts(attr_name, options = {})
cipher_class = EncryptedStrings.const_get(class_name)
end
- # Set the encrypted value right before validation takes place
- before_validation(:if => options.delete(:if), :unless => options.delete(:unless)) do |record|
+ # Set the encrypted value on the configured callback
+ callback = options.delete(:on) || :before_validation
+ send(callback, :if => options.delete(:if), :unless => options.delete(:unless)) do |record|
record.send(:write_encrypted_attribute, attr_name, to_attr_name, cipher_class, options)
true
end
@@ -178,37 +178,37 @@ def teardown
end
class EncryptedAttributesWithConditionalsTest < ActiveSupport::TestCase
- def test_should_not_encrypt_if_if_conditional_is_false
- User.encrypts :password, :if => lambda {|user| false}
- user = create_user(:login => 'admin', :password => 'secret')
- assert_equal 'secret', user.password
- end
-
- def test_should_encrypt_if_if_conditional_is_true
- User.encrypts :password, :if => lambda {|user| true}
- user = create_user(:login => 'admin', :password => 'secret')
- assert_equal '8152bc582f58c854f580cb101d3182813dec4afe', "#{user.password}"
+ def setup
+ User.encrypts :password, :on => :before_create
end
- def test_should_not_encrypt_if_unless_conditional_is_true
- User.encrypts :password, :unless => lambda {|user| true}
- user = create_user(:login => 'admin', :password => 'secret')
+ def test_should_not_encrypt_on_validation
+ user = new_user(:login => 'admin', :password => 'secret')
+ user.valid?
assert_equal 'secret', user.password
end
- def test_should_encrypt_if_unless_conditional_is_false
- User.encrypts :password, :unless => lambda {|user| false}
- user = create_user(:login => 'admin', :password => 'secret')
+ def test_should_encrypt_on_create
+ user = new_user(:login => 'admin', :password => 'secret')
+ user.save
assert_equal '8152bc582f58c854f580cb101d3182813dec4afe', "#{user.password}"
end
def teardown
User.class_eval do
- @before_validation_callbacks = nil
+ @before_save_callbacks = nil
end
end
end
+class EncryptedAttributesWithCustomCallbackTest < ActiveSupport::TestCase
+ def test_should_not_encrypt_if_if_conditional_is_false
+ User.encrypts :password, :if => lambda {|user| false}
+ user = create_user(:login => 'admin', :password => 'secret')
+ assert_equal 'secret', user.password
+ end
+end
+
class ShaEncryptionTest < ActiveSupport::TestCase
def setup
User.encrypts :password, :mode => :sha

0 comments on commit 52e093f

Please sign in to comment.