Skip to content

Creating a custom field for "EncryptedField": value parsed twice  #2755

@Rodrigora

Description

@Rodrigora

Hi there,

I'm struggling to create a custom field for fields encrypted by the symmetric-encryption gem.

For what I learned from your documentation and your source code, I just need to implement a new type and implement the method parse_value(value) which is called prior to the query build.

So, that's is my code:

module RailsAdmin
  module Config
    module Fields
      module Types
        class EncryptedField < RailsAdmin::Config::Fields::Types::String
          RailsAdmin::Config::Fields::Types::register(self)

          def parse_value(value)
            SymmetricEncryption.encrypt(value)
          end
        end
      end
    end
  end
end

RailsAdmin.config do |config|
  config.model 'User' do
    list do
      field :email, :encrypted_field do
        searchable ['encrypted_email'] 
        queryable true
      end
    end
end

Ok, the problem is on the ActiveRecord adapter, which calls the method parse_value twice before building the query:
https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/adapters/active_record.rb#L102
https://github.com/sferik/rails_admin/blob/master/lib/rails_admin/adapters/active_record.rb#L127

Before forking and removing one call for the parse_value method, I would like to know if there's a reason for this (will it break something else?). If no, I can submit a PR.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions