Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't change iv field when value is the same #284

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/attr_encrypted.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ def attr_encrypted(*attributes)
end

define_method("#{attribute}=") do |value|
send("#{encrypted_attribute_name}=", encrypt(attribute, value))
instance_variable_set("@#{attribute}", value)
old_value = instance_variable_get("@#{attribute}")
if old_value.nil? || old_value != value
send("#{encrypted_attribute_name}=", encrypt(attribute, value))
instance_variable_set("@#{attribute}", value)
end
end

define_method("#{attribute}?") do
Expand Down
9 changes: 9 additions & 0 deletions test/attr_encrypted_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,13 @@ def test_should_not_by_default_generate_iv_when_attribute_is_empty
user.with_true_if = nil
assert_nil user.encrypted_with_true_if_iv
end

def test_should_not_generate_iv_if_same_value
user = User.new
assert_nil user.encrypted_email_iv
user.email = '[email protected]'
refute_nil(old_value = user.encrypted_email_iv)
user.email = '[email protected]'
assert_equal old_value, user.encrypted_email_iv
end
end