jmazzi / crypt_keeper

Transparent ActiveRecord encryption
http://jmazzi.github.com/crypt_keeper/
MIT License
288 stars 99 forks source link

undefined method `crypt_keeper' for CryptKeeper::Model::ClassMethods:Module #185

Closed iamdriz closed 4 years ago

iamdriz commented 4 years ago

We're trying to use this Gem on a parent model that we can then declare fields at the child model level so we can share the settings for instantiating the encryption.

class EncryptedModel < ApplicationRecord
  self.abstract_class = true

  after_initialize do
    ::CryptKeeper::Model::ClassMethods.crypt_keeper encrypted_fields,
                                                    encryptor: :active_support,
                                                    key: key,
                                                    salt: salt
  end

  private

  def encrypted_fields
    raise "#{__method__} not implemented in #{self.class.name}"
  end

  def key
    ENV['CRYPT_KEEPER_KEY'] || 'key'
  end

  def salt
    ENV['CRYPT_KEEPER_SALT'] || 'salt'
  end
end
class MyModel < EncryptedModel

  private

  def encrypted_fields
    %i[field_one field_two]
  end
end

We have to use the after_initialize otherwise the encrypted_fields, key, and salt are undefined... but we now get the error: undefined method 'crypt_keeper' for CryptKeeper::Model::ClassMethods:Module.

If I don't specify the Module.. then it tries to find the crypt_keeper method on the MyModel class which again it doesn't exist...