Module: Mongoid::Config

Extended by:
Forwardable, Config, Defaults, Encryption, Options
Includes:
DeprecatedOptions
Included in:
Config
Defined in:
lib/mongoid/config.rb,
lib/mongoid/config/options.rb,
lib/mongoid/config/defaults.rb,
lib/mongoid/config/encryption.rb,
lib/mongoid/config/environment.rb,
lib/mongoid/config/validators/client.rb,
lib/mongoid/config/validators/option.rb,
lib/mongoid/config/validators/async_query_executor.rb

Overview

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

Defined Under Namespace

Modules: Defaults, DeprecatedOptions, Encryption, Environment, Options, Validators

Constant Summary collapse

LOCK =
Mutex.new
VALID_ISOLATION_LEVELS =
i[ rails thread fiber ].freeze

Constants included from DeprecatedOptions

DeprecatedOptions::OPTIONS

Instance Method Summary collapse

Methods included from Options

defaults, log_level, option, reset, settings

Methods included from Defaults

load_defaults

Methods included from Encryption

encryption_schema_map

Methods included from DeprecatedOptions

prepended

Instance Method Details

#clientsHash

Get the client configuration or an empty hash.

Examples:

Get the clients configuration.

config.clients

Returns:

  • The clients configuration.



457
458
459
# File 'lib/mongoid/config.rb', line 457

def clients
  @clients ||= {}
end

#configself

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

Returns:

  • The Config singleton.



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

def config
  self
end

#configured?true | false

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

Examples:

Is Mongoid configured?

config.configured?

Returns:

  • If Mongoid is configured.



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

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

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

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:

  • The database name.



298
299
300
301
302
303
304
305
306
# File 'lib/mongoid/config.rb', line 298

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

#deregister_model(klass) ⇒ Object

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.

Deregister a model in the application with Mongoid.

Parameters:

  • The model to deregister.

API:

  • private



365
366
367
368
369
# File 'lib/mongoid/config.rb', line 365

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 Mongoid::Document.

Examples:

Get the destructive fields.

config.destructive_fields

Returns:

  • An array of bad field names.



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

def destructive_fields
  Composable.prohibited_methods
end

#load!(path, environment = nil) ⇒ Object

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:

  • The path to the file.

  • (defaults to: nil)

    The environment to load.



327
328
329
330
331
332
333
334
335
# File 'lib/mongoid/config.rb', line 327

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) ⇒ Object

From a hash of settings, load all the configuration.

Examples:

Load the configuration.

config.load_configuration(settings)

Parameters:

  • The configuration settings.



377
378
379
380
381
382
383
# File 'lib/mongoid/config.rb', line 377

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 Mongoid::Document.

Examples:

Get all the models.

config.models

Returns:

  • All the models in the application.



344
345
346
# File 'lib/mongoid/config.rb', line 344

def models
  @models ||= []
end

#options=(options) ⇒ Object

Set the configuration options. Will validate each one individually.

Examples:

Set the options.

config.options = { raise_not_found_error: true }

Parameters:

  • The configuration options.



441
442
443
444
445
446
447
448
449
# File 'lib/mongoid/config.rb', line 441

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

#override_client(name) ⇒ String | Symbol

Override the client to use globally.

Examples:

Override the client globally.

config.override_client(:optional)

Parameters:

  • The name of the client.

Returns:

  • The global override.



405
406
407
# File 'lib/mongoid/config.rb', line 405

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:

  • The name of the database.

Returns:

  • The global override.



393
394
395
# File 'lib/mongoid/config.rb', line 393

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.



417
418
419
# File 'lib/mongoid/config.rb', line 417

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

#real_isolation_levelObject

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.

Returns the (potentially-dereferenced) isolation level that Mongoid will use to store its internal state. If ‘isolation_level` is set to `:rails`, this will return the isolation level that Rails is current configured to use (`ActiveSupport::IsolatedExecutionState.isolation_level`).

If using an older version of Rails that does not support ActiveSupport::IsolatedExecutionState, this will return ‘:thread` instead.

API:

  • private



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/mongoid/config.rb', line 145

def real_isolation_level
  return isolation_level unless isolation_level == :rails

  if defined?(ActiveSupport::IsolatedExecutionState)
    ActiveSupport::IsolatedExecutionState.isolation_level.tap do |level|
      # We can't guarantee that Rails will always support the same
      # isolation levels as Mongoid, so we check here to make sure
      # it's something we can work with.
      validate_isolation_level!(level)
    end
  else
    # The default, if Rails does not support IsolatedExecutionState,
    :thread
  end
end

#register_model(klass) ⇒ Object

Register a model in the application with Mongoid.

Examples:

Register a model.

config.register_model(Band)

Parameters:

  • The model to register.



354
355
356
357
358
# File 'lib/mongoid/config.rb', line 354

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

#running_with_passenger?true | false

Deprecated.

Is the application running under passenger?

Examples:

Is the application using passenger?

config.running_with_passenger?

Returns:

  • If the app is deployed on Passenger.



479
480
481
# File 'lib/mongoid/config.rb', line 479

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

#time_zoneString

Get the time zone to use.

Examples:

Get the time zone.

Config.time_zone

Returns:

  • The time zone.



467
468
469
# File 'lib/mongoid/config.rb', line 467

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.



429
430
431
432
433
# File 'lib/mongoid/config.rb', line 429

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

#validate_isolation_level!(level) ⇒ Object

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.

Checks to see if the provided isolation level is something that Mongoid supports. Raises Errors::UnsupportedIsolationLevel if it is not.

This will also raise an error if the isolation level is set to ‘:fiber` and the Ruby version is less than 3.2, since fiber-local storage is not supported in earlier Ruby versions.

API:

  • private



169
170
171
172
173
174
175
176
177
# File 'lib/mongoid/config.rb', line 169

def validate_isolation_level!(level)
  unless VALID_ISOLATION_LEVELS.include?(level)
    raise Errors::UnsupportedIsolationLevel.new(level)
  end

  if level == :fiber && RUBY_VERSION < '3.2'
    raise Errors::UnsupportedIsolationLevel.new(level)
  end
end