diff --git a/lib/vault/encrypted_model.rb b/lib/vault/encrypted_model.rb index 56b49aa0..f53ea42c 100644 --- a/lib/vault/encrypted_model.rb +++ b/lib/vault/encrypted_model.rb @@ -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 diff --git a/spec/integration/rails_spec.rb b/spec/integration/rails_spec.rb index 44005c05..9142db5f 100644 --- a/spec/integration/rails_spec.rb +++ b/spec/integration/rails_spec.rb @@ -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) @@ -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 @@ -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)