mongoid 4.0.2 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +66 -1
  3. data/README.md +11 -19
  4. data/lib/config/locales/en.yml +33 -30
  5. data/lib/mongoid/atomic.rb +2 -4
  6. data/lib/mongoid/attributes/processing.rb +4 -1
  7. data/lib/mongoid/attributes/readonly.rb +2 -2
  8. data/lib/mongoid/attributes.rb +7 -7
  9. data/lib/mongoid/changeable.rb +4 -6
  10. data/lib/mongoid/clients/factory.rb +78 -0
  11. data/lib/mongoid/{sessions → clients}/options.rb +30 -19
  12. data/lib/mongoid/{sessions → clients}/storage_options.rb +27 -13
  13. data/lib/mongoid/{sessions → clients}/thread_options.rb +6 -3
  14. data/lib/mongoid/{sessions → clients}/validators/storage.rb +5 -2
  15. data/lib/mongoid/clients/validators.rb +2 -0
  16. data/lib/mongoid/clients.rb +141 -0
  17. data/lib/mongoid/composable.rb +3 -3
  18. data/lib/mongoid/config/environment.rb +1 -1
  19. data/lib/mongoid/config/options.rb +1 -1
  20. data/lib/mongoid/config/validators/{session.rb → client.rb} +31 -28
  21. data/lib/mongoid/config/validators/option.rb +1 -1
  22. data/lib/mongoid/config/validators.rb +1 -1
  23. data/lib/mongoid/config.rb +39 -41
  24. data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
  25. data/lib/mongoid/contextual/atomic.rb +11 -11
  26. data/lib/mongoid/contextual/command.rb +9 -6
  27. data/lib/mongoid/contextual/geo_near.rb +17 -1
  28. data/lib/mongoid/contextual/map_reduce.rb +12 -11
  29. data/lib/mongoid/contextual/memory.rb +2 -5
  30. data/lib/mongoid/contextual/mongo.rb +92 -82
  31. data/lib/mongoid/contextual/none.rb +13 -0
  32. data/lib/mongoid/copyable.rb +6 -1
  33. data/lib/mongoid/criteria.rb +37 -4
  34. data/lib/mongoid/document.rb +3 -4
  35. data/lib/mongoid/errors/{mixed_session_configuration.rb → mixed_client_configuration.rb} +5 -5
  36. data/lib/mongoid/errors/mongoid_error.rb +15 -9
  37. data/lib/mongoid/errors/no_client_config.rb +22 -0
  38. data/lib/mongoid/errors/{no_session_database.rb → no_client_database.rb} +4 -4
  39. data/lib/mongoid/errors/{no_session_hosts.rb → no_client_hosts.rb} +4 -4
  40. data/lib/mongoid/errors/{no_sessions_config.rb → no_clients_config.rb} +4 -4
  41. data/lib/mongoid/errors/no_default_client.rb +23 -0
  42. data/lib/mongoid/errors.rb +6 -6
  43. data/lib/mongoid/extensions/hash.rb +5 -1
  44. data/lib/mongoid/extensions/object.rb +3 -2
  45. data/lib/mongoid/extensions/set.rb +5 -5
  46. data/lib/mongoid/factory.rb +4 -2
  47. data/lib/mongoid/fields.rb +7 -2
  48. data/lib/mongoid/findable.rb +4 -1
  49. data/lib/mongoid/indexable.rb +15 -9
  50. data/lib/mongoid/persistable/creatable.rb +2 -2
  51. data/lib/mongoid/persistable/deletable.rb +3 -3
  52. data/lib/mongoid/persistable/incrementable.rb +1 -1
  53. data/lib/mongoid/persistable/logical.rb +1 -1
  54. data/lib/mongoid/persistable/poppable.rb +1 -1
  55. data/lib/mongoid/persistable/pullable.rb +2 -2
  56. data/lib/mongoid/persistable/pushable.rb +2 -2
  57. data/lib/mongoid/persistable/renamable.rb +1 -1
  58. data/lib/mongoid/persistable/settable.rb +1 -1
  59. data/lib/mongoid/persistable/unsettable.rb +1 -1
  60. data/lib/mongoid/persistable/updatable.rb +2 -2
  61. data/lib/mongoid/persistable/upsertable.rb +1 -1
  62. data/lib/mongoid/persistable.rb +1 -2
  63. data/lib/mongoid/query_cache.rb +98 -104
  64. data/lib/mongoid/railtie.rb +4 -25
  65. data/lib/mongoid/railties/database.rake +1 -1
  66. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +3 -2
  67. data/lib/mongoid/relations/builders.rb +3 -1
  68. data/lib/mongoid/relations/counter_cache.rb +1 -1
  69. data/lib/mongoid/relations/embedded/batchable.rb +3 -10
  70. data/lib/mongoid/relations/embedded/many.rb +4 -2
  71. data/lib/mongoid/relations/embedded/one.rb +1 -0
  72. data/lib/mongoid/relations/many.rb +1 -0
  73. data/lib/mongoid/relations/metadata.rb +14 -0
  74. data/lib/mongoid/relations/proxy.rb +6 -6
  75. data/lib/mongoid/relations/referenced/many.rb +2 -1
  76. data/lib/mongoid/relations/targets/enumerable.rb +11 -11
  77. data/lib/mongoid/relations/touchable.rb +1 -1
  78. data/lib/mongoid/reloadable.rb +2 -2
  79. data/lib/mongoid/scopable.rb +6 -17
  80. data/lib/mongoid/selectable.rb +1 -36
  81. data/lib/mongoid/serializable.rb +2 -2
  82. data/lib/mongoid/stateful.rb +0 -1
  83. data/lib/mongoid/tasks/database.rake +2 -2
  84. data/lib/mongoid/tasks/database.rb +24 -17
  85. data/lib/mongoid/threaded/lifecycle.rb +21 -16
  86. data/lib/mongoid/threaded.rb +54 -33
  87. data/lib/mongoid/traversable.rb +17 -1
  88. data/lib/mongoid/validatable/queryable.rb +1 -1
  89. data/lib/mongoid/validatable/uniqueness.rb +3 -20
  90. data/lib/mongoid/validatable.rb +1 -1
  91. data/lib/mongoid/version.rb +1 -1
  92. data/lib/mongoid.rb +28 -21
  93. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +91 -57
  94. data/lib/rails/mongoid.rb +2 -2
  95. data/spec/app/models/audio.rb +1 -1
  96. data/spec/app/models/band.rb +1 -0
  97. data/spec/app/models/code.rb +6 -0
  98. data/spec/app/models/company.rb +5 -0
  99. data/spec/app/models/label.rb +7 -0
  100. data/spec/app/models/pub.rb +6 -0
  101. data/spec/app/models/staff.rb +7 -0
  102. data/spec/app/models/store_as_dup_test1.rb +5 -0
  103. data/spec/app/models/store_as_dup_test2.rb +5 -0
  104. data/spec/config/mongoid.yml +7 -25
  105. data/spec/mongoid/atomic/paths_spec.rb +3 -11
  106. data/spec/mongoid/attributes/nested_spec.rb +16 -16
  107. data/spec/mongoid/attributes/readonly_spec.rb +80 -18
  108. data/spec/mongoid/attributes_spec.rb +3 -3
  109. data/spec/mongoid/changeable_spec.rb +156 -0
  110. data/spec/mongoid/clients/factory_spec.rb +284 -0
  111. data/spec/mongoid/{sessions → clients}/options_spec.rb +4 -6
  112. data/spec/mongoid/clients_spec.rb +754 -0
  113. data/spec/mongoid/config/environment_spec.rb +14 -11
  114. data/spec/mongoid/config_spec.rb +33 -48
  115. data/spec/mongoid/contextual/atomic_spec.rb +1 -17
  116. data/spec/mongoid/contextual/geo_near_spec.rb +35 -0
  117. data/spec/mongoid/contextual/mongo_spec.rb +26 -83
  118. data/spec/mongoid/contextual/none_spec.rb +15 -0
  119. data/spec/mongoid/copyable_spec.rb +35 -1
  120. data/spec/mongoid/criteria/findable_spec.rb +197 -0
  121. data/spec/mongoid/criteria/modifiable_spec.rb +7 -29
  122. data/spec/mongoid/criteria_spec.rb +74 -91
  123. data/spec/mongoid/document_spec.rb +1 -1
  124. data/spec/mongoid/errors/{mixed_session_configuration_spec.rb → mixed_client_configuration_spec.rb} +1 -1
  125. data/spec/mongoid/errors/mongoid_error_spec.rb +19 -3
  126. data/spec/mongoid/errors/{no_session_config_spec.rb → no_client_config_spec.rb} +4 -4
  127. data/spec/mongoid/errors/{no_session_database_spec.rb → no_client_database_spec.rb} +4 -4
  128. data/spec/mongoid/errors/{no_session_hosts_spec.rb → no_client_hosts_spec.rb} +3 -3
  129. data/spec/mongoid/errors/{no_sessions_config_spec.rb → no_clients_config_spec.rb} +2 -2
  130. data/spec/mongoid/fields/localized_spec.rb +1 -0
  131. data/spec/mongoid/fields_spec.rb +1 -0
  132. data/spec/mongoid/findable_spec.rb +2 -23
  133. data/spec/mongoid/indexable_spec.rb +12 -8
  134. data/spec/mongoid/interceptable_spec.rb +15 -0
  135. data/spec/mongoid/persistable/settable_spec.rb +16 -0
  136. data/spec/mongoid/persistable/updatable_spec.rb +3 -2
  137. data/spec/mongoid/persistable_spec.rb +4 -4
  138. data/spec/mongoid/query_cache_spec.rb +13 -8
  139. data/spec/mongoid/relations/auto_save_spec.rb +1 -1
  140. data/spec/mongoid/relations/counter_cache_spec.rb +34 -0
  141. data/spec/mongoid/relations/eager/belongs_to_spec.rb +9 -0
  142. data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +3 -3
  143. data/spec/mongoid/relations/embedded/many_spec.rb +123 -1
  144. data/spec/mongoid/relations/embedded/one_spec.rb +3 -3
  145. data/spec/mongoid/relations/proxy_spec.rb +28 -0
  146. data/spec/mongoid/relations/referenced/in_spec.rb +1 -1
  147. data/spec/mongoid/relations/referenced/many_spec.rb +47 -23
  148. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +1 -1
  149. data/spec/mongoid/relations/referenced/one_spec.rb +1 -1
  150. data/spec/mongoid/relations/targets/enumerable_spec.rb +9 -2
  151. data/spec/mongoid/reloadable_spec.rb +6 -6
  152. data/spec/mongoid/scopable_spec.rb +41 -28
  153. data/spec/mongoid/selectable_spec.rb +6 -16
  154. data/spec/mongoid/tasks/database_rake_spec.rb +13 -13
  155. data/spec/mongoid/tasks/database_spec.rb +2 -2
  156. data/spec/mongoid/threaded_spec.rb +0 -7
  157. data/spec/mongoid/traversable_spec.rb +2 -2
  158. data/spec/mongoid/validatable/uniqueness_spec.rb +30 -1
  159. data/spec/mongoid_spec.rb +13 -15
  160. data/spec/rails/mongoid_spec.rb +13 -4
  161. data/spec/spec_helper.rb +44 -27
  162. data/spec/support/authorization.rb +12 -0
  163. data/spec/support/expectations.rb +14 -0
  164. metadata +50 -57
  165. data/lib/mongoid/contextual/find_and_modify.rb +0 -69
  166. data/lib/mongoid/contextual/text_search.rb +0 -178
  167. data/lib/mongoid/criteria/#findable.rb# +0 -141
  168. data/lib/mongoid/errors/no_default_session.rb +0 -23
  169. data/lib/mongoid/errors/no_session_config.rb +0 -22
  170. data/lib/mongoid/log_subscriber.rb +0 -55
  171. data/lib/mongoid/positional.rb +0 -71
  172. data/lib/mongoid/sessions/factory.rb +0 -131
  173. data/lib/mongoid/sessions/mongo_uri.rb +0 -93
  174. data/lib/mongoid/sessions/validators.rb +0 -2
  175. data/lib/mongoid/sessions.rb +0 -125
  176. data/lib/mongoid/support/query_counter.rb +0 -23
  177. data/spec/helpers.rb +0 -18
  178. data/spec/mongoid/#atomic_spec.rb# +0 -365
  179. data/spec/mongoid/contextual/find_and_modify_spec.rb +0 -220
  180. data/spec/mongoid/contextual/text_search_spec.rb +0 -209
  181. data/spec/mongoid/log_subscriber_spec.rb +0 -75
  182. data/spec/mongoid/positional_spec.rb +0 -222
  183. data/spec/mongoid/sessions/factory_spec.rb +0 -333
  184. data/spec/mongoid/sessions/mongo_uri_spec.rb +0 -103
  185. data/spec/mongoid/sessions_spec.rb +0 -1252
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- module Sessions
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 session.
31
+ # @example Store this model by default in a different client.
31
32
  # class Band
32
33
  # include Mongoid::Document
33
- # store_in session: "secondary"
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 ] :session The session name.
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
- session: :default,
78
- database: -> { Mongoid.sessions[session_name][: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 session name for the model.
95
+ # Get the client name for the model.
95
96
  #
96
- # @example Get the session name.
97
- # Model.session_name
97
+ # @example Get the client name.
98
+ # Model.client_name
98
99
  #
99
- # @return [ Symbol ] The name of the session.
100
+ # @return [ Symbol ] The name of the client.
100
101
  #
101
102
  # @since 3.0.0
102
- def session_name
103
- __evaluate__(storage_options[:session])
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 session.
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
@@ -1,14 +1,17 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- module Sessions
3
+ module Clients
4
4
  module ThreadOptions
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  module ClassMethods
8
+ extend Gem::Deprecate
8
9
 
9
- def session_name
10
- Threaded.session_override || super
10
+ def client_name
11
+ Threaded.client_override || super
11
12
  end
13
+ alias :session_name :client_name
14
+ deprecate :session_name, :client_name, 2015, 12
12
15
 
13
16
  def database_name
14
17
  Threaded.database_override || super
@@ -1,13 +1,16 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- module Sessions
3
+ module Clients
4
4
  module Validators
5
5
 
6
6
  # Validates the options passed to :store_in.
7
7
  module Storage
8
8
  extend self
9
9
 
10
- VALID_OPTIONS = [ :collection, :database, :session ]
10
+ # The valid options for storage.
11
+ #
12
+ # @since 3.0.0
13
+ VALID_OPTIONS = [ :collection, :database, :client ].freeze
11
14
 
12
15
  # Validate the options provided to :store_in.
13
16
  #
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "mongoid/clients/validators/storage"
@@ -0,0 +1,141 @@
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.with(self.persistence_options)
122
+ end
123
+ alias :mongo_session :mongo_client
124
+ deprecate :mongo_session, :mongo_client, 2015, 12
125
+
126
+ # Get the collection for this model from the client. Will check for an
127
+ # overridden collection name from the store_in macro or the collection
128
+ # with a pluralized model name.
129
+ #
130
+ # @example Get the model's collection.
131
+ # Model.collection
132
+ #
133
+ # @return [ Mongo::Collection ] The collection.
134
+ #
135
+ # @since 3.0.0
136
+ def collection
137
+ mongo_client[collection_name]
138
+ end
139
+ end
140
+ end
141
+ end
@@ -35,6 +35,7 @@ module Mongoid
35
35
  include ActiveModel::Serializers::Xml
36
36
  include Atomic
37
37
  include Changeable
38
+ include Clients
38
39
  include Attributes
39
40
  include Evolvable
40
41
  include Fields
@@ -47,7 +48,6 @@ module Mongoid
47
48
  include Scopable
48
49
  include Selectable
49
50
  include Serializable
50
- include Sessions
51
51
  include Shardable
52
52
  include Stateful
53
53
  include Threaded::Lifecycle
@@ -73,7 +73,7 @@ module Mongoid
73
73
  Reloadable,
74
74
  Scopable,
75
75
  Serializable,
76
- Sessions,
76
+ Clients,
77
77
  Shardable,
78
78
  Stateful,
79
79
  Threaded::Lifecycle,
@@ -97,7 +97,7 @@ module Mongoid
97
97
  # @since 2.1.8
98
98
  def prohibited_methods
99
99
  @prohibited_methods ||= MODULES.flat_map do |mod|
100
- mod.instance_methods.map{ |m| m.to_sym }
100
+ mod.instance_methods.map(&:to_sym)
101
101
  end
102
102
  end
103
103
  end
@@ -19,7 +19,7 @@ module Mongoid
19
19
  #
20
20
  # @since 2.3.0
21
21
  def env_name
22
- return Rails.env if defined?(Rails)
22
+ return Rails.env if defined?(Rails) && Rails.respond_to?(:env)
23
23
  return Sinatra::Base.environment.to_s if defined?(Sinatra)
24
24
  ENV["RACK_ENV"] || ENV["MONGOID_ENV"] || raise(Errors::NoEnvironment.new)
25
25
  end
@@ -20,7 +20,7 @@ module Mongoid
20
20
  # Define a configuration option with a default.
21
21
  #
22
22
  # @example Define the option.
23
- # Options.option(:persist_in_safe_mode, :default => true)
23
+ # Options.option(:logger, :default => Logger.new($stdout, :warn))
24
24
  #
25
25
  # @param [ Symbol ] name The name of the configuration option.
26
26
  # @param [ Hash ] options Extras for the option.
@@ -3,82 +3,85 @@ module Mongoid
3
3
  module Config
4
4
  module Validators
5
5
 
6
- # Validator for session specific configuration.
7
- module Session
6
+ # Validator for client specific configuration.
7
+ module Client
8
8
  extend self
9
9
 
10
- STANDARD = [ :database, :hosts, :username, :password ]
10
+ # Standard configuration options.
11
+ #
12
+ # @since 3.0.0
13
+ STANDARD = [ :database, :hosts, :username, :password ].freeze
11
14
 
12
- # Validate the session configuration.
15
+ # Validate the client configuration.
13
16
  #
14
- # @example Validate the session config.
15
- # Session.validate({ default: { hosts: [ "localhost:27017" ] }})
17
+ # @example Validate the client config.
18
+ # Client.validate({ default: { hosts: [ "localhost:27017" ] }})
16
19
  #
17
- # @param [ Hash ] sessions The sessions config.
20
+ # @param [ Hash ] clients The clients config.
18
21
  #
19
22
  # @since 3.0.0
20
- def validate(sessions)
21
- unless sessions.has_key?(:default)
22
- raise Errors::NoDefaultSession.new(sessions.keys)
23
+ def validate(clients)
24
+ unless clients.has_key?(:default)
25
+ raise Errors::NoDefaultClient.new(clients.keys)
23
26
  end
24
- sessions.each_pair do |name, config|
25
- validate_session_database(name, config)
26
- validate_session_hosts(name, config)
27
- validate_session_uri(name, config)
27
+ clients.each_pair do |name, config|
28
+ validate_client_database(name, config)
29
+ validate_client_hosts(name, config)
30
+ validate_client_uri(name, config)
28
31
  end
29
32
  end
30
33
 
31
34
  private
32
35
 
33
- # Validate that the session config has database.
36
+ # Validate that the client config has database.
34
37
  #
35
38
  # @api private
36
39
  #
37
- # @example Validate the session has database.
38
- # validator.validate_session_database(:default, {})
40
+ # @example Validate the client has database.
41
+ # validator.validate_client_database(:default, {})
39
42
  #
40
43
  # @param [ String, Symbol ] name The config key.
41
44
  # @param [ Hash ] config The configuration.
42
45
  #
43
46
  # @since 3.0.0
44
- def validate_session_database(name, config)
47
+ def validate_client_database(name, config)
45
48
  if no_database_or_uri?(config)
46
- raise Errors::NoSessionDatabase.new(name, config)
49
+ raise Errors::NoClientDatabase.new(name, config)
47
50
  end
48
51
  end
49
52
 
50
- # Validate that the session config has hosts.
53
+ # Validate that the client config has hosts.
51
54
  #
52
55
  # @api private
53
56
  #
54
- # @example Validate the session has hosts.
55
- # validator.validate_session_hosts(:default, {})
57
+ # @example Validate the client has hosts.
58
+ # validator.validate_client_hosts(:default, {})
56
59
  #
57
60
  # @param [ String, Symbol ] name The config key.
58
61
  # @param [ Hash ] config The configuration.
59
62
  #
60
63
  # @since 3.0.0
61
- def validate_session_hosts(name, config)
64
+ def validate_client_hosts(name, config)
62
65
  if no_hosts_or_uri?(config)
63
- raise Errors::NoSessionHosts.new(name, config)
66
+ raise Errors::NoClientHosts.new(name, config)
64
67
  end
65
68
  end
66
69
 
67
70
  # Validate that not both a uri and standard options are provided for a
68
- # single session.
71
+ # single client.
69
72
  #
70
73
  # @api private
71
74
  #
72
75
  # @example Validate the uri and options.
73
- # validator.validate_session_uri(:default, {})
76
+ # validator.validate_client_uri(:default, {})
74
77
  #
75
78
  # @param [ String, Symbol ] name The config key.
76
79
  # @param [ Hash ] config The configuration.
77
80
  #
78
81
  # @since 3.0.0
79
- def validate_session_uri(name, config)
82
+ def validate_client_uri(name, config)
80
83
  if both_uri_and_standard?(config)
81
- raise Errors::MixedSessionConfiguration.new(name, config)
84
+ raise Errors::MixedClientConfiguration.new(name, config)
82
85
  end
83
86
  end
84
87
 
@@ -9,7 +9,7 @@ module Mongoid
9
9
 
10
10
  # Validate a configuration option.
11
11
  #
12
- # @example Validate a configuraiton option.
12
+ # @example Validate a configuration option.
13
13
  #
14
14
  # @param [ String ] option The name of the option.
15
15
  #
@@ -1,3 +1,3 @@
1
1
  # encoding: utf-8
2
2
  require "mongoid/config/validators/option"
3
- require "mongoid/config/validators/session"
3
+ require "mongoid/config/validators/client"
@@ -8,6 +8,7 @@ module Mongoid
8
8
  # This module defines all the configuration options for Mongoid, including the
9
9
  # database connections.
10
10
  module Config
11
+ extend Gem::Deprecate
11
12
  extend self
12
13
  extend Options
13
14
 
@@ -26,7 +27,7 @@ module Mongoid
26
27
  option :use_utc, default: false
27
28
 
28
29
  # Has Mongoid been configured? This is checking that at least a valid
29
- # session config exists.
30
+ # client config exists.
30
31
  #
31
32
  # @example Is Mongoid configured?
32
33
  # config.configured?
@@ -35,10 +36,10 @@ module Mongoid
35
36
  #
36
37
  # @since 3.0.9
37
38
  def configured?
38
- sessions.has_key?(:default)
39
+ clients.key?(:default)
39
40
  end
40
41
 
41
- # Connect to the provided database name on the default session.
42
+ # Connect to the provided database name on the default client.
42
43
  #
43
44
  # @note Use only in development or test environments for convenience.
44
45
  #
@@ -48,8 +49,8 @@ module Mongoid
48
49
  # @param [ String ] name The database name.
49
50
  #
50
51
  # @since 3.0.0
51
- def connect_to(name, options = { read: :primary })
52
- self.sessions = {
52
+ def connect_to(name, options = { read: { mode: :primary }})
53
+ self.clients = {
53
54
  default: {
54
55
  database: name,
55
56
  hosts: [ "localhost:27017" ],
@@ -82,8 +83,8 @@ module Mongoid
82
83
  def load!(path, environment = nil)
83
84
  settings = Environment.load_yaml(path, environment)
84
85
  if settings.present?
85
- Sessions.disconnect
86
- Sessions.clear
86
+ Clients.disconnect
87
+ Clients.clear
87
88
  load_configuration(settings)
88
89
  end
89
90
  settings
@@ -127,7 +128,7 @@ module Mongoid
127
128
  def load_configuration(settings)
128
129
  configuration = settings.with_indifferent_access
129
130
  self.options = configuration[:options]
130
- self.sessions = configuration[:sessions]
131
+ self.clients = configuration[:clients]
131
132
  end
132
133
 
133
134
  # Override the database to use globally.
@@ -144,19 +145,21 @@ module Mongoid
144
145
  Threaded.database_override = name
145
146
  end
146
147
 
147
- # Override the session to use globally.
148
+ # Override the client to use globally.
148
149
  #
149
- # @example Override the session globally.
150
- # config.override_session(:optional)
150
+ # @example Override the client globally.
151
+ # config.override_client(:optional)
151
152
  #
152
- # @param [ String, Symbol ] name The name of the session.
153
+ # @param [ String, Symbol ] name The name of the client.
153
154
  #
154
155
  # @return [ String, Symbol ] The global override.
155
156
  #
156
157
  # @since 3.0.0
157
- def override_session(name)
158
- Threaded.session_override = name ? name.to_s : nil
158
+ def override_client(name)
159
+ Threaded.client_override = name ? name.to_s : nil
159
160
  end
161
+ alias :override_session :override_client
162
+ deprecate :override_session, :override_client, 2015, 12
160
163
 
161
164
  # Purge all data in all collections, including indexes.
162
165
  #
@@ -169,9 +172,7 @@ module Mongoid
169
172
  #
170
173
  # @since 2.0.2
171
174
  def purge!
172
- Sessions.default.collections.each do |collection|
173
- collection.drop
174
- end and true
175
+ Clients.default.database.collections.each(&:drop) and true
175
176
  end
176
177
 
177
178
  # Truncate all data in all collections, but not the indexes.
@@ -185,8 +186,8 @@ module Mongoid
185
186
  #
186
187
  # @since 2.0.2
187
188
  def truncate!
188
- Sessions.default.collections.each do |collection|
189
- collection.find.remove_all
189
+ Clients.default.database.collections.each do |collection|
190
+ collection.find.delete_many
190
191
  end and true
191
192
  end
192
193
 
@@ -207,33 +208,19 @@ module Mongoid
207
208
  end
208
209
  end
209
210
 
210
- # Get the session configuration or an empty hash.
211
- #
212
- # @example Get the sessions configuration.
213
- # config.sessions
214
- #
215
- # @return [ Hash ] The sessions configuration.
216
- #
217
- # @since 3.0.0
218
- def sessions
219
- @sessions ||= {}
220
- end
221
-
222
- # Set the session configuration options.
211
+ # Get the client configuration or an empty hash.
223
212
  #
224
- # @example Set the session configuration options.
225
- # config.sessions = { default: { hosts: [ "localhost:27017" ] }}
213
+ # @example Get the clients configuration.
214
+ # config.clients
226
215
  #
227
- # @param [ Hash ] sessions The configuration options.
216
+ # @return [ Hash ] The clients configuration.
228
217
  #
229
218
  # @since 3.0.0
230
- def sessions=(sessions)
231
- raise Errors::NoSessionsConfig.new unless sessions
232
- sess = sessions.with_indifferent_access
233
- Validators::Session.validate(sess)
234
- @sessions = sess
235
- sess
219
+ def clients
220
+ @clients ||= {}
236
221
  end
222
+ alias :sessions :clients
223
+ deprecate :sessions, :clients, 2015, 12
237
224
 
238
225
  # Get the time zone to use.
239
226
  #
@@ -258,5 +245,16 @@ module Mongoid
258
245
  def running_with_passenger?
259
246
  @running_with_passenger ||= defined?(PhusionPassenger)
260
247
  end
248
+
249
+ private
250
+
251
+ def clients=(clients)
252
+ raise Errors::NoClientsConfig.new unless clients
253
+ c = clients.with_indifferent_access
254
+ Validators::Client.validate(c)
255
+ @clients = c
256
+ end
257
+ alias :sessions= :clients=
258
+ deprecate :sessions=, :clients=, 2015, 12
261
259
  end
262
260
  end
@@ -23,7 +23,7 @@ module Mongoid
23
23
  #
24
24
  # @since 3.0.0
25
25
  def aggregates(field)
26
- result = collection.aggregate(pipeline(field)).to_a
26
+ result = collection.find.aggregate(pipeline(field)).to_a
27
27
  if result.empty?
28
28
  { "count" => 0, "sum" => nil, "avg" => nil, "min" => nil, "max" => nil }
29
29
  else