Module: Mongoid::Sessions::ClassMethods

Defined in:
lib/mongoid/sessions.rb

Instance Method Summary collapse

Instance Method Details

#clear_persistence_optionstrue

Clear all persistence options from the current thread.

Examples:

Clear the persistence options.

Mongoid::Sessions.clear_persistence_options

Returns:

  • (true)

    True.

Since:

  • 3.0.0



151
152
153
# File 'lib/mongoid/sessions.rb', line 151

def clear_persistence_options
  Threaded.clear_persistence_options(self)
end

#collectionMoped::Collection

Get the collection for this model from the session. Will check for an overridden collection name from the store_in macro or the collection with a pluralized model name.

Examples:

Get the model’s collection.

Model.collection

Returns:

  • (Moped::Collection)

    The collection.

Since:

  • 3.0.0



165
166
167
168
169
170
171
172
173
# File 'lib/mongoid/sessions.rb', line 165

def collection
  if opts = persistence_options
    coll = mongo_session.with(opts)[opts[:collection] || collection_name]
    clear_persistence_options unless validating_with_query? || _loading_revision?
    coll
  else
    mongo_session[collection_name]
  end
end

#collection_nameSymbol

Get the name of the collection this model persists to. This will be either the pluralized class name or the option defined in the store_in macro.

Examples:

Get the collection name.

Model.collection_name

Returns:

  • (Symbol)

    The name of the collection.

Since:

  • 3.0.0



185
186
187
# File 'lib/mongoid/sessions.rb', line 185

def collection_name
  @collection_name ||= __collection_name__
end

#database_nameSymbol

Get the default database name for this model.

Examples:

Get the default database name.

Model.database_name

Returns:

  • (Symbol)

    The name of the database.

Since:

  • 3.0.0



197
198
199
# File 'lib/mongoid/sessions.rb', line 197

def database_name
  @database_name ||= __database_name__
end

#database_overrideString, Symbol

Get the overridden database name. This either can be overridden by using Model.with or by overriding at the global level via Mongoid.override_database(:name).

Examples:

Get the overridden database name.

Model.database_override

Returns:

  • (String, Symbol)

    The overridden database name.

Since:

  • 3.0.0



211
212
213
# File 'lib/mongoid/sessions.rb', line 211

def database_override
  persistence_options.try { |opts| opts[:database] } || Threaded.database_override
end

#mongo_sessionMoped::Session

Get the session for this model. This is determined in the following order:

1. Any custom configuration provided by the 'store_in' macro.
2. The 'default' session as provided in the mongoid.yml

Examples:

Get the session.

Model.mongo_session

Returns:

  • (Moped::Session)

    The default moped session.

Since:

  • 3.0.0



226
227
228
229
230
# File 'lib/mongoid/sessions.rb', line 226

def mongo_session
  session = __session__
  session.use(database_override || current_database_name(session))
  session
end

#persistence_optionsHash

Get the persistence options from the current thread.

Examples:

Get the persistence options.

Model.persistence_options

Returns:

  • (Hash)

    The persistence options.

Since:

  • 3.0.0



240
241
242
# File 'lib/mongoid/sessions.rb', line 240

def persistence_options
  Threaded.persistence_options(self)
end

#session_overrideString, Symbol

Get the overridden session name. This either can be overridden by using Model.with or by overriding at the global level via Mongoid.override_session(:name).

Examples:

Get the overridden session name.

Model.session_override

Returns:

  • (String, Symbol)

    The overridden session name.

Since:

  • 3.0.0



254
255
256
# File 'lib/mongoid/sessions.rb', line 254

def session_override
  persistence_options.try { |opts| opts[:session] } || Threaded.session_override
end

#store_in(options) ⇒ Class

Give this model specific custom default storage options.

Examples:

Store this model by default in “artists”

class Band
  include Mongoid::Document
  store_in collection: "artists"
end

Store this model by default in the sharded db.

class Band
  include Mongoid::Document
  store_in database: "echo_shard"
end

Store this model by default in a different session.

class Band
  include Mongoid::Document
  store_in session: "secondary"
end

Store this model with a combination of options.

class Band
  include Mongoid::Document
  store_in collection: "artists", database: "secondary"
end

Parameters:

  • options (Hash)

    The storage options.

Options Hash (options):

  • :collection (String, Symbol)

    The collection name.

  • :database (String, Symbol)

    The database name.

  • :session (String, Symbol)

    The session name.

Returns:

  • (Class)

    The model class.

Since:

  • 3.0.0



293
294
295
296
297
298
# File 'lib/mongoid/sessions.rb', line 293

def store_in(options)
  Validators::Storage.validate(self, options)
  @collection_name, @database_name = nil, nil
  self.storage_options ||= {}
  self.storage_options.merge! options
end

#with(options) ⇒ Class

Tell the next persistance operation to store in a specific collection, database or session.

Examples:

Create a document in a different collection.

Model.with(collection: "secondary").create(name: "test")

Create a document in a different database.

Model.with(database: "secondary").create(name: "test")

Create a document in a different session.

Model.with(session: "secondary").create(name: "test")

Create with a combination of options.

Model.with(session: "sharded", database: "secondary").create

Parameters:

  • options (Hash)

    The storage options.

Options Hash (options):

  • :collection (String, Symbol)

    The collection name.

  • :database (String, Symbol)

    The database name.

  • :session (String, Symbol)

    The session name.

Returns:

  • (Class)

    The model class.

Since:

  • 3.0.0



324
325
326
327
# File 'lib/mongoid/sessions.rb', line 324

def with(options)
  Threaded.set_persistence_options(self, options)
  self
end