Module: Mongoid::Clients::ClassMethods

Defined in:
lib/mongoid/clients.rb

Instance Method Summary collapse

Instance Method Details

#collectionMongo::Collection

Get the collection for this model from the client. 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:

  • (Mongo::Collection)

    The collection.

Since:

  • 3.0.0



134
135
136
# File 'lib/mongoid/clients.rb', line 134

def collection
  mongo_client[collection_name]
end

#mongo_clientMongo::Client

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

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

Examples:

Get the client.

Model.mongo_client

Returns:

  • (Mongo::Client)

    The default mongo client.

Since:

  • 3.0.0



113
114
115
116
117
118
119
120
121
122
# File 'lib/mongoid/clients.rb', line 113

def mongo_client
  return client_with_options if client_with_options
  client = Clients.with_name(client_name)
  opts = self.persistence_options ? self.persistence_options.dup : {}
  if defined?(Mongo::Client::VALID_OPTIONS)
    opts.reject! { |k, v| !Mongo::Client::VALID_OPTIONS.include?(k.to_sym) }
  end
  opts.merge!(database: database_name) unless client.database.name.to_sym == database_name.to_sym
  client.with(opts)
end