module Mongoid::Config

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

Constants

LOCK

Public Instance Methods

clients() click to toggle source

Get the client configuration or an empty hash.

@example Get the clients configuration.

config.clients

@return [ Hash ] The clients configuration.

# File lib/mongoid/config.rb, line 368
def clients
  @clients ||= {}
end
config() click to toggle source

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

@return [ self ] The Config singleton.

# File lib/mongoid/config.rb, line 186
def config
  self
end
configured?() click to toggle source

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

@example Is Mongoid configured?

config.configured?

@return [ true | false ] If Mongoid is configured.

# File lib/mongoid/config.rb, line 197
def configured?
  clients.key?(:default)
end
connect_to(name, options = { read: { mode: :primary }}) click to toggle source

Connect to the provided database name on the default client.

@note Use only in development or test environments for convenience.

@example Set the database to connect to.

config.connect_to("mongoid_test")

@param [ String ] name The database name.

# 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) click to toggle source

Deregister a model in the application with Mongoid.

@param [ Class ] klass The model to deregister.

@api private

# File lib/mongoid/config.rb, line 276
def deregister_model(klass)
  LOCK.synchronize do
    models.delete(klass)
  end
end
destructive_fields() click to toggle source

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

@example Get the destructive fields.

config.destructive_fields

@return [ Array<String> ] An array of bad field names.

# File lib/mongoid/config.rb, line 226
def destructive_fields
  Composable.prohibited_methods
end
load!(path, environment = nil) click to toggle source

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

@example Configure Mongoid.

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

@param [ String ] path The path to the file. @param [ String | Symbol ] environment The environment to load.

# 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) click to toggle source

From a hash of settings, load all the configuration.

@example Load the configuration.

config.load_configuration(settings)

@param [ Hash ] settings The configuration settings.

# 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
models() click to toggle source

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

@example Get all the models.

config.models

@return [ Array<Class> ] All the models in the application.

# File lib/mongoid/config.rb, line 255
def models
  @models ||= []
end
options=(options) click to toggle source

Set the configuration options. Will validate each one individually.

@example Set the options.

config.options = { raise_not_found_error: true }

@param [ Hash ] options The configuration options.

# 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
override_client(name) click to toggle source

Override the client to use globally.

@example Override the client globally.

config.override_client(:optional)

@param [ String | Symbol ] name The name of the client.

@return [ String | Symbol ] The global override.

# File lib/mongoid/config.rb, line 316
def override_client(name)
  Threaded.client_override = name ? name.to_s : nil
end
override_database(name) click to toggle source

Override the database to use globally.

@example Override the database globally.

config.override_database(:optional)

@param [ String | Symbol ] name The name of the database.

@return [ String | Symbol ] The global override.

# File lib/mongoid/config.rb, line 304
def override_database(name)
  Threaded.database_override = name
end
purge!() click to toggle source

Purge all data in all collections, including indexes.

@example Purge all data.

Mongoid::Config.purge!

@note This is the fastest way to drop all data.

@return [ true ] true.

# File lib/mongoid/config.rb, line 328
def purge!
  global_client.database.collections.each(&:drop) and true
end
register_model(klass) click to toggle source

Register a model in the application with Mongoid.

@example Register a model.

config.register_model(Band)

@param [ Class ] klass The model to register.

# File lib/mongoid/config.rb, line 265
def register_model(klass)
  LOCK.synchronize do
    models.push(klass) unless models.include?(klass)
  end
end
running_with_passenger?() click to toggle source

Is the application running under passenger?

@example Is the application using passenger?

config.running_with_passenger?

@return [ true | false ] If the app is deployed on Passenger.

@deprecated

# File lib/mongoid/config.rb, line 390
def running_with_passenger?
  @running_with_passenger ||= defined?(PhusionPassenger)
end
time_zone() click to toggle source

Get the time zone to use.

@example Get the time zone.

Config.time_zone

@return [ String ] The time zone.

# File lib/mongoid/config.rb, line 378
def time_zone
  use_utc? ? "UTC" : ::Time.zone
end
truncate!() click to toggle source

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

@example Truncate all collection data.

Mongoid::Config.truncate!

@note This will be slower than purge!

@return [ true ] true.

# File lib/mongoid/config.rb, line 340
def truncate!
  global_client.database.collections.each do |collection|
    collection.find.delete_many
  end and true
end

Private Instance Methods

clients=(clients) click to toggle source
# 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
global_client() click to toggle source

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

@return [Mongo::Client] Client according to global overrides.

# 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
set_log_levels() click to toggle source
# 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