Skip to content

Commit

Permalink
Merge pull request #107 from hashicorp/fix-nil-assignments
Browse files Browse the repository at this point in the history
Fix dirty nil assignments
  • Loading branch information
chrisarcand authored Sep 22, 2020
2 parents 8c95325 + b84fe08 commit a9866a3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/vault/encrypted_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def __vault_load_attributes!(attribute_to_read = nil)
self.__vault_load_attribute!(attribute, options)
end

@__vault_loaded = self.class.__vault_attributes.all? { |attribute, __| instance_variable_get("@#{attribute}") }
@__vault_loaded = self.class.__vault_attributes.all? { |attribute, __| instance_variable_defined?("@#{attribute}") }

return true
end
Expand Down
27 changes: 27 additions & 0 deletions spec/integration/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@
expect(person.ssn).to be(nil)
end

it "allows dirty attributes to be unset" do
person = Person.create!(ssn: "123-45-6789")
person.ssn = nil
expect(person.ssn).to be_nil

person2 = Person.create!(ssn: "123-45-6789")
person2.assign_attributes(ssn: nil)
expect(person2.ssn).to be_nil
end

it "allows saving without validations" do
person = Person.new(ssn: "123-456-7890")
person.save(validate: false)
Expand Down Expand Up @@ -145,6 +155,12 @@
expect(person.ssn_changed?).to be(true)
expect(person.ssn_change).to eq(["123-45-6789", "111-11-1111"])
expect(person.ssn_was).to eq("123-45-6789")

person.assign_attributes(ssn: "222-22-2222")

expect(person.ssn_changed?).to be(true)
expect(person.ssn_change).to eq(["123-45-6789", "222-22-2222"])
expect(person.ssn_was).to eq("123-45-6789")
end

it "allows attributes to be unset" do
Expand All @@ -155,6 +171,17 @@
expect(person.ssn).to be(nil)
end

it "allows dirty attributes to be unset" do
person = LazyPerson.create!(ssn: "123-45-6789")
person.ssn = nil
expect(person.ssn).to be_nil

person2 = LazyPerson.create!(ssn: "123-45-6789")
person2.assign_attributes(ssn: nil)
expect(person2.ssn).to be_nil
end


it "allows saving without validations" do
person = LazyPerson.new(ssn: "123-456-7890")
expect(person.save(validate: false)).to be(true)
Expand Down

0 comments on commit a9866a3

Please sign in to comment.