Skip to content

Fix dirty checking #2

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

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
8 changes: 8 additions & 0 deletions lib/encrypted_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ def encrypts(*attr_names, &config)
attr_writer attr_name unless method_defined?("#{attr_name}=")
end

# Fix dirty checking when a single column is used
if attr_name == to_attr_name
define_method("#{to_attr_name}=") do |value|
write_attribute(to_attr_name, value)
send("#{to_attr_name}_will_change!")
end
end

# Define the reader when reading the encrypted attribute from the database
define_method(to_attr_name) do
read_encrypted_attribute(to_attr_name, cipher_class, config || options)
Expand Down
42 changes: 42 additions & 0 deletions test/unit/encrypted_attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ def test_should_encrypt_attribute_if_updating_with_changes
assert_equal '162cf5debf84cbc2af13da848544c3e2c515b4d3', "#{user.password}"
end

def test_should_encrypt_attribute_if_updating_with_same_password
user = create_user(:login => 'admin', :password => 'secret')
user.password = 'secret'
user.save!
user.reload
assert user.password.encrypted?
assert_not_equal String.new(user.password), 'secret'
assert_equal '8152bc582f58c854f580cb101d3182813dec4afe', "#{user.password}"
end

def teardown
User.class_eval do
@before_validation_callbacks = nil
Expand Down Expand Up @@ -449,6 +459,22 @@ def test_should_be_able_to_check_password
assert_equal 'secret', @user.password
end

def test_should_be_dirty_when_attr_is_accessed_then_set_to_same_value
# Access so that the cipher gets set, making equality work
@user.password

@user.password = 'secret'
assert @user.password_changed?
end

def test_should_not_be_encrypted_if_changed_until_saved
# Access so that the cipher gets set, making equality work
@user.password

@user.password = 'secret'
3.times { assert !@user.password.encrypted? }
end

def teardown
User.class_eval do
@before_validation_callbacks = nil
Expand Down Expand Up @@ -482,6 +508,22 @@ def test_should_be_able_to_check_password
assert_equal 'secret', @user.password
end

def test_should_be_dirty_when_attr_is_accessed_then_set_to_same_value
# Access so that the cipher gets set, making equality work
@user.password

@user.password = 'secret'
assert @user.password_changed?
end

def test_should_not_be_encrypted_if_changed_until_saved
# Access so that the cipher gets set, making equality work
@user.password

@user.password = 'secret'
3.times { assert !@user.password.encrypted? }
end

def teardown
User.class_eval do
@before_validation_callbacks = nil
Expand Down