lib/sequel/plugins/attr_encrypted.rb in umbrellio-sequel-plugins-0.4.0.145 vs lib/sequel/plugins/attr_encrypted.rb in umbrellio-sequel-plugins-0.4.0.152

- old
+ new

@@ -30,11 +30,10 @@ # # => INSERT INTO "orders" ("encrypted_first_name") # VALUES ('/sTi9Q==$OTpuMRq5k8R3JayQ$WjSManQGP9UaZ3C40yDjKg==') # # order = Order.create(first_name: "Ivan", last_name: "Smith", # secret_data: { "some_key" => "Some Value" }) - # order.reload # order.first_name # => "Ivan" # order.last_name # => "Smith" # order.secret_data # => { "some_key" => "Some Value" } def attr_encrypted(*attrs, key:) include_encrypted_module! @@ -78,14 +77,22 @@ prepend @_attr_encrypted_module end end module InstanceMethods - def reload + def save(*) + super.tap { _reset_encrypted_attrs_ivars } + end + + def refresh(*) + super.tap { _reset_encrypted_attrs_ivars } + end + + private + + def _reset_encrypted_attrs_ivars self.class.instance_variable_get(:@_encrypted_attributes)&.each do |attr| instance_variable_set("@#{attr}", nil) end - - super end end end