railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.88k stars 2.25k forks source link

Let Rails Admin play nicely with Lockbox #3241

Open bo-oz opened 4 years ago

bo-oz commented 4 years ago

I have a strange issue. I'm using Lockbox to encrypt my database values for the user model. Lockbox stores the field as {field_name}_ciphertext. Besides encrypting the data, lockbox also provides instance methods with the original {field_name}. I'd expect Rails Admin to be able to display the field, by calling the instance method, but somehow this doesn't work.

# suppose my field is name, this doesn't work

rails_admin do
  list do
    fields :name
  end
end

If I manually create an instance method, that is basically an alias for the instance method Lockbox creates, it does work:

def name_decode
  name
end

rails_admin do
  list do
    fields :name_decode
  end
end

Any ideas how I could let Rails Admin call the correct method on the instance?

bo-oz commented 4 years ago

What is strange is, when I look at another model, that is related to the User model (that has the name_cipertext encryption), that User model's name is de decyphered value for name. So in relationships it works, but when showing the original record, it doesn't.

incubus commented 4 years ago

Hi! Have you found any solution?

incubus commented 4 years ago

Well, here is my solution with app/models/concerns/entity/encryptable.rb:

module Entity
  module Encryptable
    def safe_send(value)
      if self.class.lockbox_attributes.key?(value)
        send(value)
      else
        super
      end
    end
  end
end
class User < ApplicationRecord
  include Entity::Encryptable
  encrypts :email, :name
end