Initial commit
This commit is contained in:
25
app/models/concerns/encryptable.rb
Normal file
25
app/models/concerns/encryptable.rb
Normal file
@@ -0,0 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Encryptable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
class_methods do
|
||||
def attr_encrypted(*attributes)
|
||||
attributes.each do |attribute|
|
||||
define_method("#{attribute}=".to_sym) do |value|
|
||||
return if value.nil?
|
||||
|
||||
self.public_send(
|
||||
"#{attribute}_encrypted=".to_sym,
|
||||
EncryptionService.encrypt(value)
|
||||
)
|
||||
end
|
||||
|
||||
define_method(attribute) do
|
||||
value = self.public_send("#{attribute}_encrypted".to_sym)
|
||||
EncryptionService.decrypt(value) if value.present?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user