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
@@ -31,7 +31,7 @@ module Mongoid
31
31
  def apply_default_scoping
32
32
  if default_scoping
33
33
  default_scoping.call.selector.each do |field, value|
34
- attributes[field] = value unless value.respond_to?(:each_pair)
34
+ attributes[field] = value unless value.respond_to?(:each)
35
35
  end
36
36
  end
37
37
  end
@@ -115,7 +115,7 @@ module Mongoid
115
115
  #
116
116
  # @since 3.0.0
117
117
  def queryable
118
- scope_stack.last || Criteria.new(self)
118
+ Threaded.current_scope || Criteria.new(self)
119
119
  end
120
120
 
121
121
  # Create a scope that can be accessed from the class level or chained to
@@ -150,18 +150,6 @@ module Mongoid
150
150
  define_scope_method(normalized)
151
151
  end
152
152
 
153
- # Initializes and returns the current scope stack.
154
- #
155
- # @example Get the scope stack.
156
- # Person.scope_stack
157
- #
158
- # @return [ Array<Criteria> ] The scope stack.
159
- #
160
- # @since 1.0.0
161
- def scope_stack
162
- Threaded.scope_stack[object_id] ||= []
163
- end
164
-
165
153
  # Get a criteria for the document with normal scoping.
166
154
  #
167
155
  # @example Get the criteria.
@@ -234,11 +222,11 @@ module Mongoid
234
222
  #
235
223
  # @since 1.0.0
236
224
  def with_scope(criteria)
237
- scope_stack.push(criteria)
225
+ Threaded.current_scope = criteria
238
226
  begin
239
227
  yield criteria
240
228
  ensure
241
- scope_stack.pop
229
+ Threaded.current_scope = nil
242
230
  end
243
231
  end
244
232
 
@@ -325,7 +313,8 @@ module Mongoid
325
313
  singleton_class.class_eval do
326
314
  define_method name do |*args|
327
315
  scoping = _declared_scopes[name]
328
- scope, extension = scoping[:scope][*args], scoping[:extension]
316
+ scope = instance_exec(*args, &scoping[:scope])
317
+ extension = scoping[:extension]
329
318
  criteria = with_default_scope.merge(scope || queryable)
330
319
  criteria.extend(extension)
331
320
  criteria
@@ -18,42 +18,7 @@ module Mongoid
18
18
  #
19
19
  # @since 1.0.0
20
20
  def atomic_selector
21
- @atomic_selector ||=
22
- (embedded? ? embedded_atomic_selector : root_atomic_selector)
23
- end
24
-
25
- private
26
-
27
- # Get the atomic selector for an embedded document.
28
- #
29
- # @api private
30
- #
31
- # @example Get the embedded atomic selector.
32
- # document.embedded_atomic_selector
33
- #
34
- # @return [ Hash ] The embedded document selector.
35
- #
36
- # @since 4.0.0
37
- def embedded_atomic_selector
38
- if persisted? && _id_changed?
39
- _parent.atomic_selector
40
- else
41
- _parent.atomic_selector.merge("#{atomic_path}._id" => _id)
42
- end
43
- end
44
-
45
- # Get the atomic selector for a root document.
46
- #
47
- # @api private
48
- #
49
- # @example Get the root atomic selector.
50
- # document.root_atomic_selector
51
- #
52
- # @return [ Hash ] The root document selector.
53
- #
54
- # @since 4.0.0
55
- def root_atomic_selector
56
- { "_id" => _id }.merge!(shard_key_selector)
21
+ @atomic_selector ||= { "_id" => _root._id }.merge!(shard_key_selector)
57
22
  end
58
23
  end
59
24
  end
@@ -97,10 +97,10 @@ module Mongoid
97
97
  #
98
98
  # @since 3.0.0
99
99
  def serialize_attribute(attrs, name, names, options)
100
- if relations.has_key?(name)
100
+ if relations.key?(name)
101
101
  value = send(name)
102
102
  attrs[name] = value ? value.serializable_hash(options) : nil
103
- elsif names.include?(name) && !fields.has_key?(name)
103
+ elsif names.include?(name) && !fields.key?(name)
104
104
  attrs[name] = read_attribute(name)
105
105
  elsif !attribute_missing?(name)
106
106
  attrs[name] = send(name)
@@ -55,7 +55,6 @@ module Mongoid
55
55
  def destroyed?
56
56
  @destroyed ||= false
57
57
  end
58
- alias :deleted? :destroyed?
59
58
 
60
59
  # Determine if the document can be pushed.
61
60
  #
@@ -18,9 +18,9 @@ namespace :db do
18
18
  ::Mongoid::Tasks::Database.remove_indexes
19
19
  end
20
20
 
21
- desc "Drops the default session"
21
+ desc "Drops the default client database"
22
22
  task :drop => :environment do
23
- ::Mongoid::Sessions.default.drop
23
+ ::Mongoid::Clients.default.database.drop
24
24
  end
25
25
 
26
26
  desc "Drop all collections except the system collections"
@@ -42,18 +42,20 @@ module Mongoid
42
42
 
43
43
  models.each do |model|
44
44
  unless model.embedded?
45
- model.collection.indexes.each do |index|
46
- # ignore default index
47
- unless index['name'] == '_id_'
48
- key = index['key'].symbolize_keys
49
- spec = model.index_specification(key)
50
- unless spec
51
- # index not specified
52
- undefined_by_model[model] ||= []
53
- undefined_by_model[model] << index
45
+ begin
46
+ model.collection.indexes.each do |index|
47
+ # ignore default index
48
+ unless index['name'] == '_id_'
49
+ key = index['key'].symbolize_keys
50
+ spec = model.index_specification(key)
51
+ unless spec
52
+ # index not specified
53
+ undefined_by_model[model] ||= []
54
+ undefined_by_model[model] << index
55
+ end
54
56
  end
55
57
  end
56
- end
58
+ rescue Mongo::Error::OperationFailure; end
57
59
  end
58
60
  end
59
61
 
@@ -73,8 +75,12 @@ module Mongoid
73
75
  undefined_indexes(models).each do |model, indexes|
74
76
  indexes.each do |index|
75
77
  key = index['key'].symbolize_keys
76
- model.collection.indexes.drop(key)
77
- logger.info("MONGOID: Removing index: #{index['name']} on #{model}.")
78
+ collection = model.collection
79
+ collection.indexes.drop_one(key)
80
+ logger.info(
81
+ "MONGOID: Removed index '#{index['name']}' on collection " +
82
+ "'#{collection.name}' in database '#{collection.database.name}'."
83
+ )
78
84
  end
79
85
  end
80
86
  end
@@ -90,17 +96,18 @@ module Mongoid
90
96
  def remove_indexes(models = ::Mongoid.models)
91
97
  models.each do |model|
92
98
  next if model.embedded?
93
- indexes = model.collection.indexes.map{ |doc| doc["name"] }
94
- indexes.delete_one("_id_")
95
- model.remove_indexes
96
- logger.info("MONGOID: Removing indexes on: #{model} for: #{indexes.join(', ')}.")
99
+ begin
100
+ model.remove_indexes
101
+ rescue Mongo::Error::OperationFailure
102
+ next
103
+ end
97
104
  model
98
105
  end.compact
99
106
  end
100
107
 
101
108
  private
102
109
  def logger
103
- @logger ||= Logger.new($stdout)
110
+ Mongoid.logger
104
111
  end
105
112
  end
106
113
  end
@@ -1,7 +1,12 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
3
  module Threaded
4
-
4
+ BIND = 'bind'.freeze
5
+ ASSIGN = 'assign'.freeze
6
+ BUILD = 'build'.freeze
7
+ LOAD = 'load'.freeze
8
+ CREATE = 'create'.freeze
9
+
5
10
  # This module contains convenience methods for document lifecycle that
6
11
  # resides on thread locals.
7
12
  module Lifecycle
@@ -22,10 +27,10 @@ module Mongoid
22
27
  #
23
28
  # @since 2.2.0
24
29
  def _assigning
25
- Threaded.begin_execution("assign")
30
+ Threaded.begin_execution(ASSIGN)
26
31
  yield
27
32
  ensure
28
- Threaded.exit_execution("assign")
33
+ Threaded.exit_execution(ASSIGN)
29
34
  end
30
35
 
31
36
  # Is the current thread in assigning mode?
@@ -37,7 +42,7 @@ module Mongoid
37
42
  #
38
43
  # @since 2.1.0
39
44
  def _assigning?
40
- Threaded.executing?("assign")
45
+ Threaded.executing?(ASSIGN)
41
46
  end
42
47
 
43
48
  # Execute a block in binding mode.
@@ -51,10 +56,10 @@ module Mongoid
51
56
  #
52
57
  # @since 2.1.0
53
58
  def _binding
54
- Threaded.begin_execution("bind")
59
+ Threaded.begin_execution(BIND)
55
60
  yield
56
61
  ensure
57
- Threaded.exit_execution("bind")
62
+ Threaded.exit_execution(BIND)
58
63
  end
59
64
 
60
65
  # Is the current thread in binding mode?
@@ -66,7 +71,7 @@ module Mongoid
66
71
  #
67
72
  # @since 2.1.0
68
73
  def _binding?
69
- Threaded.executing?("bind")
74
+ Threaded.executing?(BIND)
70
75
  end
71
76
 
72
77
  # Execute a block in building mode.
@@ -80,10 +85,10 @@ module Mongoid
80
85
  #
81
86
  # @since 2.1.0
82
87
  def _building
83
- Threaded.begin_execution("build")
88
+ Threaded.begin_execution(BUILD)
84
89
  yield
85
90
  ensure
86
- Threaded.exit_execution("build")
91
+ Threaded.exit_execution(BUILD)
87
92
  end
88
93
 
89
94
  # Is the current thread in building mode?
@@ -95,7 +100,7 @@ module Mongoid
95
100
  #
96
101
  # @since 2.1.0
97
102
  def _building?
98
- Threaded.executing?("build")
103
+ Threaded.executing?(BUILD)
99
104
  end
100
105
 
101
106
  # Is the current thread in creating mode?
@@ -107,7 +112,7 @@ module Mongoid
107
112
  #
108
113
  # @since 2.1.0
109
114
  def _creating?
110
- Threaded.executing?("create")
115
+ Threaded.executing?(CREATE)
111
116
  end
112
117
 
113
118
  # Execute a block in loading mode.
@@ -121,10 +126,10 @@ module Mongoid
121
126
  #
122
127
  # @since 2.3.2
123
128
  def _loading
124
- Threaded.begin_execution("load")
129
+ Threaded.begin_execution(LOAD)
125
130
  yield
126
131
  ensure
127
- Threaded.exit_execution("load")
132
+ Threaded.exit_execution(LOAD)
128
133
  end
129
134
 
130
135
  # Is the current thread in loading mode?
@@ -136,7 +141,7 @@ module Mongoid
136
141
  #
137
142
  # @since 2.3.2
138
143
  def _loading?
139
- Threaded.executing?("load")
144
+ Threaded.executing?(LOAD)
140
145
  end
141
146
 
142
147
  module ClassMethods
@@ -152,10 +157,10 @@ module Mongoid
152
157
  #
153
158
  # @since 2.1.0
154
159
  def _creating
155
- Threaded.begin_execution("create")
160
+ Threaded.begin_execution(CREATE)
156
161
  yield
157
162
  ensure
158
- Threaded.exit_execution("create")
163
+ Threaded.exit_execution(CREATE)
159
164
  end
160
165
 
161
166
  end
@@ -6,10 +6,24 @@ module Mongoid
6
6
  # This module contains logic for easy access to objects that have a lifecycle
7
7
  # on the current thread.
8
8
  module Threaded
9
+
9
10
  DATABASE_OVERRIDE_KEY = "[mongoid]:db-override"
10
- SESSIONS_KEY = "[mongoid]:sessions"
11
- SESSION_OVERRIDE_KEY = "[mongoid]:session-override"
12
- SCOPE_STACK_KEY = "[mongoid]:scope-stack"
11
+
12
+ # Constant for the key to store clients.
13
+ #
14
+ # @since 5.0.0
15
+ CLIENTS_KEY = "[mongoid]:clients"
16
+
17
+ # The key to override the client.
18
+ #
19
+ # @since 5.0.0
20
+ CLIENT_OVERRIDE_KEY = "[mongoid]:client-override"
21
+
22
+ # The key for the current thread's scope stack.
23
+ #
24
+ # @since 2.0.0
25
+ CURRENT_SCOPE_KEY = "[mongoid]:current-scope"
26
+
13
27
  AUTOSAVES_KEY = "[mongoid]:autosaves"
14
28
  VALIDATIONS_KEY = "[mongoid]:validations"
15
29
 
@@ -17,6 +31,7 @@ module Mongoid
17
31
  hash[key] = "[mongoid]:#{key}-stack"
18
32
  end
19
33
 
34
+ extend Gem::Deprecate
20
35
  extend self
21
36
 
22
37
  # Begin entry into a named thread local stack.
@@ -59,18 +74,6 @@ module Mongoid
59
74
  Thread.current[DATABASE_OVERRIDE_KEY] = name
60
75
  end
61
76
 
62
- # Get the database sessions from the current thread.
63
- #
64
- # @example Get the database sessions.
65
- # Threaded.sessions
66
- #
67
- # @return [ Hash ] The sessions.
68
- #
69
- # @since 3.0.0
70
- def sessions
71
- Thread.current[SESSIONS_KEY] ||= {}
72
- end
73
-
74
77
  # Are in the middle of executing the named stack
75
78
  #
76
79
  # @example Are we in the stack execution?
@@ -161,42 +164,60 @@ module Mongoid
161
164
  validations_for(document.class).delete_one(document._id)
162
165
  end
163
166
 
164
- # Get the global session override.
167
+ # Get the global client override.
165
168
  #
166
- # @example Get the global session override.
167
- # Threaded.session_override
169
+ # @example Get the global client override.
170
+ # Threaded.client_override
168
171
  #
169
172
  # @return [ String, Symbol ] The override.
170
173
  #
171
- # @since 3.0.0
172
- def session_override
173
- Thread.current[SESSION_OVERRIDE_KEY]
174
+ # @since 5.0.0
175
+ def client_override
176
+ Thread.current[CLIENT_OVERRIDE_KEY]
174
177
  end
178
+ alias :session_override :client_override
179
+ deprecate :session_override, :client_override, 2015, 12
175
180
 
176
- # Set the global session override.
181
+ # Set the global client override.
177
182
  #
178
- # @example Set the global session override.
179
- # Threaded.session_override = :testing
183
+ # @example Set the global client override.
184
+ # Threaded.client_override = :testing
180
185
  #
181
186
  # @param [ String, Symbol ] The global override name.
182
187
  #
183
188
  # @return [ String, Symbol ] The override.
184
189
  #
185
190
  # @since 3.0.0
186
- def session_override=(name)
187
- Thread.current[SESSION_OVERRIDE_KEY] = name
191
+ def client_override=(name)
192
+ Thread.current[CLIENT_OVERRIDE_KEY] = name
193
+ end
194
+ alias :session_override= :client_override=
195
+ deprecate :session_override=, :client_override=, 2015, 12
196
+
197
+ # Get the current Mongoid scope.
198
+ #
199
+ # @example Get the scope.
200
+ # Threaded.current_scope
201
+ #
202
+ # @return [ Criteria ] The scope.
203
+ #
204
+ # @since 5.0.0
205
+ def current_scope
206
+ Thread.current[CURRENT_SCOPE_KEY]
188
207
  end
189
208
 
190
- # Get the mongoid scope stack for chained criteria.
209
+ # Set the current Mongoid scope.
210
+ #
211
+ # @example Set the scope.
212
+ # Threaded.current_scope = scope
191
213
  #
192
- # @example Get the scope stack.
193
- # Threaded.scope_stack
214
+ # @param [ Criteria ] scope The current scope.
194
215
  #
195
- # @return [ Hash ] The scope stack.
216
+ # @return [ Criteria ] The scope.
196
217
  #
197
- # @since 2.1.0
198
- def scope_stack
199
- Thread.current[SCOPE_STACK_KEY] ||= {}
218
+ # @since 5.0.0
219
+ def current_scope=(scope)
220
+ Thread.current[CURRENT_SCOPE_KEY] = scope
200
221
  end
201
222
 
202
223
  # Is the document autosaved on the current thread?
@@ -124,6 +124,22 @@ module Mongoid
124
124
  child.move_changes
125
125
  child.new_record = false
126
126
  end
127
+ _reset_memoized_children!
128
+ end
129
+
130
+ # Resets the memoized children on the object. Called internally when an
131
+ # embedded array changes size.
132
+ #
133
+ # @api semiprivate
134
+ #
135
+ # @example Reset the memoized children.
136
+ # document._reset_memoized_children!
137
+ #
138
+ # @return [ nil ] nil.
139
+ #
140
+ # @since 5.0.0
141
+ def _reset_memoized_children!
142
+ _parent._reset_memoized_children! if _parent
127
143
  @__children = nil
128
144
  end
129
145
 
@@ -161,7 +177,7 @@ module Mongoid
161
177
  #
162
178
  # @return [ true, false ] True if hereditary, false if not.
163
179
  def hereditary?
164
- Mongoid::Document > superclass
180
+ !!(Mongoid::Document > superclass)
165
181
  end
166
182
 
167
183
  # When inheriting, we want to copy the fields from the parent class and
@@ -4,7 +4,7 @@ module Mongoid
4
4
  module Queryable
5
5
 
6
6
  # Wrap the validation inside the an execution block that alert's the
7
- # session not to clear its persistence options.
7
+ # client not to clear its persistence options.
8
8
  #
9
9
  # @example Execute the validation with a query.
10
10
  # with_query(document) do
@@ -122,8 +122,8 @@ module Mongoid
122
122
  def criterion(document, attribute, value)
123
123
  field = document.database_field_name(attribute)
124
124
 
125
- if localized?(document, field)
126
- conditions = value.inject([]) { |acc, (k,v)| acc << { "#{field}.#{k}" => filter(v) } }
125
+ if value && localized?(document, field)
126
+ conditions = (value || {}).inject([]) { |acc, (k,v)| acc << { "#{field}.#{k}" => filter(v) }}
127
127
  selector = { "$or" => conditions }
128
128
  else
129
129
  selector = { field => filter(value) }
@@ -274,7 +274,7 @@ module Mongoid
274
274
  criteria = create_criteria(klass, document, attribute, value)
275
275
  criteria = criteria.merge(options[:conditions].call) if options[:conditions]
276
276
 
277
- if criteria.with(persistence_options(criteria)).exists?
277
+ if criteria.with(criteria.persistence_options).read(mode: :primary).exists?
278
278
  add_error(document, attribute, value)
279
279
  end
280
280
  end
@@ -296,23 +296,6 @@ module Mongoid
296
296
  scope_value_changed?(document)
297
297
  end
298
298
 
299
- # Get the persistence options to perform to check, merging with any
300
- # existing.
301
- #
302
- # @api private
303
- #
304
- # @example Get the persistence options.
305
- # validator.persistence_options(criteria)
306
- #
307
- # @param [ Criteria ] criteria The criteria.
308
- #
309
- # @return [ Hash ] The persistence options.
310
- #
311
- # @since 3.0.23
312
- def persistence_options(criteria)
313
- (criteria.persistence_options || {}).merge!(read: :primary)
314
- end
315
-
316
299
  # Is the attribute localized?
317
300
  #
318
301
  # @api private
@@ -68,7 +68,7 @@ module Mongoid
68
68
  # @since 2.0.0.rc.1
69
69
  def read_attribute_for_validation(attr)
70
70
  attribute = database_field_name(attr)
71
- if relations.has_key?(attribute)
71
+ if relations.key?(attribute)
72
72
  begin_validate
73
73
  relation = without_autobuild { send(attr) }
74
74
  exit_validate
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid
3
- VERSION = "4.0.2"
3
+ VERSION = "5.0.0"
4
4
  end
data/lib/mongoid.rb CHANGED
@@ -13,14 +13,13 @@ require "active_support/time_with_zone"
13
13
  require "active_model"
14
14
 
15
15
  require "origin"
16
- require "moped"
16
+ require "mongo"
17
17
 
18
18
  require "mongoid/version"
19
19
  require "mongoid/config"
20
20
  require "mongoid/loggable"
21
- require "mongoid/sessions"
21
+ require "mongoid/clients"
22
22
  require "mongoid/document"
23
- require "mongoid/log_subscriber"
24
23
  require "mongoid/tasks/database"
25
24
  require "mongoid/query_cache"
26
25
 
@@ -35,9 +34,11 @@ I18n.load_path << File.join(File.dirname(__FILE__), "config", "locales", "en.yml
35
34
 
36
35
  module Mongoid
37
36
  extend Loggable
37
+ extend Gem::Deprecate
38
38
  extend self
39
39
 
40
- MONGODB_VERSION = "2.2.0"
40
+ # The minimum MongoDB version supported.
41
+ MONGODB_VERSION = "2.4.0"
41
42
 
42
43
  # Sets the Mongoid configuration options. Best used by passing a block.
43
44
  #
@@ -53,41 +54,47 @@ module Mongoid
53
54
  block_given? ? yield(Config) : Config
54
55
  end
55
56
 
56
- # Convenience method for getting the default session.
57
+ # Convenience method for getting the default client.
57
58
  #
58
- # @example Get the default session.
59
- # Mongoid.default_session
59
+ # @example Get the default client.
60
+ # Mongoid.default_client
60
61
  #
61
- # @return [ Moped::Session ] The default session.
62
+ # @return [ Mongo::Client ] The default client.
62
63
  #
63
64
  # @since 3.0.0
64
- def default_session
65
- Sessions.default
65
+ def default_client
66
+ Clients.default
66
67
  end
68
+ alias :default_session :default_client
69
+ deprecate :default_session, :default_client, 2015, 12
67
70
 
68
- # Disconnect all active sessions.
71
+ # Disconnect all active clients.
69
72
  #
70
- # @example Disconnect all active sessions.
71
- # Mongoid.disconnect_sessions
73
+ # @example Disconnect all active clients.
74
+ # Mongoid.disconnect_clients
72
75
  #
73
76
  # @return [ true ] True.
74
77
  #
75
78
  # @since 3.1.0
76
- def disconnect_sessions
77
- Sessions.disconnect
79
+ def disconnect_clients
80
+ Clients.disconnect
78
81
  end
82
+ alias :disconnect_sessions :disconnect_clients
83
+ deprecate :disconnect_sessions, :disconnect_clients, 2015, 12
79
84
 
80
- # Convenience method for getting a named session.
85
+ # Convenience method for getting a named client.
81
86
  #
82
- # @example Get a named session.
83
- # Mongoid.session(:default)
87
+ # @example Get a named client.
88
+ # Mongoid.client(:default)
84
89
  #
85
- # @return [ Moped::Session ] The named session.
90
+ # @return [ Mongo::Client ] The named client.
86
91
  #
87
92
  # @since 3.0.0
88
- def session(name)
89
- Sessions.with_name(name)
93
+ def client(name)
94
+ Clients.with_name(name)
90
95
  end
96
+ alias :session :client
97
+ deprecate :session, :client, 2015, 12
91
98
 
92
99
  # Take all the public instance methods from the Config singleton and allow
93
100
  # them to be accessed through the Mongoid module directly.