Skip to content

Commit

Permalink
Merge pull request mongodb#3038 from mongoid/session_options
Browse files Browse the repository at this point in the history
Refectory on persistence options.
  • Loading branch information
arthurnn committed Aug 13, 2013
2 parents 85e1466 + af65a1b commit 0a486e0
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 208 deletions.
2 changes: 1 addition & 1 deletion lib/mongoid/contextual/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def map(field = nil, &block)
# @since 3.0.0
def initialize(criteria)
@criteria, @klass, @cache = criteria, criteria.klass, criteria.options[:cache]
@collection = klass.collection
@collection = @klass.with(criteria.persistence_options || {}).collection
criteria.send(:merge_type_selection)
@query = collection.find(criteria.selector)
apply_options
Expand Down
21 changes: 2 additions & 19 deletions lib/mongoid/criteria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "mongoid/criteria/marshalable"
require "mongoid/criteria/modifiable"
require "mongoid/criteria/scopable"
require "mongoid/sessions/options"

module Mongoid

Expand All @@ -22,6 +23,7 @@ class Criteria
include Marshalable
include Modifiable
include Scopable
include Sessions::Options

# Static array used to check with method missing - we only need to ever
# instantiate once.
Expand Down Expand Up @@ -396,25 +398,6 @@ def where(expression)
super
end

# Tell the next persistance operation to query from a specific collection,
# database or session.
#
# @example Send the criteria to another collection.
# Band.where(name: "Depeche Mode").with(collection: "artists")
#
# @param [ Hash ] options The storage options.
#
# @option options [ String, Symbol ] :collection The collection name.
# @option options [ String, Symbol ] :database The database name.
# @option options [ String, Symbol ] :session The session name.
#
# @return [ Criteria ] The criteria.
#
# @since 3.0.0
def with(options)
Threaded.set_persistence_options(klass, options)
self
end

# Get a version of this criteria without the options.
#
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def initialize(attrs = nil)
_building do
@new_record = true
@attributes ||= {}
options ||= {}
with(self.class.persistence_options)
apply_pre_processed_defaults
apply_default_scoping
process_attributes(attrs) do
Expand Down
3 changes: 2 additions & 1 deletion lib/mongoid/relations/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def substitutable
#
# @since 3.0.0
def with(options)
Threaded.set_persistence_options(klass, options)
@persistence_options = options
self
end

Expand All @@ -124,6 +124,7 @@ def with(options)
# @since 2.0.0
def collection
root = base._root
root.with(@persistence_options)
root.collection unless root.embedded?
end

Expand Down
2 changes: 2 additions & 0 deletions lib/mongoid/relations/referenced/many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ def unscoped
#
# @since 2.0.0.rc.1
def append(document)
document.with(@persistence_options) if @persistence_options

execute_callback :before_add, document
target.push(document)
characterize_one(document)
Expand Down
167 changes: 32 additions & 135 deletions lib/mongoid/sessions.rb
Original file line number Diff line number Diff line change
@@ -1,88 +1,18 @@
# encoding: utf-8
require "mongoid/sessions/factory"
require "mongoid/sessions/validators"
require "mongoid/sessions/options"

module Mongoid
module Sessions
extend ActiveSupport::Concern
include Options

included do
cattr_accessor :default_collection_name, :storage_options
self.default_collection_name = self.name.collectionize.to_sym
end

# 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.
#
# @example Get the model's collection.
# Model.collection
#
# @return [ Moped::Collection ] The collection.
#
# @since 3.0.0
def collection
self.class.collection
end

# 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.
#
# @example Get the collection name.
# Model.collection_name
#
# @return [ String ] The name of the collection.
#
# @since 3.0.0
def collection_name
self.class.collection_name
end

# 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
#
# @example Get the session.
# model.mongo_session
#
# @return [ Moped::Session ] The default moped session.
#
# @since 3.0.0
def mongo_session
self.class.mongo_session
end

# Tell the next persistance operation to store in a specific collection,
# database or session.
#
# @example Save the current document to a different collection.
# model.with(collection: "secondary").save
#
# @example Save the current document to a different database.
# model.with(database: "secondary").save
#
# @example Save the current document to a different session.
# model.with(session: "replica_set").save
#
# @example Save with a combination of options.
# model.with(session: "sharded", database: "secondary").save
#
# @param [ Hash ] options The storage options.
#
# @option options [ String, Symbol ] :collection The collection name.
# @option options [ String, Symbol ] :database The database name.
# @option options [ String, Symbol ] :session The session name.
#
# @return [ Document ] The current document.
#
# @since 3.0.0
def with(options)
Threaded.set_persistence_options(self.class, options)
self
end

class << self

# Clear all sessions from the current thread.
Expand Down Expand Up @@ -138,19 +68,29 @@ def with_name(name)
end
end

module ClassMethods
# 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.
#
# @example Get the model's collection.
# Model.collection
#
# @return [ Moped::Collection ] The collection.
#
# @since 3.0.0
def collection
mongo_session[collection_name]
end

# Clear all persistence options from the current thread.
#
# @example Clear the persistence options.
# Mongoid::Sessions.clear_persistence_options
#
# @return [ true ] True.
#
# @since 3.0.0
def clear_persistence_options
Threaded.clear_persistence_options(self)
end
def mongo_session
persistence_options ? self.class.mongo_session.with(persistence_options) : self.class.mongo_session
end

def collection_name
persistence_options.try { |opts| opts[:collection] } || self.class.collection_name
end

module ClassMethods

# Get the collection for this model from the session. Will check for an
# overridden collection name from the store_in macro or the collection
Expand All @@ -163,13 +103,7 @@ def clear_persistence_options
#
# @since 3.0.0
def collection
if opts = persistence_options
coll = mongo_session.with(opts)[opts[:collection] || collection_name]
clear_persistence_options unless validating_with_query?
coll
else
mongo_session[collection_name]
end
mongo_session[collection_name]
end

# Get the name of the collection this model persists to. This will be
Expand Down Expand Up @@ -209,7 +143,7 @@ def database_name
#
# @since 3.0.0
def database_override
persistence_options.try { |opts| opts[:database] } || Threaded.database_override
self.persistence_options.try { |opts| opts[:database] } || Threaded.database_override
end

# Get the session for this model. This is determined in the following order:
Expand All @@ -226,19 +160,7 @@ def database_override
def mongo_session
session = __session__
session.use(database_override || current_database_name(session))
session
end

# Get the persistence options from the current thread.
#
# @example Get the persistence options.
# Model.persistence_options
#
# @return [ Hash ] The persistence options.
#
# @since 3.0.0
def persistence_options
Threaded.persistence_options(self)
self.persistence_options ? session.with(self.persistence_options) : session
end

# Get the overridden session name. This either can be overridden by
Expand All @@ -252,7 +174,7 @@ def persistence_options
#
# @since 3.0.0
def session_override
persistence_options.try { |opts| opts[:session] } || Threaded.session_override
self.persistence_options.try { |opts| opts[:session] } || Threaded.session_override
end

# Give this model specific custom default storage options.
Expand Down Expand Up @@ -296,35 +218,6 @@ def store_in(options)
self.storage_options.merge!(options)
end

# Tell the next persistance operation to store in a specific collection,
# database or session.
#
# @example Create a document in a different collection.
# Model.with(collection: "secondary").create(name: "test")
#
# @example Create a document in a different database.
# Model.with(database: "secondary").create(name: "test")
#
# @example Create a document in a different session.
# Model.with(session: "secondary").create(name: "test")
#
# @example Create with a combination of options.
# Model.with(session: "sharded", database: "secondary").create
#
# @param [ Hash ] options The storage options.
#
# @option options [ String, Symbol ] :collection The collection name.
# @option options [ String, Symbol ] :database The database name.
# @option options [ String, Symbol ] :session The session name.
#
# @return [ Class ] The model class.
#
# @since 3.0.0
def with(options)
Threaded.set_persistence_options(self, options)
self
end

private

# Get the name of the collection this model persists to.
Expand All @@ -336,6 +229,10 @@ def with(options)
#
# @since 3.0.0
def __collection_name__
if coll = self.persistence_options.try(:[], :collection)
return coll
end

if storage_options && name = storage_options[:collection]
__evaluate__(name)
else
Expand Down
Loading

0 comments on commit 0a486e0

Please sign in to comment.