mongoid 5.4.0 → 6.4.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (301) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/README.md +3 -3
  4. data/Rakefile +26 -0
  5. data/lib/config/locales/en.yml +40 -0
  6. data/lib/mongoid/atomic/modifiers.rb +2 -2
  7. data/lib/mongoid/atomic.rb +5 -5
  8. data/lib/mongoid/attributes/readonly.rb +22 -0
  9. data/lib/mongoid/attributes.rb +22 -21
  10. data/lib/mongoid/cacheable.rb +36 -0
  11. data/lib/mongoid/changeable.rb +36 -0
  12. data/lib/mongoid/clients/options.rb +55 -250
  13. data/lib/mongoid/clients/sessions.rb +113 -0
  14. data/lib/mongoid/clients/storage_options.rb +2 -69
  15. data/lib/mongoid/clients.rb +10 -63
  16. data/lib/mongoid/composable.rb +29 -2
  17. data/lib/mongoid/config.rb +1 -0
  18. data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
  19. data/lib/mongoid/contextual/atomic.rb +4 -4
  20. data/lib/mongoid/contextual/map_reduce.rb +7 -3
  21. data/lib/mongoid/contextual/memory.rb +9 -4
  22. data/lib/mongoid/contextual/mongo.rb +65 -30
  23. data/lib/mongoid/contextual/none.rb +12 -0
  24. data/lib/mongoid/copyable.rb +13 -6
  25. data/lib/mongoid/criteria/marshalable.rb +2 -2
  26. data/lib/mongoid/criteria/modifiable.rb +29 -3
  27. data/lib/mongoid/criteria/options.rb +25 -0
  28. data/lib/mongoid/criteria/queryable/aggregable.rb +120 -0
  29. data/lib/mongoid/criteria/queryable/extensions/array.rb +185 -0
  30. data/lib/mongoid/criteria/queryable/extensions/big_decimal.rb +37 -0
  31. data/lib/mongoid/criteria/queryable/extensions/boolean.rb +34 -0
  32. data/lib/mongoid/criteria/queryable/extensions/date.rb +63 -0
  33. data/lib/mongoid/criteria/queryable/extensions/date_time.rb +53 -0
  34. data/lib/mongoid/criteria/queryable/extensions/hash.rb +200 -0
  35. data/lib/mongoid/criteria/queryable/extensions/nil_class.rb +86 -0
  36. data/lib/mongoid/criteria/queryable/extensions/numeric.rb +90 -0
  37. data/lib/mongoid/criteria/queryable/extensions/object.rb +206 -0
  38. data/lib/mongoid/criteria/queryable/extensions/range.rb +70 -0
  39. data/lib/mongoid/criteria/queryable/extensions/regexp.rb +79 -0
  40. data/lib/mongoid/criteria/queryable/extensions/set.rb +34 -0
  41. data/lib/mongoid/criteria/queryable/extensions/string.rb +137 -0
  42. data/lib/mongoid/criteria/queryable/extensions/symbol.rb +79 -0
  43. data/lib/mongoid/criteria/queryable/extensions/time.rb +60 -0
  44. data/lib/mongoid/criteria/queryable/extensions/time_with_zone.rb +54 -0
  45. data/lib/mongoid/criteria/queryable/extensions.rb +28 -0
  46. data/lib/mongoid/criteria/queryable/forwardable.rb +65 -0
  47. data/lib/mongoid/criteria/queryable/key.rb +103 -0
  48. data/lib/mongoid/criteria/queryable/macroable.rb +27 -0
  49. data/lib/mongoid/criteria/queryable/mergeable.rb +273 -0
  50. data/lib/mongoid/criteria/queryable/optional.rb +429 -0
  51. data/lib/mongoid/criteria/queryable/options.rb +153 -0
  52. data/lib/mongoid/criteria/queryable/pipeline.rb +111 -0
  53. data/lib/mongoid/criteria/queryable/selectable.rb +689 -0
  54. data/lib/mongoid/criteria/queryable/selector.rb +212 -0
  55. data/lib/mongoid/criteria/queryable/smash.rb +104 -0
  56. data/lib/mongoid/criteria/queryable.rb +87 -0
  57. data/lib/mongoid/criteria.rb +6 -2
  58. data/lib/mongoid/document.rb +34 -41
  59. data/lib/mongoid/errors/ambiguous_relationship.rb +1 -1
  60. data/lib/mongoid/errors/in_memory_collation_not_supported.rb +1 -1
  61. data/lib/mongoid/errors/invalid_field.rb +2 -2
  62. data/lib/mongoid/errors/invalid_persistence_option.rb +29 -0
  63. data/lib/mongoid/errors/invalid_relation.rb +66 -0
  64. data/lib/mongoid/errors/invalid_session_use.rb +24 -0
  65. data/lib/mongoid/errors.rb +3 -0
  66. data/lib/mongoid/evolvable.rb +1 -1
  67. data/lib/mongoid/extensions/big_decimal.rb +17 -8
  68. data/lib/mongoid/extensions/date.rb +4 -1
  69. data/lib/mongoid/extensions/decimal128.rb +3 -3
  70. data/lib/mongoid/extensions/hash.rb +1 -0
  71. data/lib/mongoid/extensions/regexp.rb +1 -0
  72. data/lib/mongoid/extensions/string.rb +6 -3
  73. data/lib/mongoid/extensions/time.rb +4 -1
  74. data/lib/mongoid/extensions.rb +0 -4
  75. data/lib/mongoid/factory.rb +2 -1
  76. data/lib/mongoid/fields/validators/macro.rb +18 -0
  77. data/lib/mongoid/findable.rb +2 -2
  78. data/lib/mongoid/indexable.rb +16 -14
  79. data/lib/mongoid/interceptable.rb +9 -22
  80. data/lib/mongoid/matchable/all.rb +2 -2
  81. data/lib/mongoid/matchable/and.rb +3 -3
  82. data/lib/mongoid/matchable/default.rb +2 -2
  83. data/lib/mongoid/matchable/elem_match.rb +28 -0
  84. data/lib/mongoid/matchable/exists.rb +2 -2
  85. data/lib/mongoid/matchable/gt.rb +4 -2
  86. data/lib/mongoid/matchable/gte.rb +4 -2
  87. data/lib/mongoid/matchable/in.rb +2 -2
  88. data/lib/mongoid/matchable/lt.rb +4 -2
  89. data/lib/mongoid/matchable/lte.rb +4 -2
  90. data/lib/mongoid/matchable/ne.rb +2 -2
  91. data/lib/mongoid/matchable/nin.rb +2 -2
  92. data/lib/mongoid/matchable/nor.rb +37 -0
  93. data/lib/mongoid/matchable/or.rb +3 -3
  94. data/lib/mongoid/matchable/regexp.rb +3 -3
  95. data/lib/mongoid/matchable/size.rb +2 -2
  96. data/lib/mongoid/matchable.rb +16 -7
  97. data/lib/mongoid/persistable/creatable.rb +5 -3
  98. data/lib/mongoid/persistable/deletable.rb +5 -3
  99. data/lib/mongoid/persistable/destroyable.rb +1 -5
  100. data/lib/mongoid/persistable/settable.rb +5 -5
  101. data/lib/mongoid/persistable/updatable.rb +7 -14
  102. data/lib/mongoid/persistable/upsertable.rb +2 -1
  103. data/lib/mongoid/persistable.rb +4 -6
  104. data/lib/mongoid/persistence_context.rb +220 -0
  105. data/lib/mongoid/query_cache.rb +67 -23
  106. data/lib/mongoid/railtie.rb +17 -1
  107. data/lib/mongoid/railties/controller_runtime.rb +86 -0
  108. data/lib/mongoid/relations/accessors.rb +3 -0
  109. data/lib/mongoid/relations/auto_save.rb +12 -4
  110. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +4 -4
  111. data/lib/mongoid/relations/counter_cache.rb +15 -5
  112. data/lib/mongoid/relations/eager/base.rb +3 -3
  113. data/lib/mongoid/relations/eager/has_and_belongs_to_many.rb +2 -2
  114. data/lib/mongoid/relations/eager/has_many.rb +1 -1
  115. data/lib/mongoid/relations/eager.rb +6 -11
  116. data/lib/mongoid/relations/embedded/batchable.rb +20 -18
  117. data/lib/mongoid/relations/embedded/in.rb +13 -1
  118. data/lib/mongoid/relations/embedded/many.rb +51 -10
  119. data/lib/mongoid/relations/embedded/one.rb +14 -1
  120. data/lib/mongoid/relations/macros.rb +9 -1
  121. data/lib/mongoid/relations/many.rb +4 -0
  122. data/lib/mongoid/relations/metadata.rb +3 -3
  123. data/lib/mongoid/relations/options.rb +2 -2
  124. data/lib/mongoid/relations/proxy.rb +1 -31
  125. data/lib/mongoid/relations/referenced/in.rb +19 -10
  126. data/lib/mongoid/relations/referenced/many.rb +30 -26
  127. data/lib/mongoid/relations/referenced/many_to_many.rb +20 -13
  128. data/lib/mongoid/relations/referenced/one.rb +15 -1
  129. data/lib/mongoid/relations/synchronization.rb +12 -12
  130. data/lib/mongoid/relations/targets/enumerable.rb +24 -4
  131. data/lib/mongoid/relations/touchable.rb +7 -4
  132. data/lib/mongoid/reloadable.rb +2 -2
  133. data/lib/mongoid/scopable.rb +3 -3
  134. data/lib/mongoid/serializable.rb +1 -1
  135. data/lib/mongoid/stateful.rb +1 -0
  136. data/lib/mongoid/tasks/database.rb +3 -2
  137. data/lib/mongoid/threaded.rb +74 -0
  138. data/lib/mongoid/traversable.rb +1 -1
  139. data/lib/mongoid/validatable/uniqueness.rb +1 -2
  140. data/lib/mongoid/version.rb +1 -1
  141. data/lib/mongoid.rb +6 -6
  142. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +18 -3
  143. data/spec/app/models/agent.rb +2 -0
  144. data/spec/app/models/album.rb +5 -1
  145. data/spec/app/models/array_field.rb +7 -0
  146. data/spec/app/models/artist.rb +21 -0
  147. data/spec/app/models/band.rb +3 -0
  148. data/spec/app/models/book.rb +2 -1
  149. data/spec/app/models/delegating_patient.rb +16 -0
  150. data/spec/app/models/dokument.rb +1 -0
  151. data/spec/app/models/ordered_post.rb +5 -0
  152. data/spec/app/models/oscar.rb +1 -2
  153. data/spec/app/models/page.rb +1 -1
  154. data/spec/app/models/person.rb +3 -3
  155. data/spec/app/models/princess.rb +2 -0
  156. data/spec/app/models/record.rb +1 -0
  157. data/spec/app/models/subscription.rb +1 -0
  158. data/spec/app/models/thing.rb +1 -1
  159. data/spec/config/mongoid.yml +15 -0
  160. data/spec/integration/document_spec.rb +22 -0
  161. data/spec/mongoid/atomic/modifiers_spec.rb +3 -3
  162. data/spec/mongoid/atomic_spec.rb +5 -5
  163. data/spec/mongoid/attributes/nested_spec.rb +18 -14
  164. data/spec/mongoid/attributes/readonly_spec.rb +87 -44
  165. data/spec/mongoid/attributes_spec.rb +90 -5
  166. data/spec/mongoid/cacheable_spec.rb +112 -0
  167. data/spec/mongoid/changeable_spec.rb +58 -0
  168. data/spec/mongoid/clients/factory_spec.rb +80 -28
  169. data/spec/mongoid/clients/options_spec.rb +396 -95
  170. data/spec/mongoid/clients/sessions_spec.rb +334 -0
  171. data/spec/mongoid/clients_spec.rb +243 -101
  172. data/spec/mongoid/composable_spec.rb +7 -0
  173. data/spec/mongoid/config_spec.rb +67 -11
  174. data/spec/mongoid/contextual/atomic_spec.rb +3 -3
  175. data/spec/mongoid/contextual/geo_near_spec.rb +1 -0
  176. data/spec/mongoid/contextual/mongo_spec.rb +275 -22
  177. data/spec/mongoid/contextual/none_spec.rb +15 -0
  178. data/spec/mongoid/copyable_spec.rb +13 -4
  179. data/spec/mongoid/criteria/modifiable_spec.rb +297 -16
  180. data/spec/mongoid/criteria/options_spec.rb +29 -0
  181. data/spec/mongoid/criteria/queryable/aggregable_spec.rb +370 -0
  182. data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +523 -0
  183. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +59 -0
  184. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +58 -0
  185. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +213 -0
  186. data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +330 -0
  187. data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +405 -0
  188. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +58 -0
  189. data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +65 -0
  190. data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +327 -0
  191. data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +65 -0
  192. data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +77 -0
  193. data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +108 -0
  194. data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +309 -0
  195. data/spec/mongoid/{extensions/origin → criteria/queryable/extensions}/regexp_raw_spec.rb +2 -2
  196. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +90 -0
  197. data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +39 -0
  198. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +302 -0
  199. data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +167 -0
  200. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +376 -0
  201. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +347 -0
  202. data/spec/mongoid/criteria/queryable/forwardable_spec.rb +87 -0
  203. data/spec/mongoid/criteria/queryable/key_spec.rb +52 -0
  204. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +49 -0
  205. data/spec/mongoid/criteria/queryable/optional_spec.rb +1799 -0
  206. data/spec/mongoid/criteria/queryable/options_spec.rb +360 -0
  207. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +200 -0
  208. data/spec/mongoid/criteria/queryable/queryable_spec.rb +137 -0
  209. data/spec/mongoid/criteria/queryable/selectable_spec.rb +4242 -0
  210. data/spec/mongoid/criteria/queryable/selector_spec.rb +844 -0
  211. data/spec/mongoid/criteria/queryable/smash_spec.rb +30 -0
  212. data/spec/mongoid/criteria/scopable_spec.rb +81 -0
  213. data/spec/mongoid/criteria_spec.rb +156 -22
  214. data/spec/mongoid/document_spec.rb +100 -90
  215. data/spec/mongoid/errors/invalid_relation_spec.rb +37 -0
  216. data/spec/mongoid/errors/mongoid_error_spec.rb +6 -3
  217. data/spec/mongoid/extensions/big_decimal_spec.rb +321 -19
  218. data/spec/mongoid/extensions/boolean_spec.rb +14 -0
  219. data/spec/mongoid/extensions/date_spec.rb +2 -6
  220. data/spec/mongoid/extensions/date_time_spec.rb +2 -6
  221. data/spec/mongoid/extensions/decimal128_spec.rb +1 -1
  222. data/spec/mongoid/extensions/float_spec.rb +8 -1
  223. data/spec/mongoid/extensions/hash_spec.rb +15 -0
  224. data/spec/mongoid/extensions/integer_spec.rb +8 -1
  225. data/spec/mongoid/extensions/object_spec.rb +11 -0
  226. data/spec/mongoid/extensions/regexp_spec.rb +23 -0
  227. data/spec/mongoid/extensions/string_spec.rb +53 -4
  228. data/spec/mongoid/extensions/time_spec.rb +2 -6
  229. data/spec/mongoid/extensions/time_with_zone_spec.rb +2 -6
  230. data/spec/mongoid/factory_spec.rb +11 -0
  231. data/spec/mongoid/fields_spec.rb +1 -1
  232. data/spec/mongoid/findable_spec.rb +47 -2
  233. data/spec/mongoid/indexable_spec.rb +15 -3
  234. data/spec/mongoid/interceptable_spec.rb +85 -19
  235. data/spec/mongoid/matchable/all_spec.rb +4 -4
  236. data/spec/mongoid/matchable/and_spec.rb +10 -10
  237. data/spec/mongoid/matchable/default_spec.rb +12 -12
  238. data/spec/mongoid/matchable/elem_match_spec.rb +86 -0
  239. data/spec/mongoid/matchable/exists_spec.rb +5 -5
  240. data/spec/mongoid/matchable/gt_spec.rb +18 -7
  241. data/spec/mongoid/matchable/gte_spec.rb +17 -7
  242. data/spec/mongoid/matchable/in_spec.rb +5 -5
  243. data/spec/mongoid/matchable/lt_spec.rb +18 -7
  244. data/spec/mongoid/matchable/lte_spec.rb +18 -7
  245. data/spec/mongoid/matchable/ne_spec.rb +5 -5
  246. data/spec/mongoid/matchable/nin_spec.rb +5 -5
  247. data/spec/mongoid/matchable/nor_spec.rb +209 -0
  248. data/spec/mongoid/matchable/or_spec.rb +7 -7
  249. data/spec/mongoid/matchable/regexp_spec.rb +5 -5
  250. data/spec/mongoid/matchable/size_spec.rb +3 -3
  251. data/spec/mongoid/matchable_spec.rb +199 -54
  252. data/spec/mongoid/persistable/creatable_spec.rb +7 -2
  253. data/spec/mongoid/persistable/deletable_spec.rb +35 -1
  254. data/spec/mongoid/persistable/destroyable_spec.rb +25 -2
  255. data/spec/mongoid/persistable/incrementable_spec.rb +6 -6
  256. data/spec/mongoid/persistable/savable_spec.rb +34 -29
  257. data/spec/mongoid/persistable/settable_spec.rb +77 -27
  258. data/spec/mongoid/persistable/updatable_spec.rb +182 -3
  259. data/spec/mongoid/persistable_spec.rb +16 -16
  260. data/spec/mongoid/persistence_context_spec.rb +694 -0
  261. data/spec/mongoid/positional_spec.rb +1 -1
  262. data/spec/mongoid/query_cache_spec.rb +170 -12
  263. data/spec/mongoid/relations/accessors_spec.rb +1 -1
  264. data/spec/mongoid/relations/auto_save_spec.rb +39 -6
  265. data/spec/mongoid/relations/bindings/referenced/many_to_many_spec.rb +4 -4
  266. data/spec/mongoid/relations/builders_spec.rb +37 -10
  267. data/spec/mongoid/relations/counter_cache_spec.rb +64 -3
  268. data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +16 -0
  269. data/spec/mongoid/relations/eager_spec.rb +40 -0
  270. data/spec/mongoid/relations/embedded/many_spec.rb +305 -59
  271. data/spec/mongoid/relations/embedded/one_spec.rb +2 -1
  272. data/spec/mongoid/relations/macros_spec.rb +415 -7
  273. data/spec/mongoid/relations/metadata_spec.rb +15 -1
  274. data/spec/mongoid/relations/proxy_spec.rb +27 -1
  275. data/spec/mongoid/relations/referenced/in_spec.rb +41 -1
  276. data/spec/mongoid/relations/referenced/many_spec.rb +35 -25
  277. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +14 -26
  278. data/spec/mongoid/relations/synchronization_spec.rb +48 -2
  279. data/spec/mongoid/relations/targets/enumerable_spec.rb +108 -0
  280. data/spec/mongoid/relations/touchable_spec.rb +40 -0
  281. data/spec/mongoid/reloadable_spec.rb +51 -0
  282. data/spec/mongoid/scopable_spec.rb +13 -0
  283. data/spec/mongoid/serializable_spec.rb +0 -50
  284. data/spec/mongoid/threaded_spec.rb +68 -0
  285. data/spec/mongoid/validatable/presence_spec.rb +1 -1
  286. data/spec/mongoid/validatable/uniqueness_spec.rb +18 -9
  287. data/spec/mongoid/validatable_spec.rb +16 -0
  288. data/spec/rails/controller_extension/controller_runtime_spec.rb +110 -0
  289. data/spec/spec_helper.rb +101 -8
  290. data/spec/support/cluster_config.rb +158 -0
  291. data/spec/support/constraints.rb +101 -0
  292. data/spec/support/macros.rb +20 -0
  293. data/spec/support/session_registry.rb +50 -0
  294. data/spec/support/spec_config.rb +42 -0
  295. data.tar.gz.sig +0 -0
  296. metadata +163 -61
  297. metadata.gz.sig +0 -0
  298. data/lib/mongoid/clients/thread_options.rb +0 -19
  299. data/lib/mongoid/extensions/origin/regexp_raw.rb +0 -43
  300. data/lib/mongoid/railties/document.rb +0 -12
  301. data/spec/mongoid/railties/document_spec.rb +0 -24
@@ -6,34 +6,38 @@ describe Mongoid::Clients do
6
6
 
7
7
  shared_examples_for "an overridden collection at the class level" do
8
8
 
9
- let(:band) do
10
- klass.new
11
- end
12
-
13
9
  it "returns the collection for the model" do
14
- expect(band.collection).to be_a(Mongo::Collection)
10
+ expect(instance_collection).to be_a(Mongo::Collection)
15
11
  end
16
12
 
17
13
  it "sets the correct collection name" do
18
- expect(band.collection.name).to eq("artists")
14
+ expect(instance_collection.name).to eq("artists")
19
15
  end
20
16
 
21
17
  context "when accessing from the class level" do
22
18
 
23
19
  it "returns the collection for the model" do
24
- expect(klass.collection).to be_a(Mongo::Collection)
20
+ expect(class_collection).to be_a(Mongo::Collection)
25
21
  end
26
22
 
27
23
  it "sets the correct collection name" do
28
- expect(klass.collection.name).to eq("artists")
24
+ expect(class_collection.name).to eq("artists")
29
25
  end
30
26
  end
31
27
  end
32
28
 
33
29
  context "when overriding the persistence options" do
34
30
 
35
- let(:klass) do
36
- Band.with(collection: "artists")
31
+ let(:instance_collection) do
32
+ Band.with(collection: "artists") do |klass|
33
+ klass.new.collection
34
+ end
35
+ end
36
+
37
+ let(:class_collection) do
38
+ Band.with(collection: "artists") do |klass|
39
+ klass.collection
40
+ end
37
41
  end
38
42
 
39
43
  it_behaves_like "an overridden collection at the class level"
@@ -41,10 +45,6 @@ describe Mongoid::Clients do
41
45
 
42
46
  context "when overriding store_in and persistence options" do
43
47
 
44
- let(:klass) do
45
- Band.with(collection: "artists")
46
- end
47
-
48
48
  before do
49
49
  Band.store_in collection: "foo"
50
50
  end
@@ -53,6 +53,18 @@ describe Mongoid::Clients do
53
53
  Band.reset_storage_options!
54
54
  end
55
55
 
56
+ let(:instance_collection) do
57
+ Band.with(collection: "artists") do |klass|
58
+ klass.new.collection
59
+ end
60
+ end
61
+
62
+ let(:class_collection) do
63
+ Band.with(collection: "artists") do |klass|
64
+ klass.collection
65
+ end
66
+ end
67
+
56
68
  it_behaves_like "an overridden collection at the class level"
57
69
  end
58
70
 
@@ -77,19 +89,23 @@ describe Mongoid::Clients do
77
89
 
78
90
  context "when overriding with a proc" do
79
91
 
80
- let(:klass) { Band }
81
-
82
92
  before do
83
93
  Band.store_in(collection: ->{ "artists" })
84
94
  end
85
95
 
96
+ let(:instance_collection) do
97
+ Band.new.collection
98
+ end
99
+
100
+ let(:class_collection) do
101
+ Band.collection
102
+ end
103
+
86
104
  it_behaves_like "an overridden collection at the class level"
87
105
  end
88
106
 
89
107
  context "when overriding with a string" do
90
108
 
91
- let(:klass) { Band }
92
-
93
109
  before do
94
110
  Band.store_in(collection: "artists")
95
111
  end
@@ -98,19 +114,33 @@ describe Mongoid::Clients do
98
114
  Band.reset_storage_options!
99
115
  end
100
116
 
117
+ let(:instance_collection) do
118
+ Band.new.collection
119
+ end
120
+
121
+ let(:class_collection) do
122
+ Band.collection
123
+ end
124
+
101
125
  it_behaves_like "an overridden collection at the class level"
102
126
  end
103
127
 
104
128
  context "when overriding with a symbol" do
105
129
 
106
- let(:klass) { Band }
107
-
108
130
  before do
109
- klass.store_in(collection: :artists)
131
+ Band.store_in(collection: :artists)
110
132
  end
111
133
 
112
134
  after do
113
- klass.reset_storage_options!
135
+ Band.reset_storage_options!
136
+ end
137
+
138
+ let(:instance_collection) do
139
+ Band.new.collection
140
+ end
141
+
142
+ let(:class_collection) do
143
+ Band.collection
114
144
  end
115
145
 
116
146
  it_behaves_like "an overridden collection at the class level"
@@ -148,29 +178,33 @@ describe Mongoid::Clients do
148
178
 
149
179
  shared_examples_for "an overridden collection name at the class level" do
150
180
 
151
- let(:band) do
152
- klass.new
153
- end
154
-
155
181
  context "when accessing from the instance" do
156
182
 
157
183
  it "returns the overridden value" do
158
- expect(band.collection_name).to eq(:artists)
184
+ expect(instance_collection_name).to eq(:artists)
159
185
  end
160
186
  end
161
187
 
162
188
  context "when accessing from the class level" do
163
189
 
164
190
  it "returns the overridden value" do
165
- expect(klass.collection_name).to eq(:artists)
191
+ expect(class_collection_name).to eq(:artists)
166
192
  end
167
193
  end
168
194
  end
169
195
 
170
196
  context "when overriding the persistence options" do
171
197
 
172
- let(:klass) do
173
- Band.with(collection: "artists")
198
+ let(:instance_collection_name) do
199
+ Band.with(collection: "artists") do |klass|
200
+ klass.new.collection_name
201
+ end
202
+ end
203
+
204
+ let(:class_collection_name) do
205
+ Band.with(collection: "artists") do |klass|
206
+ klass.collection_name
207
+ end
174
208
  end
175
209
 
176
210
  it_behaves_like "an overridden collection name at the class level"
@@ -178,8 +212,16 @@ describe Mongoid::Clients do
178
212
 
179
213
  context "when overriding store_in and persistence options" do
180
214
 
181
- let(:klass) do
182
- Band.with(collection: "artists")
215
+ let(:instance_collection_name) do
216
+ Band.with(collection: "artists") do |klass|
217
+ klass.new.collection_name
218
+ end
219
+ end
220
+
221
+ let(:class_collection_name) do
222
+ Band.with(collection: "artists") do |klass|
223
+ klass.collection_name
224
+ end
183
225
  end
184
226
 
185
227
  before do
@@ -195,7 +237,13 @@ describe Mongoid::Clients do
195
237
 
196
238
  context "when overriding the default with store_in" do
197
239
 
198
- let(:klass) { Band }
240
+ let(:instance_collection_name) do
241
+ Band.new.collection_name
242
+ end
243
+
244
+ let(:class_collection_name) do
245
+ Band.collection_name
246
+ end
199
247
 
200
248
  after do
201
249
  Band.reset_storage_options!
@@ -266,38 +314,51 @@ describe Mongoid::Clients do
266
314
  end
267
315
  end
268
316
 
269
- describe "#database_name", if: non_legacy_server? do
317
+ describe "#database_name" do
270
318
 
271
319
  shared_examples_for "an overridden database name" do
272
320
 
273
- let(:band) do
274
- klass.new
321
+ after do
322
+ class_mongo_client.close
275
323
  end
276
324
 
277
325
  context "when accessing from the instance" do
278
326
 
279
327
  it "returns the overridden value" do
280
- # @todo
281
- # expect(band.mongo_client.options[:database].to_s).to eq(database_id_alt)
328
+ expect(instance_database.name.to_s).to eq(database_id_alt)
282
329
  end
283
330
  end
284
331
 
285
332
  context "when accessing from the class level" do
286
333
 
287
334
  it "returns the overridden value" do
288
- expect(klass.database_name.to_s).to eq(database_id_alt)
335
+ expect(class_database.name.to_s).to eq(database_id_alt)
289
336
  end
290
337
 
291
338
  it "client returns the overridden value" do
292
- expect(klass.mongo_client.options[:database].to_s).to eq(database_id_alt)
339
+ expect(class_mongo_client.database.name).to eq(database_id_alt)
293
340
  end
294
341
  end
295
342
  end
296
343
 
297
344
  context "when overriding the persistence options" do
298
345
 
299
- let(:klass) do
300
- Band.with(database: database_id_alt)
346
+ let(:instance_database) do
347
+ Band.with(database: database_id_alt) do |klass|
348
+ klass.new.mongo_client.database
349
+ end
350
+ end
351
+
352
+ let(:class_database) do
353
+ Band.with(database: database_id_alt) do |klass|
354
+ klass.mongo_client.database
355
+ end
356
+ end
357
+
358
+ let(:class_mongo_client) do
359
+ Band.with(database: database_id_alt) do |klass|
360
+ klass.new.mongo_client
361
+ end
301
362
  end
302
363
 
303
364
  it_behaves_like "an overridden database name"
@@ -305,7 +366,17 @@ describe Mongoid::Clients do
305
366
 
306
367
  context "when overriding with store_in" do
307
368
 
308
- let(:klass) { Band }
369
+ let(:instance_database) do
370
+ Band.new.mongo_client.database
371
+ end
372
+
373
+ let(:class_database) do
374
+ Band.mongo_client.database
375
+ end
376
+
377
+ let(:class_mongo_client) do
378
+ Band.mongo_client
379
+ end
309
380
 
310
381
  before do
311
382
  Band.store_in database: database_id_alt
@@ -313,6 +384,7 @@ describe Mongoid::Clients do
313
384
 
314
385
  after do
315
386
  Band.reset_storage_options!
387
+ class_mongo_client.close
316
388
  end
317
389
 
318
390
  it_behaves_like "an overridden database name"
@@ -320,8 +392,22 @@ describe Mongoid::Clients do
320
392
 
321
393
  context "when overriding store_in and persistence options" do
322
394
 
323
- let(:klass) do
324
- Band.with(database: database_id_alt)
395
+ let(:instance_database) do
396
+ Band.with(database: database_id_alt) do |klass|
397
+ klass.new.mongo_client.database
398
+ end
399
+ end
400
+
401
+ let(:class_database) do
402
+ Band.with(database: database_id_alt) do |klass|
403
+ klass.mongo_client.database
404
+ end
405
+ end
406
+
407
+ let(:class_mongo_client) do
408
+ Band.with(database: database_id_alt) do |klass|
409
+ klass.new.mongo_client
410
+ end
325
411
  end
326
412
 
327
413
  before do
@@ -330,6 +416,7 @@ describe Mongoid::Clients do
330
416
 
331
417
  after do
332
418
  Band.reset_storage_options!
419
+ class_mongo_client.close
333
420
  end
334
421
 
335
422
  it_behaves_like "an overridden database name"
@@ -349,8 +436,22 @@ describe Mongoid::Clients do
349
436
 
350
437
  context "when overriding the persistence options" do
351
438
 
352
- let(:klass) do
353
- Band.with(client: client_name)
439
+ let(:instance_database) do
440
+ Band.with(client: :alternative) do |klass|
441
+ klass.new.mongo_client.database
442
+ end
443
+ end
444
+
445
+ let(:class_database) do
446
+ Band.with(client: :alternative) do |klass|
447
+ klass.mongo_client.database
448
+ end
449
+ end
450
+
451
+ let(:class_mongo_client) do
452
+ Band.with(client: :alternative) do |klass|
453
+ klass.new.mongo_client
454
+ end
354
455
  end
355
456
 
356
457
  it_behaves_like "an overridden database name"
@@ -358,7 +459,17 @@ describe Mongoid::Clients do
358
459
 
359
460
  context "when overriding with store_in" do
360
461
 
361
- let(:klass) { Band }
462
+ let(:instance_database) do
463
+ Band.new.mongo_client.database
464
+ end
465
+
466
+ let(:class_database) do
467
+ Band.mongo_client.database
468
+ end
469
+
470
+ let(:class_mongo_client) do
471
+ Band.mongo_client
472
+ end
362
473
 
363
474
  before do
364
475
  Band.store_in(client: client_name)
@@ -366,6 +477,7 @@ describe Mongoid::Clients do
366
477
 
367
478
  after do
368
479
  Band.reset_storage_options!
480
+ class_mongo_client.close
369
481
  end
370
482
 
371
483
  it_behaves_like "an overridden database name"
@@ -373,7 +485,7 @@ describe Mongoid::Clients do
373
485
  end
374
486
  end
375
487
 
376
- describe "#mongo_client", if: non_legacy_server? do
488
+ describe "#mongo_client" do
377
489
 
378
490
  let(:file) do
379
491
  File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
@@ -397,6 +509,10 @@ describe Mongoid::Clients do
397
509
  Mongoid.clients[:default][:database] = database_id
398
510
  end
399
511
 
512
+ after do
513
+ mongo_client.close
514
+ end
515
+
400
516
  let!(:band) do
401
517
  Band.new
402
518
  end
@@ -424,10 +540,6 @@ describe Mongoid::Clients do
424
540
  Band.store_in(client: :nonexistent)
425
541
  end
426
542
 
427
- after do
428
- Band.reset_storage_options!
429
- end
430
-
431
543
  let(:band) do
432
544
  Band.new
433
545
  end
@@ -439,7 +551,7 @@ describe Mongoid::Clients do
439
551
  end
440
552
  end
441
553
 
442
- context "when getting a client by name", if: testing_locally? do
554
+ context "when getting a client by name" do
443
555
 
444
556
  let(:file) do
445
557
  File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
@@ -457,11 +569,7 @@ describe Mongoid::Clients do
457
569
  Band.reset_storage_options!
458
570
  end
459
571
 
460
- let!(:band) do
461
- Band.store_in(client: :reports)
462
- end
463
-
464
- let!(:mongo_client) do
572
+ let(:mongo_client) do
465
573
  Band.new.mongo_client
466
574
  end
467
575
 
@@ -497,7 +605,7 @@ describe Mongoid::Clients do
497
605
  end
498
606
  end
499
607
 
500
- describe ".mongo_client", if: non_legacy_server? do
608
+ describe ".mongo_client" do
501
609
 
502
610
  let(:file) do
503
611
  File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
@@ -526,7 +634,7 @@ describe Mongoid::Clients do
526
634
  Mongoid.clients[:default][:database] = database_id
527
635
  end
528
636
 
529
- let(:mongo_client) do
637
+ let!(:mongo_client) do
530
638
  Band.mongo_client
531
639
  end
532
640
 
@@ -568,7 +676,7 @@ describe Mongoid::Clients do
568
676
  end
569
677
  end
570
678
 
571
- context "when provided a class that extend another document" do
679
+ context "when provided a class that extends another document" do
572
680
 
573
681
  let(:klass) do
574
682
  Class.new(Band)
@@ -599,7 +707,9 @@ describe Mongoid::Clients do
599
707
  context "when changing write concern options" do
600
708
 
601
709
  let(:client_one) do
602
- Band.with(write: { w: 2 }).mongo_client
710
+ Band.with(write: { w: 2 }) do |klass|
711
+ klass.mongo_client
712
+ end
603
713
  end
604
714
 
605
715
  let(:client_two) do
@@ -614,13 +724,17 @@ describe Mongoid::Clients do
614
724
  context "when sending operations to a different database" do
615
725
 
616
726
  after do
617
- Band.with(database: database_id_alt).delete_all
727
+ Band.with(database: database_id_alt) do |klass|
728
+ klass.delete_all
729
+ end
618
730
  end
619
731
 
620
732
  describe ".create" do
621
733
 
622
734
  let!(:band) do
623
- Band.with(database: database_id_alt).create
735
+ Band.with(database: database_id_alt) do |klass|
736
+ klass.create
737
+ end
624
738
  end
625
739
 
626
740
  it "does not persist to the default database" do
@@ -630,15 +744,23 @@ describe Mongoid::Clients do
630
744
  end
631
745
 
632
746
  let(:from_db) do
633
- Band.with(database: database_id_alt).find(band.id)
747
+ Band.with(database: database_id_alt) do |klass|
748
+ klass.find(band.id)
749
+ end
634
750
  end
635
751
 
636
752
  it "persists to the specified database" do
637
753
  expect(from_db).to eq(band)
638
754
  end
639
755
 
756
+ let(:count) do
757
+ Band.with(database: database_id_alt) do |klass|
758
+ klass.count
759
+ end
760
+ end
761
+
640
762
  it "persists the correct number of documents" do
641
- expect(Band.with(database: database_id_alt).count).to eq(1)
763
+ expect(count).to eq(1)
642
764
  end
643
765
  end
644
766
  end
@@ -648,7 +770,9 @@ describe Mongoid::Clients do
648
770
  describe ".create" do
649
771
 
650
772
  let!(:band) do
651
- Band.with(collection: "artists").create
773
+ Band.with(collection: "artists") do |klass|
774
+ klass.create
775
+ end
652
776
  end
653
777
 
654
778
  it "does not persist to the default database" do
@@ -658,15 +782,23 @@ describe Mongoid::Clients do
658
782
  end
659
783
 
660
784
  let(:from_db) do
661
- Band.with(collection: "artists").find(band.id)
785
+ Band.with(collection: "artists") do |klass|
786
+ klass.find(band.id)
787
+ end
662
788
  end
663
789
 
664
790
  it "persists to the specified database" do
665
791
  expect(from_db).to eq(band)
666
792
  end
667
793
 
794
+ let(:count) do
795
+ Band.with(collection: "artists") do |klass|
796
+ klass.count
797
+ end
798
+ end
799
+
668
800
  it "persists the correct number of documents" do
669
- expect(Band.with(collection: "artists").count).to eq(1)
801
+ expect(count).to eq(1)
670
802
  end
671
803
  end
672
804
  end
@@ -676,10 +808,15 @@ describe Mongoid::Clients do
676
808
  describe ".create" do
677
809
 
678
810
  before do
811
+ Person.index({ ssn: 1 }, { unique: true })
679
812
  Person.create_indexes
680
813
  Person.create(ssn: "432-97-1111")
681
814
  end
682
815
 
816
+ after do
817
+ Person.collection.drop
818
+ end
819
+
683
820
  context "when no error occurs" do
684
821
 
685
822
  it "inserts the document" do
@@ -703,6 +840,10 @@ describe Mongoid::Clients do
703
840
  Person.create!(ssn: "432-97-1112")
704
841
  end
705
842
 
843
+ after do
844
+ Person.collection.drop
845
+ end
846
+
706
847
  context "when no error occurs" do
707
848
 
708
849
  it "inserts the document" do
@@ -713,9 +854,14 @@ describe Mongoid::Clients do
713
854
  context "when a mongodb error occurs" do
714
855
 
715
856
  before do
857
+ Person.index({ ssn: 1 }, { unique: true })
716
858
  Person.create_indexes
717
859
  end
718
860
 
861
+ after do
862
+ Person.collection.drop
863
+ end
864
+
719
865
  it "bubbles up to the caller" do
720
866
  expect {
721
867
  Person.create!(ssn: "432-97-1112")
@@ -746,9 +892,14 @@ describe Mongoid::Clients do
746
892
  end
747
893
 
748
894
  before do
895
+ Person.index({ ssn: 1 }, { unique: true })
749
896
  Person.create_indexes
750
897
  end
751
898
 
899
+ after do
900
+ Person.collection.drop
901
+ end
902
+
752
903
  it "bubbles up to the caller" do
753
904
  expect {
754
905
  person.save
@@ -770,9 +921,14 @@ describe Mongoid::Clients do
770
921
  end
771
922
 
772
923
  before do
924
+ Person.index({ ssn: 1 }, { unique: true })
773
925
  Person.create_indexes
774
926
  end
775
927
 
928
+ after do
929
+ Person.collection.drop
930
+ end
931
+
776
932
  it "bubbles up to the caller" do
777
933
  expect {
778
934
  person.save!
@@ -796,7 +952,7 @@ describe Mongoid::Clients do
796
952
  end
797
953
  end
798
954
 
799
- context "when overriding the default database", if: non_legacy_server? do
955
+ context "when overriding the default database" do
800
956
 
801
957
  let(:file) do
802
958
  File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
@@ -808,42 +964,28 @@ describe Mongoid::Clients do
808
964
 
809
965
  context "when the override is global" do
810
966
 
811
- shared_examples_for "a global database override" do
812
-
813
- before do
814
- Mongoid.override_database(:mongoid_optional)
815
- end
816
-
817
- after do
818
- Band.delete_all
819
- Mongoid.override_database(nil)
820
- end
821
-
822
- let!(:band) do
823
- klass.create(name: "Tool")
824
- end
825
-
826
- it "persists to the overridden database" do
827
- mongo_client = Band.mongo_client.with(database: :mongoid_optional)
828
- expect(mongo_client[:bands].count(name: "Tool")).to eq(1)
829
- end
830
-
831
- it 'uses that database for the model mongo_client' do
832
- expect(Band.mongo_client.database.name).to eq('mongoid_optional')
833
- end
967
+ before do
968
+ Mongoid.override_database(:mongoid_optional)
834
969
  end
835
970
 
836
- context "when normal usage" do
837
- let(:klass) { Band }
838
-
839
- it_behaves_like "a global database override"
971
+ after do
972
+ Band.delete_all
973
+ Band.mongo_client.close
974
+ Mongoid.override_database(nil)
840
975
  end
841
976
 
842
- context "when overriding the persistence options" do
977
+ let!(:band) do
978
+ Band.create(name: "Tool")
979
+ end
843
980
 
844
- let(:klass) { Band.with(connect_timeout: 10) }
981
+ it "persists to the overridden database" do
982
+ Band.mongo_client.with(database: :mongoid_optional) do |sess|
983
+ expect(sess[:bands].find(name: "Tool")).to_not be_nil
984
+ end
985
+ end
845
986
 
846
- it_behaves_like "a global database override"
987
+ it 'uses that database for the model mongo_client' do
988
+ expect(Band.mongo_client.database.name).to eq('mongoid_optional')
847
989
  end
848
990
  end
849
991
  end
@@ -20,5 +20,12 @@ describe Mongoid::Composable do
20
20
  end
21
21
  end
22
22
  end
23
+
24
+ Mongoid::Composable::RESERVED_METHOD_NAMES.each do |name|
25
+
26
+ it "includes the method names #{name}" do
27
+ expect(methods).to include(name)
28
+ end
29
+ end
23
30
  end
24
31
  end