Module: Mongoid::Sessions::ClassMethods

Defined in:
lib/mongoid/sessions.rb

Instance Method Summary collapse

Instance Method Details

#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



105
106
107
# File 'lib/mongoid/sessions.rb', line 105

def collection
  mongo_session[collection_name]
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



119
120
121
# File 'lib/mongoid/sessions.rb', line 119

def 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



131
132
133
# File 'lib/mongoid/sessions.rb', line 131

def 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



145
146
147
# File 'lib/mongoid/sessions.rb', line 145

def database_override
  self.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



160
161
162
163
164
# File 'lib/mongoid/sessions.rb', line 160

def mongo_session
  session = __session__
  session.use(database_override || current_database_name(session))
  self.persistence_options ? session.with(self.persistence_options) : session
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



176
177
178
# File 'lib/mongoid/sessions.rb', line 176

def session_override
  self.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



215
216
217
218
219
# File 'lib/mongoid/sessions.rb', line 215

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