mongoid 4.0.2 → 5.0.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +66 -1
- data/README.md +14 -13
- data/lib/config/locales/en.yml +28 -28
- data/lib/mongoid.rb +28 -21
- data/lib/mongoid/atomic.rb +2 -4
- data/lib/mongoid/attributes.rb +7 -7
- data/lib/mongoid/attributes/processing.rb +4 -1
- data/lib/mongoid/attributes/readonly.rb +2 -2
- data/lib/mongoid/changeable.rb +4 -6
- data/lib/mongoid/clients.rb +142 -0
- data/lib/mongoid/clients/factory.rb +78 -0
- data/lib/mongoid/{sessions → clients}/options.rb +30 -19
- data/lib/mongoid/{sessions → clients}/storage_options.rb +27 -13
- data/lib/mongoid/{sessions → clients}/thread_options.rb +6 -3
- data/lib/mongoid/clients/validators.rb +2 -0
- data/lib/mongoid/{sessions → clients}/validators/storage.rb +5 -2
- data/lib/mongoid/composable.rb +3 -3
- data/lib/mongoid/config.rb +39 -41
- data/lib/mongoid/config/environment.rb +1 -1
- data/lib/mongoid/config/validators.rb +1 -1
- data/lib/mongoid/config/validators/{session.rb → client.rb} +31 -28
- data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
- data/lib/mongoid/contextual/atomic.rb +11 -11
- data/lib/mongoid/contextual/command.rb +9 -6
- data/lib/mongoid/contextual/geo_near.rb +17 -1
- data/lib/mongoid/contextual/map_reduce.rb +12 -11
- data/lib/mongoid/contextual/memory.rb +2 -5
- data/lib/mongoid/contextual/mongo.rb +92 -82
- data/lib/mongoid/contextual/none.rb +13 -0
- data/lib/mongoid/copyable.rb +6 -1
- data/lib/mongoid/criteria.rb +36 -3
- data/lib/mongoid/document.rb +3 -4
- data/lib/mongoid/errors.rb +6 -6
- data/lib/mongoid/errors/{mixed_session_configuration.rb → mixed_client_configuration.rb} +5 -5
- data/lib/mongoid/errors/no_client_config.rb +22 -0
- data/lib/mongoid/errors/{no_session_database.rb → no_client_database.rb} +4 -4
- data/lib/mongoid/errors/{no_session_hosts.rb → no_client_hosts.rb} +4 -4
- data/lib/mongoid/errors/{no_sessions_config.rb → no_clients_config.rb} +4 -4
- data/lib/mongoid/errors/no_default_client.rb +23 -0
- data/lib/mongoid/extensions/hash.rb +5 -1
- data/lib/mongoid/extensions/object.rb +3 -2
- data/lib/mongoid/extensions/set.rb +5 -5
- data/lib/mongoid/factory.rb +4 -2
- data/lib/mongoid/fields.rb +7 -2
- data/lib/mongoid/findable.rb +4 -1
- data/lib/mongoid/indexable.rb +15 -9
- data/lib/mongoid/persistable.rb +1 -2
- data/lib/mongoid/persistable/creatable.rb +2 -2
- data/lib/mongoid/persistable/deletable.rb +3 -3
- data/lib/mongoid/persistable/incrementable.rb +1 -1
- data/lib/mongoid/persistable/logical.rb +1 -1
- data/lib/mongoid/persistable/poppable.rb +1 -1
- data/lib/mongoid/persistable/pullable.rb +2 -2
- data/lib/mongoid/persistable/pushable.rb +2 -2
- data/lib/mongoid/persistable/renamable.rb +1 -1
- data/lib/mongoid/persistable/settable.rb +1 -1
- data/lib/mongoid/persistable/unsettable.rb +1 -1
- data/lib/mongoid/persistable/updatable.rb +2 -2
- data/lib/mongoid/persistable/upsertable.rb +1 -1
- data/lib/mongoid/query_cache.rb +98 -104
- data/lib/mongoid/railtie.rb +1 -21
- data/lib/mongoid/railties/database.rake +1 -1
- data/lib/mongoid/relations/builders.rb +3 -1
- data/lib/mongoid/relations/counter_cache.rb +1 -1
- data/lib/mongoid/relations/embedded/batchable.rb +3 -10
- data/lib/mongoid/relations/embedded/many.rb +4 -2
- data/lib/mongoid/relations/many.rb +1 -0
- data/lib/mongoid/relations/proxy.rb +6 -6
- data/lib/mongoid/relations/referenced/many.rb +2 -1
- data/lib/mongoid/relations/targets/enumerable.rb +11 -11
- data/lib/mongoid/relations/touchable.rb +1 -1
- data/lib/mongoid/reloadable.rb +2 -2
- data/lib/mongoid/scopable.rb +6 -17
- data/lib/mongoid/selectable.rb +1 -36
- data/lib/mongoid/serializable.rb +2 -2
- data/lib/mongoid/stateful.rb +0 -1
- data/lib/mongoid/tasks/database.rake +2 -2
- data/lib/mongoid/tasks/database.rb +23 -16
- data/lib/mongoid/threaded.rb +54 -33
- data/lib/mongoid/threaded/lifecycle.rb +21 -16
- data/lib/mongoid/traversable.rb +16 -1
- data/lib/mongoid/validatable.rb +1 -1
- data/lib/mongoid/validatable/queryable.rb +1 -1
- data/lib/mongoid/validatable/uniqueness.rb +3 -20
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +91 -57
- data/lib/rails/mongoid.rb +2 -2
- data/spec/app/models/audio.rb +1 -1
- data/spec/app/models/band.rb +1 -0
- data/spec/app/models/company.rb +5 -0
- data/spec/app/models/label.rb +7 -0
- data/spec/app/models/pub.rb +6 -0
- data/spec/app/models/staff.rb +7 -0
- data/spec/app/models/store_as_dup_test1.rb +5 -0
- data/spec/app/models/store_as_dup_test2.rb +5 -0
- data/spec/config/mongoid.yml +7 -25
- data/spec/mongoid/atomic/paths_spec.rb +3 -11
- data/spec/mongoid/attributes/nested_spec.rb +16 -16
- data/spec/mongoid/attributes/readonly_spec.rb +80 -18
- data/spec/mongoid/attributes_spec.rb +3 -3
- data/spec/mongoid/changeable_spec.rb +70 -0
- data/spec/mongoid/clients/factory_spec.rb +284 -0
- data/spec/mongoid/{sessions → clients}/options_spec.rb +4 -6
- data/spec/mongoid/clients_spec.rb +739 -0
- data/spec/mongoid/config/environment_spec.rb +14 -11
- data/spec/mongoid/config_spec.rb +33 -48
- data/spec/mongoid/contextual/atomic_spec.rb +1 -17
- data/spec/mongoid/contextual/geo_near_spec.rb +35 -0
- data/spec/mongoid/contextual/mongo_spec.rb +26 -83
- data/spec/mongoid/contextual/none_spec.rb +15 -0
- data/spec/mongoid/copyable_spec.rb +35 -1
- data/spec/mongoid/criteria/findable_spec.rb +197 -0
- data/spec/mongoid/criteria/modifiable_spec.rb +7 -29
- data/spec/mongoid/criteria_spec.rb +74 -91
- data/spec/mongoid/document_spec.rb +1 -1
- data/spec/mongoid/errors/{mixed_session_configuration_spec.rb → mixed_client_configuration_spec.rb} +1 -1
- data/spec/mongoid/errors/{no_session_config_spec.rb → no_client_config_spec.rb} +4 -4
- data/spec/mongoid/errors/{no_session_database_spec.rb → no_client_database_spec.rb} +4 -4
- data/spec/mongoid/errors/{no_session_hosts_spec.rb → no_client_hosts_spec.rb} +3 -3
- data/spec/mongoid/errors/{no_sessions_config_spec.rb → no_clients_config_spec.rb} +2 -2
- data/spec/mongoid/fields/localized_spec.rb +1 -0
- data/spec/mongoid/fields_spec.rb +1 -0
- data/spec/mongoid/findable_spec.rb +2 -23
- data/spec/mongoid/indexable_spec.rb +12 -8
- data/spec/mongoid/interceptable_spec.rb +15 -0
- data/spec/mongoid/persistable/settable_spec.rb +16 -0
- data/spec/mongoid/persistable/updatable_spec.rb +3 -2
- data/spec/mongoid/persistable_spec.rb +4 -4
- data/spec/mongoid/query_cache_spec.rb +13 -8
- data/spec/mongoid/relations/auto_save_spec.rb +1 -1
- data/spec/mongoid/relations/counter_cache_spec.rb +34 -0
- data/spec/mongoid/relations/eager/belongs_to_spec.rb +9 -0
- data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +3 -3
- data/spec/mongoid/relations/embedded/many_spec.rb +123 -1
- data/spec/mongoid/relations/embedded/one_spec.rb +3 -3
- data/spec/mongoid/relations/proxy_spec.rb +28 -0
- data/spec/mongoid/relations/referenced/in_spec.rb +1 -1
- data/spec/mongoid/relations/referenced/many_spec.rb +47 -23
- data/spec/mongoid/relations/referenced/many_to_many_spec.rb +1 -1
- data/spec/mongoid/relations/referenced/one_spec.rb +1 -1
- data/spec/mongoid/relations/targets/enumerable_spec.rb +9 -2
- data/spec/mongoid/reloadable_spec.rb +6 -6
- data/spec/mongoid/scopable_spec.rb +41 -28
- data/spec/mongoid/selectable_spec.rb +6 -16
- data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
- data/spec/mongoid/tasks/database_spec.rb +2 -2
- data/spec/mongoid/threaded_spec.rb +0 -7
- data/spec/mongoid/traversable_spec.rb +2 -2
- data/spec/mongoid/validatable/uniqueness_spec.rb +30 -1
- data/spec/mongoid_spec.rb +13 -15
- data/spec/rails/mongoid_spec.rb +13 -4
- data/spec/spec_helper.rb +44 -27
- data/spec/support/authorization.rb +12 -0
- data/spec/support/expectations.rb +14 -0
- metadata +52 -59
- data/lib/mongoid/contextual/find_and_modify.rb +0 -69
- data/lib/mongoid/contextual/text_search.rb +0 -178
- data/lib/mongoid/criteria/#findable.rb# +0 -141
- data/lib/mongoid/errors/no_default_session.rb +0 -23
- data/lib/mongoid/errors/no_session_config.rb +0 -22
- data/lib/mongoid/log_subscriber.rb +0 -55
- data/lib/mongoid/positional.rb +0 -71
- data/lib/mongoid/sessions.rb +0 -125
- data/lib/mongoid/sessions/factory.rb +0 -131
- data/lib/mongoid/sessions/mongo_uri.rb +0 -93
- data/lib/mongoid/sessions/validators.rb +0 -2
- data/lib/mongoid/support/query_counter.rb +0 -23
- data/spec/helpers.rb +0 -18
- data/spec/mongoid/#atomic_spec.rb# +0 -365
- data/spec/mongoid/contextual/find_and_modify_spec.rb +0 -220
- data/spec/mongoid/contextual/text_search_spec.rb +0 -209
- data/spec/mongoid/log_subscriber_spec.rb +0 -75
- data/spec/mongoid/positional_spec.rb +0 -222
- data/spec/mongoid/sessions/factory_spec.rb +0 -333
- data/spec/mongoid/sessions/mongo_uri_spec.rb +0 -103
- data/spec/mongoid/sessions_spec.rb +0 -1252
data/lib/mongoid/changeable.rb
CHANGED
@@ -16,7 +16,7 @@ module Mongoid
|
|
16
16
|
#
|
17
17
|
# @since 2.4.0
|
18
18
|
def changed
|
19
|
-
changed_attributes.keys
|
19
|
+
changed_attributes.keys.select { |attr| attribute_change(attr) }
|
20
20
|
end
|
21
21
|
|
22
22
|
# Has the document changed?
|
@@ -40,9 +40,7 @@ module Mongoid
|
|
40
40
|
#
|
41
41
|
# @since 2.4.1
|
42
42
|
def children_changed?
|
43
|
-
_children.any?
|
44
|
-
child.changed?
|
45
|
-
end
|
43
|
+
_children.any?(&:changed?)
|
46
44
|
end
|
47
45
|
|
48
46
|
# Get the attribute changes.
|
@@ -184,7 +182,7 @@ module Mongoid
|
|
184
182
|
# @since 2.1.6
|
185
183
|
def attribute_changed?(attr)
|
186
184
|
attr = database_field_name(attr)
|
187
|
-
return false unless changed_attributes.
|
185
|
+
return false unless changed_attributes.key?(attr)
|
188
186
|
changed_attributes[attr] != attributes[attr]
|
189
187
|
end
|
190
188
|
|
@@ -228,7 +226,7 @@ module Mongoid
|
|
228
226
|
#
|
229
227
|
# @since 2.3.0
|
230
228
|
def attribute_will_change!(attr)
|
231
|
-
unless changed_attributes.
|
229
|
+
unless changed_attributes.key?(attr)
|
232
230
|
changed_attributes[attr] = read_attribute(attr).__deep_copy__
|
233
231
|
end
|
234
232
|
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "mongoid/clients/factory"
|
3
|
+
require "mongoid/clients/validators"
|
4
|
+
require "mongoid/clients/storage_options"
|
5
|
+
require "mongoid/clients/thread_options"
|
6
|
+
require "mongoid/clients/options"
|
7
|
+
|
8
|
+
module Mongoid
|
9
|
+
module Clients
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
extend Gem::Deprecate
|
12
|
+
include StorageOptions
|
13
|
+
include ThreadOptions
|
14
|
+
include Options
|
15
|
+
|
16
|
+
class << self
|
17
|
+
|
18
|
+
# Clear all clients from the current thread.
|
19
|
+
#
|
20
|
+
# @example Clear all clients.
|
21
|
+
# Mongoid::Clients.clear
|
22
|
+
#
|
23
|
+
# @return [ Array ] The empty clients.
|
24
|
+
#
|
25
|
+
# @since 3.0.0
|
26
|
+
def clear
|
27
|
+
clients.clear
|
28
|
+
end
|
29
|
+
|
30
|
+
# Get the default client.
|
31
|
+
#
|
32
|
+
# @example Get the default client.
|
33
|
+
# Mongoid::Clients.default
|
34
|
+
#
|
35
|
+
# @return [ Mongo::Client ] The default client.
|
36
|
+
#
|
37
|
+
# @since 3.0.0
|
38
|
+
def default
|
39
|
+
clients[:default] ||= Clients::Factory.default
|
40
|
+
end
|
41
|
+
|
42
|
+
# Disconnect all active clients.
|
43
|
+
#
|
44
|
+
# @example Disconnect all active clients.
|
45
|
+
# Mongoid::Clients.disconnect
|
46
|
+
#
|
47
|
+
# @return [ true ] True.
|
48
|
+
#
|
49
|
+
# @since 3.1.0
|
50
|
+
def disconnect
|
51
|
+
clients.values.each do |client|
|
52
|
+
client.close
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Get a client with the provided name.
|
57
|
+
#
|
58
|
+
# @example Get a client with the name.
|
59
|
+
# Mongoid::Clients.with_name(:replica)
|
60
|
+
#
|
61
|
+
# @param [ Symbol ] name The name of the client.
|
62
|
+
#
|
63
|
+
# @return [ Mongo::Client ] The named client.
|
64
|
+
#
|
65
|
+
# @since 3.0.0
|
66
|
+
def with_name(name)
|
67
|
+
clients[name.to_sym] ||= Clients::Factory.create(name)
|
68
|
+
end
|
69
|
+
|
70
|
+
def set(name, client)
|
71
|
+
clients[name.to_sym] = client
|
72
|
+
end
|
73
|
+
|
74
|
+
def clients
|
75
|
+
@clients ||= {}
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Get the collection for this model from the client. Will check for an
|
80
|
+
# overridden collection name from the store_in macro or the collection
|
81
|
+
# with a pluralized model name.
|
82
|
+
#
|
83
|
+
# @example Get the model's collection.
|
84
|
+
# Model.collection
|
85
|
+
#
|
86
|
+
# @return [ Mongo::Collection ] The collection.
|
87
|
+
#
|
88
|
+
# @since 3.0.0
|
89
|
+
def collection
|
90
|
+
mongo_client[collection_name]
|
91
|
+
end
|
92
|
+
|
93
|
+
def mongo_client
|
94
|
+
super || self.class.mongo_client
|
95
|
+
end
|
96
|
+
alias :mongo_session :mongo_client
|
97
|
+
deprecate :mongo_session, :mongo_client, 2015, 12
|
98
|
+
|
99
|
+
def collection_name
|
100
|
+
super || self.class.collection_name
|
101
|
+
end
|
102
|
+
|
103
|
+
module ClassMethods
|
104
|
+
extend Gem::Deprecate
|
105
|
+
|
106
|
+
# Get the client for this model. This is determined in the following order:
|
107
|
+
#
|
108
|
+
# 1. Any custom configuration provided by the 'store_in' macro.
|
109
|
+
# 2. The 'default' client as provided in the mongoid.yml
|
110
|
+
#
|
111
|
+
# @example Get the client.
|
112
|
+
# Model.mongo_client
|
113
|
+
#
|
114
|
+
# @return [ Mongo::Client ] The default mongo client.
|
115
|
+
#
|
116
|
+
# @since 3.0.0
|
117
|
+
def mongo_client
|
118
|
+
name = client_name
|
119
|
+
client = Clients.with_name(name)
|
120
|
+
client = client.use(database_name)
|
121
|
+
client = client.with(self.persistence_options)
|
122
|
+
Clients.set(name, client)
|
123
|
+
end
|
124
|
+
alias :mongo_session :mongo_client
|
125
|
+
deprecate :mongo_session, :mongo_client, 2015, 12
|
126
|
+
|
127
|
+
# Get the collection for this model from the client. Will check for an
|
128
|
+
# overridden collection name from the store_in macro or the collection
|
129
|
+
# with a pluralized model name.
|
130
|
+
#
|
131
|
+
# @example Get the model's collection.
|
132
|
+
# Model.collection
|
133
|
+
#
|
134
|
+
# @return [ Mongo::Collection ] The collection.
|
135
|
+
#
|
136
|
+
# @since 3.0.0
|
137
|
+
def collection
|
138
|
+
mongo_client[collection_name]
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
module Clients
|
5
|
+
module Factory
|
6
|
+
extend self
|
7
|
+
|
8
|
+
# Create a new client given the named configuration. If no name is
|
9
|
+
# provided, return a new client with the default configuration. If a
|
10
|
+
# name is provided for which no configuration exists, an error will be
|
11
|
+
# raised.
|
12
|
+
#
|
13
|
+
# @example Create the client.
|
14
|
+
# Factory.create(:secondary)
|
15
|
+
#
|
16
|
+
# @param [ String, Symbol ] name The named client configuration.
|
17
|
+
#
|
18
|
+
# @raise [ Errors::NoClientConfig ] If no config could be found.
|
19
|
+
#
|
20
|
+
# @return [ Mongo::Client ] The new client.
|
21
|
+
#
|
22
|
+
# @since 3.0.0
|
23
|
+
def create(name = nil)
|
24
|
+
return default unless name
|
25
|
+
config = Mongoid.clients[name]
|
26
|
+
raise Errors::NoClientConfig.new(name) unless config
|
27
|
+
create_client(config)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Get the default client.
|
31
|
+
#
|
32
|
+
# @example Get the default client.
|
33
|
+
# Factory.default
|
34
|
+
#
|
35
|
+
# @raise [ Errors::NoClientConfig ] If no default configuration is
|
36
|
+
# found.
|
37
|
+
#
|
38
|
+
# @return [ Mongo::Client ] The default client.
|
39
|
+
#
|
40
|
+
# @since 3.0.0
|
41
|
+
def default
|
42
|
+
create_client(Mongoid.clients[:default])
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
# Create the client for the provided config.
|
48
|
+
#
|
49
|
+
# @api private
|
50
|
+
#
|
51
|
+
# @example Create the client.
|
52
|
+
# Factory.create_client(config)
|
53
|
+
#
|
54
|
+
# @param [ Hash ] configuration The client config.
|
55
|
+
#
|
56
|
+
# @return [ Mongo::Client ] The client.
|
57
|
+
#
|
58
|
+
# @since 3.0.0
|
59
|
+
def create_client(configuration)
|
60
|
+
raise Errors::NoClientsConfig.new unless configuration
|
61
|
+
if configuration[:uri]
|
62
|
+
Mongo::Client.new(configuration[:uri], options(configuration))
|
63
|
+
else
|
64
|
+
Mongo::Client.new(
|
65
|
+
configuration[:hosts],
|
66
|
+
options(configuration).merge(database: configuration[:database])
|
67
|
+
)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def options(configuration)
|
72
|
+
config = configuration.dup
|
73
|
+
options = config.delete(:options) || {}
|
74
|
+
options.reject{ |k, v| k == :hosts }.to_hash.symbolize_keys!
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -1,11 +1,12 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module Mongoid
|
3
|
-
module
|
3
|
+
module Clients
|
4
4
|
module Options
|
5
5
|
extend ActiveSupport::Concern
|
6
|
+
extend Gem::Deprecate
|
6
7
|
|
7
8
|
# Tell the next persistance operation to store in a specific collection,
|
8
|
-
# database or
|
9
|
+
# database or client.
|
9
10
|
#
|
10
11
|
# @example Save the current document to a different collection.
|
11
12
|
# model.with(collection: "secondary").save
|
@@ -13,17 +14,22 @@ module Mongoid
|
|
13
14
|
# @example Save the current document to a different database.
|
14
15
|
# model.with(database: "secondary").save
|
15
16
|
#
|
16
|
-
# @example Save the current document to a different
|
17
|
-
# model.with(
|
17
|
+
# @example Save the current document to a different client.
|
18
|
+
# model.with(client: "replica_set").save
|
18
19
|
#
|
19
20
|
# @example Save with a combination of options.
|
20
|
-
# model.with(
|
21
|
+
# model.with(client: "sharded", database: "secondary").save
|
22
|
+
#
|
23
|
+
# @note This method will instantiate a new client under the covers and
|
24
|
+
# can be expensive. It is also recommended that the user manually
|
25
|
+
# closes the extra client after using it, otherwise an excessive amount
|
26
|
+
# of connections to the server will be eventually opened.
|
21
27
|
#
|
22
28
|
# @param [ Hash ] options The storage options.
|
23
29
|
#
|
24
30
|
# @option options [ String, Symbol ] :collection The collection name.
|
25
31
|
# @option options [ String, Symbol ] :database The database name.
|
26
|
-
# @option options [ String, Symbol ] :
|
32
|
+
# @option options [ String, Symbol ] :client The client name.
|
27
33
|
#
|
28
34
|
# @return [ Document ] The current document.
|
29
35
|
#
|
@@ -37,17 +43,19 @@ module Mongoid
|
|
37
43
|
@persistence_options
|
38
44
|
end
|
39
45
|
|
40
|
-
def
|
46
|
+
def mongo_client
|
41
47
|
if persistence_options
|
42
|
-
if persistence_options[:
|
43
|
-
|
48
|
+
if persistence_options[:client]
|
49
|
+
client = Clients.with_name(persistence_options[:client])
|
44
50
|
else
|
45
|
-
|
46
|
-
|
51
|
+
client = Clients.with_name(self.class.client_name)
|
52
|
+
client.use(self.class.database_name)
|
47
53
|
end
|
48
|
-
|
54
|
+
client.with(persistence_options.reject{ |k, v| k == :collection || k == :client })
|
49
55
|
end
|
50
56
|
end
|
57
|
+
alias :mongo_session :mongo_client
|
58
|
+
deprecate :mongo_session, :mongo_client, 2015, 12
|
51
59
|
|
52
60
|
def collection_name
|
53
61
|
if persistence_options && v = persistence_options[:collection]
|
@@ -91,14 +99,17 @@ module Mongoid
|
|
91
99
|
end
|
92
100
|
|
93
101
|
module ClassMethods
|
102
|
+
extend Gem::Deprecate
|
94
103
|
include Threaded
|
95
104
|
|
96
|
-
def
|
97
|
-
if persistence_options && v = persistence_options[:
|
105
|
+
def client_name
|
106
|
+
if persistence_options && v = persistence_options[:client]
|
98
107
|
return v.to_sym
|
99
108
|
end
|
100
109
|
super
|
101
110
|
end
|
111
|
+
alias :session_name :client_name
|
112
|
+
deprecate :session_name, :client_name, 2015, 12
|
102
113
|
|
103
114
|
def collection_name
|
104
115
|
if persistence_options && v = persistence_options[:collection]
|
@@ -115,7 +126,7 @@ module Mongoid
|
|
115
126
|
end
|
116
127
|
|
117
128
|
# Tell the next persistance operation to store in a specific collection,
|
118
|
-
# database or
|
129
|
+
# database or client.
|
119
130
|
#
|
120
131
|
# @example Create a document in a different collection.
|
121
132
|
# Model.with(collection: "secondary").create(name: "test")
|
@@ -123,17 +134,17 @@ module Mongoid
|
|
123
134
|
# @example Create a document in a different database.
|
124
135
|
# Model.with(database: "secondary").create(name: "test")
|
125
136
|
#
|
126
|
-
# @example Create a document in a different
|
127
|
-
# Model.with(
|
137
|
+
# @example Create a document in a different client.
|
138
|
+
# Model.with(client: "secondary").create(name: "test")
|
128
139
|
#
|
129
140
|
# @example Create with a combination of options.
|
130
|
-
# Model.with(
|
141
|
+
# Model.with(client: "sharded", database: "secondary").create
|
131
142
|
#
|
132
143
|
# @param [ Hash ] options The storage options.
|
133
144
|
#
|
134
145
|
# @option options [ String, Symbol ] :collection The collection name.
|
135
146
|
# @option options [ String, Symbol ] :database The database name.
|
136
|
-
# @option options [ String, Symbol ] :
|
147
|
+
# @option options [ String, Symbol ] :client The client name.
|
137
148
|
#
|
138
149
|
# @return [ Class ] The model class.
|
139
150
|
#
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module Mongoid
|
3
|
-
module
|
3
|
+
module Clients
|
4
4
|
module StorageOptions
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
@@ -12,6 +12,7 @@ module Mongoid
|
|
12
12
|
end
|
13
13
|
|
14
14
|
module ClassMethods
|
15
|
+
extend Gem::Deprecate
|
15
16
|
|
16
17
|
# Give this model specific custom default storage options.
|
17
18
|
#
|
@@ -27,10 +28,10 @@ module Mongoid
|
|
27
28
|
# store_in database: "echo_shard"
|
28
29
|
# end
|
29
30
|
#
|
30
|
-
# @example Store this model by default in a different
|
31
|
+
# @example Store this model by default in a different client.
|
31
32
|
# class Band
|
32
33
|
# include Mongoid::Document
|
33
|
-
# store_in
|
34
|
+
# store_in client: "secondary"
|
34
35
|
# end
|
35
36
|
#
|
36
37
|
# @example Store this model with a combination of options.
|
@@ -43,7 +44,7 @@ module Mongoid
|
|
43
44
|
#
|
44
45
|
# @option options [ String, Symbol ] :collection The collection name.
|
45
46
|
# @option options [ String, Symbol ] :database The database name.
|
46
|
-
# @option options [ String, Symbol ] :
|
47
|
+
# @option options [ String, Symbol ] :client The client name.
|
47
48
|
#
|
48
49
|
# @return [ Class ] The model class.
|
49
50
|
#
|
@@ -74,8 +75,8 @@ module Mongoid
|
|
74
75
|
def storage_options_defaults
|
75
76
|
{
|
76
77
|
collection: name.collectionize.to_sym,
|
77
|
-
|
78
|
-
database: -> {
|
78
|
+
client: :default,
|
79
|
+
database: -> { configured_database }
|
79
80
|
}
|
80
81
|
end
|
81
82
|
|
@@ -91,24 +92,26 @@ module Mongoid
|
|
91
92
|
__evaluate__(storage_options[:collection])
|
92
93
|
end
|
93
94
|
|
94
|
-
# Get the
|
95
|
+
# Get the client name for the model.
|
95
96
|
#
|
96
|
-
# @example Get the
|
97
|
-
# Model.
|
97
|
+
# @example Get the client name.
|
98
|
+
# Model.client_name
|
98
99
|
#
|
99
|
-
# @return [ Symbol ] The name of the
|
100
|
+
# @return [ Symbol ] The name of the client.
|
100
101
|
#
|
101
102
|
# @since 3.0.0
|
102
|
-
def
|
103
|
-
__evaluate__(storage_options[:
|
103
|
+
def client_name
|
104
|
+
__evaluate__(storage_options[:client])
|
104
105
|
end
|
106
|
+
alias :session_name :client_name
|
107
|
+
deprecate :session_name, :client_name, 2015, 12
|
105
108
|
|
106
109
|
# Get the database name for the model.
|
107
110
|
#
|
108
111
|
# @example Get the database name.
|
109
112
|
# Model.database_name
|
110
113
|
#
|
111
|
-
# @return [ Symbol ] The name of the
|
114
|
+
# @return [ Symbol ] The name of the client.
|
112
115
|
#
|
113
116
|
# @since 4.0.0
|
114
117
|
def database_name
|
@@ -134,6 +137,17 @@ module Mongoid
|
|
134
137
|
return nil unless name
|
135
138
|
name.respond_to?(:call) ? name.call.to_sym : name.to_sym
|
136
139
|
end
|
140
|
+
|
141
|
+
def configured_database
|
142
|
+
client = Mongoid.clients[client_name]
|
143
|
+
if db = client[:database]
|
144
|
+
db
|
145
|
+
elsif uri = client[:uri]
|
146
|
+
client[:database] = Mongo::URI.new(uri).database
|
147
|
+
else
|
148
|
+
nil
|
149
|
+
end
|
150
|
+
end
|
137
151
|
end
|
138
152
|
end
|
139
153
|
end
|