Module: Mongoid::Config::Encryption Private

Extended by:
Encryption
Included in:
Mongoid::Config, Encryption
Defined in:
lib/mongoid/config/encryption.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

This module contains the logic for configuring Client Side Field Level automatic encryption.

Instance Method Summary collapse

Instance Method Details

#encryption_schema_map(default_database, models = ::Mongoid.models) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generate the encryption schema map for the provided models.

Parameters:

  • default_database (String)

    The default database name.

  • models (Array<Mongoid::Document>) (defaults to: ::Mongoid.models)

    The models to generate the schema map for. Defaults to all models in the application.

Returns:

  • (Hash)

    The encryption schema map.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mongoid/config/encryption.rb', line 22

def encryption_schema_map(default_database, models = ::Mongoid.models)
  models.each_with_object({}) do |model, map|
    next if model.embedded?
    next unless model.requires_encryption_schema?

    database = model.storage_options.fetch(:database) { default_database }
    # A callable database name cannot be resolved here: the documented
    # multi-tenant idiom has no correct value while the client is being
    # built. Interpolating the callable would produce a key that never
    # matches any namespace, so leave the model out of the map. Writes
    # are refused later, by PersistenceContext, rather than silently
    # going out unencrypted.
    next if database.respond_to?(:call)

    key = "#{database}.#{model.collection_name}"
    props = (model).merge(properties_for(model, [ model ]))
    # The root of a collection schema describes the document, so it is
    # always an object. Saying so matters when a nested schema carries
    # encryptMetadata: mongocryptd rejects the schema otherwise.
    map[key] = { 'bsonType' => 'object' }.merge(props) unless props.empty?
  end
end