Module: Mongoid::Config

Extended by:
Config, Options
Included in:
Config
Defined in:
lib/mongoid/config.rb,
lib/mongoid/config/options.rb,
lib/mongoid/config/environment.rb,
lib/mongoid/config/validators/option.rb,
lib/mongoid/config/validators/session.rb

Overview

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

Defined Under Namespace

Modules: Environment, Options, Validators

Constant Summary collapse

LOCK =
Mutex.new

Instance Method Summary collapse

Methods included from Options

defaults, option, reset, settings

Instance Method Details

#configured?true, false

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

Examples:

Is Mongoid configured?

config.configured?

Returns:

  • (true, false)

    If Mongoid is configured.

Since:

  • 3.0.9



37
38
39
# File 'lib/mongoid/config.rb', line 37

def configured?
  sessions.has_key?(:default)
end

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

Note:

Use only in development or test environments for convenience.

Connect to the provided database name on the default session.

Examples:

Set the database to connect to.

config.connect_to("mongoid_test")

Parameters:

  • name (String)

    The database name.

Since:

  • 3.0.0



51
52
53
54
55
56
57
58
59
# File 'lib/mongoid/config.rb', line 51

def connect_to(name, options = { read: :primary })
  self.sessions = {
    default: {
      database: name,
      hosts: [ "localhost:27017" ],
      options: options
    }
  }
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:

  • (Array<String>)

    An array of bad field names.



68
69
70
# File 'lib/mongoid/config.rb', line 68

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:

  • path (String)

    The path to the file.

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

    The environment to load.

Since:

  • 2.0.1



82
83
84
85
86
87
88
89
90
# File 'lib/mongoid/config.rb', line 82

def load!(path, environment = nil)
  settings = Environment.load_yaml(path, environment)
  if settings.present?
    Sessions.disconnect
    Sessions.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:

  • settings (Hash)

    The configuration settings.

Since:

  • 3.1.0



127
128
129
130
131
# File 'lib/mongoid/config.rb', line 127

def load_configuration(settings)
  configuration = settings.with_indifferent_access
  self.options = configuration[:options]
  self.sessions = configuration[:sessions]
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:

  • (Array<Class>)

    All the models in the application.

Since:

  • 3.1.0



101
102
103
# File 'lib/mongoid/config.rb', line 101

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:

  • options (Hash)

    The configuration options.

Since:

  • 3.0.0



201
202
203
204
205
206
207
208
# File 'lib/mongoid/config.rb', line 201

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

#override_database(name) ⇒ String, Symbol

Override the database to use globally.

Examples:

Override the database globally.

config.override_database(:optional)

Parameters:

  • name (String, Symbol)

    The name of the database.

Returns:

  • (String, Symbol)

    The global override.

Since:

  • 3.0.0



143
144
145
# File 'lib/mongoid/config.rb', line 143

def override_database(name)
  Threaded.database_override = name
end

#override_session(name) ⇒ String, Symbol

Override the session to use globally.

Examples:

Override the session globally.

config.override_session(:optional)

Parameters:

  • name (String, Symbol)

    The name of the session.

Returns:

  • (String, Symbol)

    The global override.

Since:

  • 3.0.0



157
158
159
# File 'lib/mongoid/config.rb', line 157

def override_session(name)
  Threaded.session_override = name ? name.to_s : nil
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.

Since:

  • 2.0.2



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

def purge!
  Sessions.default.collections.each do |collection|
    collection.drop
  end and true
end

#register_model(klass) ⇒ Object

Register a model in the application with Mongoid.

Examples:

Register a model.

config.register_model(Band)

Parameters:

  • klass (Class)

    The model to register.

Since:

  • 3.1.0



113
114
115
116
117
# File 'lib/mongoid/config.rb', line 113

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

#running_with_passenger?true, false

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.

Since:

  • 3.0.11



258
259
260
# File 'lib/mongoid/config.rb', line 258

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

#sessionsHash

Get the session configuration or an empty hash.

Examples:

Get the sessions configuration.

config.sessions

Returns:

  • (Hash)

    The sessions configuration.

Since:

  • 3.0.0



218
219
220
# File 'lib/mongoid/config.rb', line 218

def sessions
  @sessions ||= {}
end

#sessions=(sessions) ⇒ Object

Set the session configuration options.

Examples:

Set the session configuration options.

config.sessions = { default: { hosts: [ "localhost:27017" ] }}

Parameters:

  • sessions (Hash)

    The configuration options.

Raises:

Since:

  • 3.0.0



230
231
232
233
234
235
236
# File 'lib/mongoid/config.rb', line 230

def sessions=(sessions)
  raise Errors::NoSessionsConfig.new unless sessions
  sess = sessions.with_indifferent_access
  Validators::Session.validate(sess)
  @sessions = sess
  sess
end

#time_zoneString

Get the time zone to use.

Examples:

Get the time zone.

Config.time_zone

Returns:

  • (String)

    The time zone.

Since:

  • 3.0.0



246
247
248
# File 'lib/mongoid/config.rb', line 246

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.

Since:

  • 2.0.2



187
188
189
190
191
# File 'lib/mongoid/config.rb', line 187

def truncate!
  Sessions.default.collections.each do |collection|
    collection.find.remove_all
  end and true
end