123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Config

Overview

This module defines all the configuration options for ::Mongoid, including the database connections.

Constant Summary

DeprecatedOptions - Included

OPTIONS

Class Method Summary

Encryption - Extended

encryption_schema_map

Generate the encryption schema map for the provided models.

algorithm_for

Get the encryption algorithm to use for the provided field.

bson_type_for

Get the ::BSON type identifier for the provided field according to the www.mongodb.com/docs/manual/reference/bson-types/#std-label-bson-types.

key_id_for

Get the keyId encryption schema field for the base64 encrypted key id.

metadata_for

Generate the encryptMetadata object for the provided model.

properties_for

Generate encryption properties for the provided model.

properties_for_fields

Generate encryption properties for the fields of the provided model.

properties_for_relations

Generate encryption properties for the relations of the provided model.

Defaults - Extended

load_defaults

Load the defaults for the feature flags in the given ::Mongoid version.

Options - Extended

defaults

Get the defaults or initialize a new empty hash.

log_level

Get the log level.

option

Define a configuration option with a default.

reset

Reset the configuration options to the defaults.

settings

Get the settings or initialize a new empty hash.

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#clientsHash (rw)

Get the client configuration or an empty hash.

Examples:

Get the clients configuration.

config.clients

Returns:

  • (Hash)

    The clients configuration.

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 368

def clients
  @clients ||= {}
end

#clients=(clients) (rw, private)

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 403

def clients=(clients)
  raise Errors::NoClientsConfig.new unless clients
  c = clients.with_indifferent_access
  Validators::Client.validate(c)
  @clients = c
end

#configured?true | false (readonly)

Has Mongoid been configured? This is checking that at least a valid client config exists.

Examples:

Is Mongoid configured?

config.configured?

Returns:

  • (true | false)

    If Mongoid is configured.

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 197

def configured?
  clients.key?(:default)
end

#options=(options) (writeonly)

::Set the configuration options. Will validate each one individually.

Examples:

::Set the options.

config.options = { raise_not_found_error: true }

Parameters:

  • options (Hash)

    The configuration options.

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 352

def options=(options)
  if options
    Validators::AsyncQueryExecutor.validate(options)
    options.each_pair do |option, value|
      Validators::Option.validate(option)
      send("#{option}=", value)
    end
  end
end

#running_with_passenger?true | false (readonly)

Deprecated.

Is the application running under passenger?

Examples:

Is the application using passenger?

config.running_with_passenger?

Returns:

  • (true | false)

    If the app is deployed on Passenger.

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 390

def running_with_passenger?
  @running_with_passenger ||= defined?(PhusionPassenger)
end

Instance Method Details

#configself

Returns the Config singleton, for use in the configure DSL.

Returns:

  • (self)

    The Config singleton.

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 186

def config
  self
end

#connect_to(name, options = { read: { mode: :primary }})

Note:

Use only in development or test environments for convenience.

Connect to the provided database name on the default client.

Examples:

::Set the database to connect to.

config.connect_to("mongoid_test")

Parameters:

  • name (String)

    The database name.

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 209

def connect_to(name, options = { read: { mode: :primary }})
  self.clients = {
    default: {
      database: name,
      hosts: [ "localhost:27017" ],
      options: options
    }
  }
end

#deregister_model(klass)

This method is for internal use only.

Deregister a model in the application with ::Mongoid.

Parameters:

  • klass (Class)

    The model to deregister.

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 276

def deregister_model(klass)
  LOCK.synchronize do
    models.delete(klass)
  end
end

#destructive_fieldsArray<String>

Return field names that could cause destructive things to happen if defined in a Document.

Examples:

Get the destructive fields.

config.destructive_fields

Returns:

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 226

def destructive_fields
  Composable.prohibited_methods
end

#global_clientMongo::Client (private)

Get database client that respects global overrides Config.override_database and Config.override_client.

Returns:

  • (Mongo::Client)

    Client according to global overrides.

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 414

def global_client
  client =  if Threaded.client_override
              Clients.with_name(Threaded.client_override)
            else
              Clients.default
            end
  if Threaded.database_override
    client.use(Threaded.database_override)
  else
    client
  end
end

#load!(path, environment = nil)

Load the settings from a compliant mongoid.yml file. This can be used for easy setup with frameworks other than ::Rails.

Examples:

Configure Mongoid.

Mongoid.load!("/path/to/mongoid.yml")

Parameters:

  • path (String)

    The path to the file.

  • environment (String | Symbol) (defaults to: nil)

    The environment to load.

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 238

def load!(path, environment = nil)
  settings = Environment.load_yaml(path, environment)
  if settings.present?
    Clients.disconnect
    Clients.clear
    load_configuration(settings)
  end
  settings
end

#load_configuration(settings)

From a hash of settings, load all the configuration.

Examples:

Load the configuration.

config.load_configuration(settings)

Parameters:

  • settings (Hash)

    The configuration settings.

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 288

def load_configuration(settings)
  configuration = settings.with_indifferent_access
  self.options = configuration[:options]
  self.clients = configuration[:clients]
  Mongo.options = configuration[:driver_options] || {}
  set_log_levels
end

#modelsArray<Class>

Get all the models in the application - this is everything that includes Document.

Examples:

Get all the models.

config.models

Returns:

  • (Array<Class>)

    All the models in the application.

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 255

def models
  @models ||= []
end

#override_client(name) ⇒ String | Symbol

Override the client to use globally.

Examples:

Override the client globally.

config.override_client(:optional)

Parameters:

Returns:

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 316

def override_client(name)
  Threaded.client_override = name ? name.to_s : nil
end

#override_database(name) ⇒ String | Symbol

Override the database to use globally.

Examples:

Override the database globally.

config.override_database(:optional)

Parameters:

Returns:

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 304

def override_database(name)
  Threaded.database_override = name
end

#purge!true

Note:

This is the fastest way to drop all data.

Purge all data in all collections, including indexes.

Examples:

Purge all data.

Mongoid::Config.purge!

Returns:

  • (true)

    true.

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 328

def purge!
  global_client.database.collections.each(&:drop) and true
end

#register_model(klass)

Register a model in the application with ::Mongoid.

Examples:

Register a model.

config.register_model(Band)

Parameters:

  • klass (Class)

    The model to register.

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 265

def register_model(klass)
  LOCK.synchronize do
    models.push(klass) unless models.include?(klass)
  end
end

#set_log_levels (private)

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 398

def set_log_levels
  Mongoid.logger.level = Mongoid::Config.log_level unless defined?(::Rails)
  Mongo::Logger.logger.level = Mongoid.logger.level
end

#time_zoneString

Get the time zone to use.

Examples:

Get the time zone.

Config.time_zone

Returns:

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 378

def time_zone
  use_utc? ? "UTC" : ::Time.zone
end

#truncate!true

Note:

This will be slower than purge!

Truncate all data in all collections, but not the indexes.

Examples:

Truncate all collection data.

Mongoid::Config.truncate!

Returns:

  • (true)

    true.

[ GitHub ]

  
# File 'lib/mongoid/config.rb', line 340

def truncate!
  global_client.database.collections.each do |collection|
    collection.find.delete_many
  end and true
end