Skip to content

Commit

Permalink
Merge pull request #210 from saghaulor/attr_was_fix
Browse files Browse the repository at this point in the history
Fix attr_was method.
  • Loading branch information
saghaulor committed Apr 2, 2016
2 parents 8800a28 + 39488d2 commit 90c69e4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/attr_encrypted/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ def attr_encrypted(*attrs)
end

define_method("#{attr}_was") do
iv_and_salt = { iv: send("#{options[:attribute]}_iv_was"), salt: send("#{options[:attribute]}_salt_was"), operation: :decrypting }
encrypted_attributes[attr].merge!(iv_and_salt)
attr_was_options = { operation: :decrypting }
attr_was_options[:iv]= send("#{options[:attribute]}_iv_was") if respond_to?("#{options[:attribute]}_iv_was")
attr_was_options[:salt]= send("#{options[:attribute]}_salt_was") if respond_to?("#{options[:attribute]}_salt_was")
encrypted_attributes[attr].merge!(attr_was_options)
evaluated_options = evaluated_attr_encrypted_options_for(attr)
[:iv, :salt, :operation].each { |key| encrypted_attributes[attr].delete(key) }
self.class.decrypt(attr, send("#{options[:attribute]}_was"), evaluated_options)
Expand Down
6 changes: 6 additions & 0 deletions test/active_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ def test_should_create_was_predicate
assert_equal original_email, person.email_was
person.email = 'test2@example.com'
assert_equal original_email, person.email_was
old_pm_name = "Winston Churchill"
pm = PrimeMinister.create!(name: old_pm_name)
assert_equal old_pm_name, pm.name_was
old_zipcode = "90210"
address = Address.create!(zipcode: old_zipcode, mode: "single_iv_and_salt")
assert_equal old_zipcode, address.zipcode_was
end

if ::ActiveRecord::VERSION::STRING > "4.0"
Expand Down

0 comments on commit 90c69e4

Please sign in to comment.