Skip to content

Commit

Permalink
Merge pull request #88 from FundingCircle/do-not-pollute-model-changes
Browse files Browse the repository at this point in the history
Do not pollute model changes on initialisation
  • Loading branch information
bliof-fc authored Oct 20, 2020
2 parents 13c3653 + 1234929 commit 887e708
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Vault Rails Changelog

## 2.0.5 (October 19, 2020)

- Fix compatibility with `#with_lock` / `#lock!` - on initialization the `#changes` is no longer polluted. Fixed error:
```
RuntimeError: Locking a record with unpersisted changes is not supported. Use `save` to persist the changes, or `reload` to discard them explicitly.
```

## 2.0.4 (December 2, 2019)

IMPROVEMENTS
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_4.2.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
fc-vault-rails (2.0.4)
fc-vault-rails (2.0.5)
activerecord (>= 4.2)
vault (~> 0.7)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5.0.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
fc-vault-rails (2.0.4)
fc-vault-rails (2.0.5)
activerecord (>= 4.2)
vault (~> 0.7)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5.1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
fc-vault-rails (2.0.4)
fc-vault-rails (2.0.5)
activerecord (>= 4.2)
vault (~> 0.7)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_5.2.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
fc-vault-rails (2.0.4)
fc-vault-rails (2.0.5)
activerecord (>= 4.2)
vault (~> 0.7)

Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_6.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
fc-vault-rails (2.0.4)
fc-vault-rails (2.0.5)
activerecord (>= 4.2)
vault (~> 0.7)

Expand Down
2 changes: 1 addition & 1 deletion lib/vault/latest/encrypted_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def __vault_load_attribute!(attribute, options)
__vault_loaded_attributes << attribute

# Write the virtual attribute with the plaintext value
write_attribute(attribute, plaintext)
write_attribute(attribute, plaintext).tap { clear_attribute_changes([attribute]) }
end

# Encrypt all the attributes using Vault and set the encrypted values back
Expand Down
2 changes: 1 addition & 1 deletion lib/vault/rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Vault
module Rails
VERSION = "2.0.4"
VERSION = "2.0.5"

def self.latest?
ActiveRecord.version >= Gem::Version.new('5.0.0')
Expand Down
33 changes: 24 additions & 9 deletions spec/integration/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
expect(person.ssn_was).to eq("123-45-6789")
end

it 'does not pollute changes / dirty attributes / when loading a record from the db' do
Person.create!(ssn: "123-45-6789")

expect(Person.last.changes).to be_blank
end

it "allows attributes to be updated with nil values" do
person = Person.create!(ssn: "123-45-6789")
person.update_attributes!(ssn: nil)
Expand Down Expand Up @@ -175,6 +181,15 @@
expect(person.ssn_was).to eq("123-45-6789")
end

it 'does not pollute changes / dirty attributes / when loading a record from the db' do
Person.create!(ssn: "123-45-6789")

person = Person.last
expect(person.changes).to be_blank
expect(person.ssn).to eq('123-45-6789')
expect(person.changes).to be_blank
end

it "allows attributes to be unset" do
person = LazyPerson.create!(ssn: "123-45-6789")
person.update_attributes!(ssn: nil)
Expand Down Expand Up @@ -572,7 +587,7 @@
context 'attribute with defined serializer' do
context 'new record with duplicated IP address' do
it 'is invalid' do
person = Person.create!(ip_address: IPAddr.new('127.0.0.1'))
Person.create!(ip_address: IPAddr.new('127.0.0.1'))
same_ip_address_person = Person.new(ip_address: IPAddr.new('127.0.0.1'))

expect(same_ip_address_person).not_to be_valid
Expand Down Expand Up @@ -670,16 +685,16 @@

it 'finds the expected record' do
first_person = LazyPerson.create!(passport_number: '12345678')
second_person = LazyPerson.create!(passport_number: '12345678')
third_person = LazyPerson.create!(passport_number: '87654321')
LazyPerson.create!(passport_number: '12345678')
LazyPerson.create!(passport_number: '87654321')

expect(LazyPerson.encrypted_find_by(passport_number: '12345678')).to eq(first_person)
end

context 'searching by attributes with defined serializer' do
it 'finds the expected record' do
first_person = Person.create!(ip_address: IPAddr.new('127.0.0.1'))
second_person = Person.create!(ip_address: IPAddr.new('192.168.0.1'))
Person.create!(ip_address: IPAddr.new('192.168.0.1'))

expect(Person.encrypted_find_by(ip_address: IPAddr.new('127.0.0.1'))).to eq(first_person)
end
Expand Down Expand Up @@ -707,16 +722,16 @@

it 'finds the expected record' do
first_person = LazyPerson.create!(passport_number: '12345678')
second_person = LazyPerson.create!(passport_number: '12345678')
third_person = LazyPerson.create!(passport_number: '87654321')
LazyPerson.create!(passport_number: '12345678')
LazyPerson.create!(passport_number: '87654321')

expect(LazyPerson.encrypted_find_by!(passport_number: '12345678')).to eq(first_person)
end

context 'searching by attributes with defined serializer' do
it 'finds the expected record' do
first_person = Person.create!(ip_address: IPAddr.new('127.0.0.1'))
second_person = Person.create!(ip_address: IPAddr.new('192.168.0.1'))
Person.create!(ip_address: IPAddr.new('192.168.0.1'))

expect(Person.encrypted_find_by!(ip_address: IPAddr.new('127.0.0.1'))).to eq(first_person)
end
Expand Down Expand Up @@ -751,15 +766,15 @@
it 'finds the expected records' do
first_person = LazyPerson.create!(passport_number: '12345678')
second_person = LazyPerson.create!(passport_number: '12345678')
third_person = LazyPerson.create!(passport_number: '87654321')
LazyPerson.create!(passport_number: '87654321')

expect(LazyPerson.encrypted_where(passport_number: '12345678').pluck(:id)).to match_array([first_person, second_person].map(&:id))
end

context 'searching by attributes with defined serializer' do
it 'finds the expected records' do
first_person = Person.create!(ip_address: IPAddr.new('127.0.0.1'))
second_person = Person.create!(ip_address: IPAddr.new('192.168.0.1'))
Person.create!(ip_address: IPAddr.new('192.168.0.1'))

expect(Person.encrypted_where(ip_address: IPAddr.new('127.0.0.1')).pluck(:id)).to match_array([first_person.id])
end
Expand Down

0 comments on commit 887e708

Please sign in to comment.