module Mongoid::Config

def clients

Returns:
  • (Hash) - The clients configuration.

Other tags:
    Example: Get the clients configuration. -
def clients
  @clients ||= {}
end

def clients=(clients)

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

def config

Returns:
  • (self) - The Config singleton.
def config
  self
end

def configured?

Returns:
  • (true | false) - If Mongoid is configured.

Other tags:
    Example: Is Mongoid configured? -
def configured?
  clients.key?(:default)
end

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

Parameters:
  • name (String) -- The database name.

Other tags:
    Example: Set the database to connect to. -

Other tags:
    Note: - Use only in development or test environments for convenience.
def connect_to(name, options = { read: { mode: :primary }})
  self.clients = {
    default: {
      database: name,
      hosts: [ "localhost:27017" ],
      options: options
    }
  }
end

def deregister_model(klass)

Other tags:
    Api: - private

Parameters:
  • klass (Class) -- The model to deregister.
def deregister_model(klass)
  LOCK.synchronize do
    models.delete(klass)
  end
end

def destructive_fields

Returns:
  • (Array) - An array of bad field names.

Other tags:
    Example: Get the destructive fields. -
def destructive_fields
  Composable.prohibited_methods
end

def global_client

Returns:
  • (Mongo::Client) - Client according to global overrides.
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

def load!(path, environment = nil)

Parameters:
  • environment (String | Symbol) -- The environment to load.
  • path (String) -- The path to the file.

Other tags:
    Example: Configure Mongoid. -
def load!(path, environment = nil)
  settings = Environment.load_yaml(path, environment)
  if settings.present?
    Clients.disconnect
    Clients.clear
    load_configuration(settings)
  end
  settings
end

def load_configuration(settings)

Parameters:
  • settings (Hash) -- The configuration settings.

Other tags:
    Example: Load the configuration. -
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

def models

Returns:
  • (Array) - All the models in the application.

Other tags:
    Example: Get all the models. -
def models
  @models ||= []
end

def options=(options)

Parameters:
  • options (Hash) -- The configuration options.

Other tags:
    Example: Set the options. -
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

def override_client(name)

Returns:
  • (String | Symbol) - The global override.

Parameters:
  • name (String | Symbol) -- The name of the client.

Other tags:
    Example: Override the client globally. -
def override_client(name)
  Threaded.client_override = name ? name.to_s : nil
end

def override_database(name)

Returns:
  • (String | Symbol) - The global override.

Parameters:
  • name (String | Symbol) -- The name of the database.

Other tags:
    Example: Override the database globally. -
def override_database(name)
  Threaded.database_override = name
end

def purge!

Returns:
  • (true) - true.

Other tags:
    Note: - This is the fastest way to drop all data.

Other tags:
    Example: Purge all data. -
def purge!
  global_client.database.collections.each(&:drop) and true
end

def register_model(klass)

Parameters:
  • klass (Class) -- The model to register.

Other tags:
    Example: Register a model. -
def register_model(klass)
  LOCK.synchronize do
    models.push(klass) unless models.include?(klass)
  end
end

def running_with_passenger?

Deprecated:

Returns:
  • (true | false) - If the app is deployed on Passenger.

Other tags:
    Example: Is the application using passenger? -
def running_with_passenger?
  @running_with_passenger ||= defined?(PhusionPassenger)
end

def set_log_levels

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

def time_zone

Returns:
  • (String) - The time zone.

Other tags:
    Example: Get the time zone. -
def time_zone
  use_utc? ? "UTC" : ::Time.zone
end

def truncate!

Returns:
  • (true) - true.

Other tags:
    Note: - This will be slower than purge!

Other tags:
    Example: Truncate all collection data. -
def truncate!
  global_client.database.collections.each do |collection|
    collection.find.delete_many
  end and true
end