module EncryptedAttribute

Constants

VERSION

Public Instance Methods

attr_encrypted(attr) click to toggle source
# File lib/encrypted_attribute.rb, line 10
def attr_encrypted(attr)
  define_method(attr) do
    value = instance_variable_get("@#{attr}")
    return value if value

    raw_value = public_send("encrypted_#{attr}".to_sym)
    return nil unless raw_value

    instance_variable_set "@#{attr}", encryptor.decrypt_and_verify(raw_value)
  end

  define_method("#{attr}=") do |new_value|
    value = encryptor.encrypt_and_sign(new_value)
    instance_variable_set("@#{attr}", new_value)
    public_send("encrypted_#{attr}=", value)
  end
end

Private Instance Methods

encryptor() click to toggle source
# File lib/encrypted_attribute.rb, line 31
def encryptor
  @encryptor = ActiveSupport::MessageEncryptor.new(encryptor_key)
end
encryptor_key() click to toggle source
# File lib/encrypted_attribute.rb, line 35
def encryptor_key
  ENV['RAILS_MASTER_KEY'] || File.read(Rails.application.config.credentials.key_path)
end