add encryptable_concern
Co-authored-by: François VANTOMME <akarzim@gmail.com>
This commit is contained in:
parent
17b659539f
commit
66c35fdffe
1 changed files with 21 additions and 0 deletions
21
app/models/concerns/encryptable_concern.rb
Normal file
21
app/models/concerns/encryptable_concern.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
module EncryptableConcern
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
class_methods do
|
||||
def attr_encrypted(*attributes)
|
||||
attributes.each do |attribute|
|
||||
define_method("#{attribute}=".to_sym) do |value|
|
||||
self.public_send(
|
||||
"encrypted_#{attribute}=".to_sym,
|
||||
EncryptionService.new.encrypt(value)
|
||||
)
|
||||
end
|
||||
|
||||
define_method(attribute) do
|
||||
value = self.public_send("encrypted_#{attribute}".to_sym)
|
||||
EncryptionService.new.decrypt(value) if value.present?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue