Closed x9sim9 closed 3 years ago
Hey @x9sim9, overriding Lockbox methods isn't supported, but the 2nd approach will likely work if you combine it with Module#prepend
.
Hey @x9sim9, overriding Lockbox methods isn't supported, but the 2nd approach will likely work if you combine it with
Module#prepend
.
I may be confused but wouldn't prepend run before decrypting the field whereas I want to modify the data after its been decrypted?
For my use case I was integrating lockbox into an existing code base so it was not practical to rewrite the codebase, I did however manage to solve the infinite loop issue with self using your idea, could this fix be applied into the gem?
module Self
def [](key)
new_key = (key.to_s + "_ciphertext").to_sym
decrypt_method = ("decrypt_" + key.to_s + "_ciphertext").to_sym
if self.class.methods.include?(decrypt_method)
return self.class.send(decrypt_method, self.send(new_key))
end
super(key)
end
end
class MyModel < ActiveRecord::Base
prepend Self
end
Especially considering the infinite loop issue,
many thanks, Simon
The infinite loop is due to how it's being overridden. You'll want to do something like:
module MyModelOverride
def my_field
value = super
# do something with value
end
end
class MyModel < ActiveRecord::Base
prepend MyModelOverride
encrypts :my_field
end
The infinite loop is due to how it's being overridden. You'll want to do something like:
module MyModelOverride def my_field value = super # do something with value end end class MyModel < ActiveRecord::Base prepend MyModelOverride encrypts :my_field end
Hi the code I posted in the previous comment works perfectly I was just referencing your earlier comment "Overiding Lockbox Methods is not supported" if you integrated my suggestion or something similar then it would be supported for everyone, but thanks to your suggestion I now have a project wide fix as posted in the previous comment. Many thanks for the help, simon
Glad it's working, and thanks for sharing it. I think the gem would use a different approach to support overriding Lockbox methods directly in models, but it's not something I'd like to support right now.
using self causes infinite loop issue and using super returns blank