mongoid 5.4.1 → 6.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (260) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +3 -3
  3. data/lib/config/locales/en.yml +19 -0
  4. data/lib/mongoid.rb +4 -4
  5. data/lib/mongoid/atomic.rb +2 -2
  6. data/lib/mongoid/atomic/modifiers.rb +8 -12
  7. data/lib/mongoid/attributes.rb +22 -21
  8. data/lib/mongoid/attributes/readonly.rb +22 -0
  9. data/lib/mongoid/cacheable.rb +36 -0
  10. data/lib/mongoid/changeable.rb +36 -0
  11. data/lib/mongoid/clients.rb +8 -63
  12. data/lib/mongoid/clients/options.rb +55 -250
  13. data/lib/mongoid/clients/storage_options.rb +1 -69
  14. data/lib/mongoid/composable.rb +29 -3
  15. data/lib/mongoid/config.rb +1 -0
  16. data/lib/mongoid/contextual/atomic.rb +5 -8
  17. data/lib/mongoid/contextual/map_reduce.rb +0 -4
  18. data/lib/mongoid/contextual/memory.rb +2 -2
  19. data/lib/mongoid/contextual/mongo.rb +40 -22
  20. data/lib/mongoid/contextual/none.rb +12 -0
  21. data/lib/mongoid/copyable.rb +13 -6
  22. data/lib/mongoid/criteria.rb +5 -2
  23. data/lib/mongoid/criteria/marshalable.rb +2 -2
  24. data/lib/mongoid/criteria/modifiable.rb +17 -1
  25. data/lib/mongoid/criteria/options.rb +25 -0
  26. data/lib/mongoid/criteria/queryable.rb +87 -0
  27. data/lib/mongoid/criteria/queryable/aggregable.rb +120 -0
  28. data/lib/mongoid/criteria/queryable/extensions.rb +28 -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/forwardable.rb +65 -0
  46. data/lib/mongoid/criteria/queryable/key.rb +103 -0
  47. data/lib/mongoid/criteria/queryable/macroable.rb +27 -0
  48. data/lib/mongoid/criteria/queryable/mergeable.rb +271 -0
  49. data/lib/mongoid/criteria/queryable/optional.rb +429 -0
  50. data/lib/mongoid/criteria/queryable/options.rb +153 -0
  51. data/lib/mongoid/criteria/queryable/pipeline.rb +111 -0
  52. data/lib/mongoid/criteria/queryable/selectable.rb +662 -0
  53. data/lib/mongoid/criteria/queryable/selector.rb +212 -0
  54. data/lib/mongoid/criteria/queryable/smash.rb +104 -0
  55. data/lib/mongoid/document.rb +30 -37
  56. data/lib/mongoid/errors.rb +2 -0
  57. data/lib/mongoid/errors/ambiguous_relationship.rb +1 -1
  58. data/lib/mongoid/errors/in_memory_collation_not_supported.rb +1 -1
  59. data/lib/mongoid/errors/invalid_field.rb +2 -2
  60. data/lib/mongoid/errors/invalid_persistence_option.rb +29 -0
  61. data/lib/mongoid/errors/invalid_relation.rb +66 -0
  62. data/lib/mongoid/evolvable.rb +1 -1
  63. data/lib/mongoid/extensions.rb +0 -4
  64. data/lib/mongoid/extensions/big_decimal.rb +17 -8
  65. data/lib/mongoid/extensions/date.rb +4 -1
  66. data/lib/mongoid/extensions/decimal128.rb +3 -3
  67. data/lib/mongoid/extensions/hash.rb +1 -0
  68. data/lib/mongoid/extensions/string.rb +4 -3
  69. data/lib/mongoid/extensions/time.rb +4 -1
  70. data/lib/mongoid/fields/validators/macro.rb +18 -0
  71. data/lib/mongoid/findable.rb +2 -2
  72. data/lib/mongoid/indexable.rb +15 -13
  73. data/lib/mongoid/interceptable.rb +5 -22
  74. data/lib/mongoid/matchable.rb +13 -7
  75. data/lib/mongoid/matchable/all.rb +2 -2
  76. data/lib/mongoid/matchable/and.rb +3 -3
  77. data/lib/mongoid/matchable/default.rb +2 -2
  78. data/lib/mongoid/matchable/elem_match.rb +28 -0
  79. data/lib/mongoid/matchable/exists.rb +2 -2
  80. data/lib/mongoid/matchable/gt.rb +4 -2
  81. data/lib/mongoid/matchable/gte.rb +4 -2
  82. data/lib/mongoid/matchable/in.rb +2 -2
  83. data/lib/mongoid/matchable/lt.rb +4 -2
  84. data/lib/mongoid/matchable/lte.rb +4 -2
  85. data/lib/mongoid/matchable/ne.rb +2 -2
  86. data/lib/mongoid/matchable/nin.rb +2 -2
  87. data/lib/mongoid/matchable/or.rb +3 -3
  88. data/lib/mongoid/matchable/regexp.rb +3 -3
  89. data/lib/mongoid/matchable/size.rb +2 -2
  90. data/lib/mongoid/persistable.rb +3 -5
  91. data/lib/mongoid/persistable/creatable.rb +2 -2
  92. data/lib/mongoid/persistable/deletable.rb +1 -1
  93. data/lib/mongoid/persistable/settable.rb +1 -1
  94. data/lib/mongoid/persistable/updatable.rb +5 -12
  95. data/lib/mongoid/persistable/upsertable.rb +1 -1
  96. data/lib/mongoid/persistence_context.rb +215 -0
  97. data/lib/mongoid/query_cache.rb +3 -6
  98. data/lib/mongoid/relations/accessors.rb +3 -0
  99. data/lib/mongoid/relations/auto_save.rb +12 -4
  100. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +4 -4
  101. data/lib/mongoid/relations/counter_cache.rb +15 -5
  102. data/lib/mongoid/relations/eager.rb +6 -11
  103. data/lib/mongoid/relations/eager/base.rb +3 -3
  104. data/lib/mongoid/relations/eager/has_and_belongs_to_many.rb +2 -2
  105. data/lib/mongoid/relations/eager/has_many.rb +1 -1
  106. data/lib/mongoid/relations/embedded/batchable.rb +12 -36
  107. data/lib/mongoid/relations/embedded/in.rb +13 -1
  108. data/lib/mongoid/relations/embedded/many.rb +28 -10
  109. data/lib/mongoid/relations/embedded/one.rb +14 -1
  110. data/lib/mongoid/relations/macros.rb +9 -1
  111. data/lib/mongoid/relations/metadata.rb +3 -3
  112. data/lib/mongoid/relations/options.rb +2 -2
  113. data/lib/mongoid/relations/proxy.rb +1 -31
  114. data/lib/mongoid/relations/referenced/in.rb +19 -10
  115. data/lib/mongoid/relations/referenced/many.rb +23 -17
  116. data/lib/mongoid/relations/referenced/many_to_many.rb +20 -13
  117. data/lib/mongoid/relations/referenced/one.rb +15 -1
  118. data/lib/mongoid/relations/synchronization.rb +11 -11
  119. data/lib/mongoid/relations/touchable.rb +6 -3
  120. data/lib/mongoid/reloadable.rb +1 -1
  121. data/lib/mongoid/serializable.rb +1 -1
  122. data/lib/mongoid/traversable.rb +1 -1
  123. data/lib/mongoid/validatable/uniqueness.rb +1 -2
  124. data/lib/mongoid/version.rb +1 -1
  125. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +14 -3
  126. data/spec/app/models/album.rb +5 -1
  127. data/spec/app/models/artist.rb +21 -0
  128. data/spec/app/models/book.rb +2 -1
  129. data/spec/app/models/dokument.rb +1 -0
  130. data/spec/app/models/ordered_post.rb +5 -0
  131. data/spec/app/models/oscar.rb +1 -2
  132. data/spec/app/models/page.rb +1 -1
  133. data/spec/app/models/person.rb +3 -3
  134. data/spec/app/models/princess.rb +2 -0
  135. data/spec/app/models/record.rb +1 -0
  136. data/spec/app/models/subscription.rb +1 -0
  137. data/spec/app/models/thing.rb +1 -1
  138. data/spec/config/mongoid.yml +15 -0
  139. data/spec/mongoid/atomic/modifiers_spec.rb +17 -17
  140. data/spec/mongoid/atomic_spec.rb +17 -17
  141. data/spec/mongoid/attributes/nested_spec.rb +14 -14
  142. data/spec/mongoid/attributes/readonly_spec.rb +87 -44
  143. data/spec/mongoid/attributes_spec.rb +90 -5
  144. data/spec/mongoid/cacheable_spec.rb +112 -0
  145. data/spec/mongoid/changeable_spec.rb +58 -0
  146. data/spec/mongoid/clients/factory_spec.rb +31 -3
  147. data/spec/mongoid/clients/options_spec.rb +382 -96
  148. data/spec/mongoid/clients_spec.rb +243 -101
  149. data/spec/mongoid/composable_spec.rb +7 -0
  150. data/spec/mongoid/config_spec.rb +67 -11
  151. data/spec/mongoid/contextual/atomic_spec.rb +3 -3
  152. data/spec/mongoid/contextual/mongo_spec.rb +165 -20
  153. data/spec/mongoid/contextual/none_spec.rb +15 -0
  154. data/spec/mongoid/copyable_spec.rb +13 -4
  155. data/spec/mongoid/criteria/modifiable_spec.rb +239 -7
  156. data/spec/mongoid/criteria/options_spec.rb +29 -0
  157. data/spec/mongoid/criteria/queryable/aggregable_spec.rb +370 -0
  158. data/spec/mongoid/criteria/queryable/extensions/array_spec.rb +523 -0
  159. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +59 -0
  160. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +58 -0
  161. data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +213 -0
  162. data/spec/mongoid/criteria/queryable/extensions/date_spec.rb +330 -0
  163. data/spec/mongoid/criteria/queryable/extensions/date_time_spec.rb +405 -0
  164. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +58 -0
  165. data/spec/mongoid/criteria/queryable/extensions/float_spec.rb +65 -0
  166. data/spec/mongoid/criteria/queryable/extensions/hash_spec.rb +327 -0
  167. data/spec/mongoid/criteria/queryable/extensions/integer_spec.rb +65 -0
  168. data/spec/mongoid/criteria/queryable/extensions/nil_class_spec.rb +77 -0
  169. data/spec/mongoid/criteria/queryable/extensions/object_spec.rb +108 -0
  170. data/spec/mongoid/criteria/queryable/extensions/range_spec.rb +309 -0
  171. data/spec/mongoid/{extensions/origin → criteria/queryable/extensions}/regexp_raw_spec.rb +2 -2
  172. data/spec/mongoid/criteria/queryable/extensions/regexp_spec.rb +90 -0
  173. data/spec/mongoid/criteria/queryable/extensions/set_spec.rb +39 -0
  174. data/spec/mongoid/criteria/queryable/extensions/string_spec.rb +302 -0
  175. data/spec/mongoid/criteria/queryable/extensions/symbol_spec.rb +167 -0
  176. data/spec/mongoid/criteria/queryable/extensions/time_spec.rb +376 -0
  177. data/spec/mongoid/criteria/queryable/extensions/time_with_zone_spec.rb +347 -0
  178. data/spec/mongoid/criteria/queryable/forwardable_spec.rb +87 -0
  179. data/spec/mongoid/criteria/queryable/key_spec.rb +52 -0
  180. data/spec/mongoid/criteria/queryable/mergeable_spec.rb +49 -0
  181. data/spec/mongoid/criteria/queryable/optional_spec.rb +1799 -0
  182. data/spec/mongoid/criteria/queryable/options_spec.rb +360 -0
  183. data/spec/mongoid/criteria/queryable/pipeline_spec.rb +200 -0
  184. data/spec/mongoid/criteria/queryable/queryable_spec.rb +137 -0
  185. data/spec/mongoid/criteria/queryable/selectable_spec.rb +4174 -0
  186. data/spec/mongoid/criteria/queryable/selector_spec.rb +844 -0
  187. data/spec/mongoid/criteria/queryable/smash_spec.rb +30 -0
  188. data/spec/mongoid/criteria_spec.rb +152 -21
  189. data/spec/mongoid/document_spec.rb +37 -88
  190. data/spec/mongoid/errors/invalid_relation_spec.rb +37 -0
  191. data/spec/mongoid/errors/mongoid_error_spec.rb +6 -3
  192. data/spec/mongoid/extensions/big_decimal_spec.rb +320 -18
  193. data/spec/mongoid/extensions/boolean_spec.rb +14 -0
  194. data/spec/mongoid/extensions/date_spec.rb +2 -6
  195. data/spec/mongoid/extensions/date_time_spec.rb +2 -6
  196. data/spec/mongoid/extensions/decimal128_spec.rb +1 -1
  197. data/spec/mongoid/extensions/float_spec.rb +8 -1
  198. data/spec/mongoid/extensions/hash_spec.rb +15 -0
  199. data/spec/mongoid/extensions/integer_spec.rb +8 -1
  200. data/spec/mongoid/extensions/object_spec.rb +11 -0
  201. data/spec/mongoid/extensions/string_spec.rb +21 -0
  202. data/spec/mongoid/extensions/time_spec.rb +2 -6
  203. data/spec/mongoid/extensions/time_with_zone_spec.rb +2 -6
  204. data/spec/mongoid/findable_spec.rb +46 -1
  205. data/spec/mongoid/indexable_spec.rb +15 -3
  206. data/spec/mongoid/interceptable_spec.rb +68 -10
  207. data/spec/mongoid/matchable/all_spec.rb +4 -4
  208. data/spec/mongoid/matchable/and_spec.rb +10 -10
  209. data/spec/mongoid/matchable/default_spec.rb +12 -12
  210. data/spec/mongoid/matchable/elem_match_spec.rb +86 -0
  211. data/spec/mongoid/matchable/exists_spec.rb +5 -5
  212. data/spec/mongoid/matchable/gt_spec.rb +18 -7
  213. data/spec/mongoid/matchable/gte_spec.rb +17 -7
  214. data/spec/mongoid/matchable/in_spec.rb +5 -5
  215. data/spec/mongoid/matchable/lt_spec.rb +18 -7
  216. data/spec/mongoid/matchable/lte_spec.rb +18 -7
  217. data/spec/mongoid/matchable/ne_spec.rb +5 -5
  218. data/spec/mongoid/matchable/nin_spec.rb +5 -5
  219. data/spec/mongoid/matchable/or_spec.rb +7 -7
  220. data/spec/mongoid/matchable/regexp_spec.rb +5 -5
  221. data/spec/mongoid/matchable/size_spec.rb +3 -3
  222. data/spec/mongoid/matchable_spec.rb +173 -53
  223. data/spec/mongoid/persistable/creatable_spec.rb +7 -2
  224. data/spec/mongoid/persistable/deletable_spec.rb +16 -1
  225. data/spec/mongoid/persistable/destroyable_spec.rb +6 -2
  226. data/spec/mongoid/persistable/savable_spec.rb +35 -30
  227. data/spec/mongoid/persistable/settable_spec.rb +45 -29
  228. data/spec/mongoid/persistable/updatable_spec.rb +184 -5
  229. data/spec/mongoid/persistence_context_spec.rb +680 -0
  230. data/spec/mongoid/positional_spec.rb +10 -10
  231. data/spec/mongoid/query_cache_spec.rb +89 -0
  232. data/spec/mongoid/relations/accessors_spec.rb +1 -1
  233. data/spec/mongoid/relations/auto_save_spec.rb +39 -6
  234. data/spec/mongoid/relations/bindings/referenced/many_to_many_spec.rb +4 -4
  235. data/spec/mongoid/relations/builders_spec.rb +37 -10
  236. data/spec/mongoid/relations/counter_cache_spec.rb +64 -3
  237. data/spec/mongoid/relations/eager/has_and_belongs_to_many_spec.rb +16 -0
  238. data/spec/mongoid/relations/eager_spec.rb +40 -0
  239. data/spec/mongoid/relations/embedded/many_spec.rb +63 -47
  240. data/spec/mongoid/relations/embedded/one_spec.rb +2 -1
  241. data/spec/mongoid/relations/macros_spec.rb +395 -7
  242. data/spec/mongoid/relations/metadata_spec.rb +15 -1
  243. data/spec/mongoid/relations/proxy_spec.rb +27 -1
  244. data/spec/mongoid/relations/referenced/in_spec.rb +41 -1
  245. data/spec/mongoid/relations/referenced/many_spec.rb +13 -25
  246. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +14 -26
  247. data/spec/mongoid/relations/synchronization_spec.rb +48 -2
  248. data/spec/mongoid/relations/touchable_spec.rb +40 -0
  249. data/spec/mongoid/reloadable_spec.rb +51 -0
  250. data/spec/mongoid/serializable_spec.rb +0 -50
  251. data/spec/mongoid/validatable/presence_spec.rb +1 -1
  252. data/spec/mongoid/validatable/uniqueness_spec.rb +18 -9
  253. data/spec/mongoid/validatable_spec.rb +16 -0
  254. data/spec/spec_helper.rb +20 -11
  255. metadata +524 -469
  256. checksums.yaml.gz.sig +0 -0
  257. data.tar.gz.sig +0 -0
  258. data/lib/mongoid/clients/thread_options.rb +0 -19
  259. data/lib/mongoid/extensions/origin/regexp_raw.rb +0 -43
  260. metadata.gz.sig +0 -0
@@ -0,0 +1,680 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::PersistenceContext do
4
+
5
+ let(:persistence_context) do
6
+ described_class.new(object, options)
7
+ end
8
+
9
+ let(:object) do
10
+ Band
11
+ end
12
+
13
+ describe '.set' do
14
+
15
+ let(:options) do
16
+ { collection: :other }
17
+ end
18
+
19
+ context 'when the persistence context is set on the thread' do
20
+
21
+ let!(:persistence_context) do
22
+ described_class.set(object, options)
23
+ end
24
+
25
+ it 'sets the persistence context for the object on the current thread' do
26
+ expect(described_class.get(object)).to be(persistence_context)
27
+ expect(described_class.get(object)).not_to be(nil)
28
+ expect(described_class.get(object).collection.name).to eq('other')
29
+ end
30
+
31
+ it 'only sets persistence context for the object on the current thread' do
32
+ Thread.new do
33
+ expect(described_class.get(object)).not_to be(persistence_context)
34
+ expect(described_class.get(object)).to be(nil)
35
+ end.value
36
+ end
37
+ end
38
+ end
39
+
40
+
41
+ describe '.get' do
42
+
43
+ let(:options) do
44
+ { collection: :other }
45
+ end
46
+
47
+ context 'when there has been a persistence context set on the current thread' do
48
+
49
+ let!(:persistence_context) do
50
+ described_class.set(object, options)
51
+ end
52
+
53
+ it 'gets the persistence context for the object on the current thread' do
54
+ expect(described_class.get(object)).to be(persistence_context)
55
+ expect(described_class.get(object).collection.name).to eq('other')
56
+ end
57
+
58
+ it 'does not get persistence context for the object from another thread' do
59
+ Thread.new do
60
+ expect(described_class.get(object)).not_to be(persistence_context)
61
+ expect(described_class.get(object)).to be(nil)
62
+ end.value
63
+ end
64
+ end
65
+ end
66
+
67
+ describe '.clear' do
68
+
69
+ let(:options) do
70
+ { collection: :other }
71
+ end
72
+
73
+ context 'when there has been a persistence context set on the current thread' do
74
+
75
+ let!(:persistence_context) do
76
+ described_class.set(object, options)
77
+ end
78
+
79
+ context 'when no cluster is passed to the method' do
80
+
81
+ before do
82
+ described_class.clear(object)
83
+ end
84
+
85
+ it 'clears the persistence context for the object on the current thread' do
86
+ expect(described_class.get(object)).to be(nil)
87
+ end
88
+ end
89
+
90
+ context 'when a cluster is passed to the method' do
91
+
92
+ context 'when the cluster is the same as that of the persistence context on the current thread' do
93
+
94
+ let(:client) do
95
+ persistence_context.client
96
+ end
97
+
98
+ before do
99
+ described_class.clear(object, client.cluster)
100
+ end
101
+
102
+ it 'does not close the cluster' do
103
+ expect(client).not_to receive(:close)
104
+ described_class.clear(object, client.cluster.dup)
105
+ end
106
+ end
107
+
108
+ context 'when the cluster is not the same as that of the persistence context on the current thread' do
109
+
110
+ let!(:client) do
111
+ persistence_context.client
112
+ end
113
+
114
+ it 'closes the client' do
115
+ expect(client).to receive(:close).and_call_original
116
+ described_class.clear(object, client.cluster.dup)
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
122
+
123
+ describe '#initialize' do
124
+
125
+ let(:options) do
126
+ { collection: 'other' }
127
+ end
128
+
129
+ context 'when an object is passed' do
130
+
131
+ context 'when the object is a klass' do
132
+
133
+ it 'sets the object on the persistence context' do
134
+ expect(persistence_context.instance_variable_get(:@object)).to eq(object)
135
+ end
136
+ end
137
+
138
+ context 'when the object is a model instance' do
139
+
140
+ let(:object) do
141
+ Band.new
142
+ end
143
+
144
+ it 'sets the object on the persistence context' do
145
+ expect(persistence_context.instance_variable_get(:@object)).to eq(object)
146
+ end
147
+ end
148
+ end
149
+
150
+ context 'when options are passed' do
151
+
152
+ let(:options) do
153
+ { connect_timeout: 3 }
154
+ end
155
+
156
+ context 'when the options are valid client options' do
157
+
158
+ it 'sets the options on the persistence context object' do
159
+ expect(persistence_context.options).to eq(options)
160
+ end
161
+ end
162
+
163
+ context 'when the options are not valid client options' do
164
+
165
+ context 'when the options are valid extra options' do
166
+
167
+ let(:options) do
168
+ { collection: 'other' }
169
+ end
170
+
171
+ it 'sets the options on the persistence context object' do
172
+ expect(persistence_context.collection_name).to eq(options[:collection].to_sym)
173
+ end
174
+ end
175
+
176
+ context 'when the options are not valid extra options' do
177
+
178
+ let(:options) do
179
+ { invalid: 'option' }
180
+ end
181
+
182
+ it 'raises an InvalidPersistenceOption error' do
183
+ expect {
184
+ persistence_context
185
+ }.to raise_error(Mongoid::Errors::InvalidPersistenceOption)
186
+ end
187
+ end
188
+ end
189
+ end
190
+ end
191
+
192
+ describe '#collection' do
193
+
194
+ let(:persistence_context) do
195
+ described_class.new(object, options)
196
+ end
197
+
198
+ let(:options) do
199
+ { read: { 'mode' => :secondary } }
200
+ end
201
+
202
+ context 'when a parent object is passed' do
203
+
204
+ it 'uses the collection of the parent object' do
205
+ expect(persistence_context.collection(Person.new).name).to eq('people')
206
+ end
207
+
208
+ it 'does not memoize the collection' do
209
+ persistence_context.collection
210
+ expect(persistence_context.collection(Person.new).name).to eq('people')
211
+ end
212
+
213
+ it 'keeps the other options of the persistence context' do
214
+ expect(persistence_context.collection(Person.new).options[:read]).to eq(options[:read])
215
+ end
216
+
217
+ context 'when the parent object has a client set' do
218
+
219
+ let(:file) do
220
+ File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
221
+ end
222
+
223
+ before do
224
+ Mongoid::Clients.clear
225
+ Mongoid.load!(file, :test)
226
+ Person.store_in(client: 'reports')
227
+ end
228
+
229
+ after do
230
+ Person.reset_storage_options!
231
+ end
232
+
233
+ it 'uses the client of the parent object' do
234
+ expect(persistence_context.collection(Person.new).client.database.name).to eq('reports')
235
+ end
236
+ end
237
+ end
238
+
239
+ context 'when a parent object is not passed' do
240
+
241
+ it 'uses the collection of the object' do
242
+ expect(persistence_context.collection.name).to eq('bands')
243
+ end
244
+
245
+ it 'does not memoize the collection' do
246
+ persistence_context.collection(Person.new)
247
+ expect(persistence_context.collection.name).to eq('bands')
248
+ end
249
+
250
+ it 'keeps the other options of the persistence context' do
251
+ expect(persistence_context.collection.client.options[:read]).to eq(options[:read])
252
+ end
253
+ end
254
+ end
255
+
256
+ describe '#collection_name' do
257
+
258
+ let(:persistence_context) do
259
+ described_class.new(object, options)
260
+ end
261
+
262
+ let(:options) do
263
+ { collection: 'other' }
264
+ end
265
+
266
+ context 'when storage options are set on the object' do
267
+
268
+ context 'when there are no options passed to the Persistence Context' do
269
+
270
+ let(:options) do
271
+ { }
272
+ end
273
+
274
+ after do
275
+ object.reset_storage_options!
276
+ end
277
+
278
+ context 'when the storage options is static' do
279
+
280
+ before do
281
+ object.store_in collection: :schmands
282
+ end
283
+
284
+ it 'uses the storage options' do
285
+ expect(persistence_context.collection_name).to eq(:schmands)
286
+ end
287
+ end
288
+
289
+ context 'when the storage options is a block' do
290
+
291
+ before do
292
+ object.store_in collection: ->{ :schmands }
293
+ end
294
+
295
+ it 'uses the storage options' do
296
+ expect(persistence_context.collection_name).to eq(:schmands)
297
+ end
298
+ end
299
+ end
300
+
301
+ context 'when there are options passed to the Persistence Context' do
302
+
303
+ let(:options) do
304
+ { collection: 'other' }
305
+ end
306
+
307
+ after do
308
+ object.reset_storage_options!
309
+ end
310
+
311
+ context 'when the storage options is static' do
312
+
313
+ before do
314
+ object.store_in collection: :schmands
315
+ end
316
+
317
+ it 'uses the persistence context options' do
318
+ expect(persistence_context.collection_name).to eq(:other)
319
+ end
320
+ end
321
+
322
+ context 'when the storage options is a block' do
323
+
324
+ before do
325
+ object.store_in collection: ->{ :schmands }
326
+ end
327
+
328
+ it 'uses the persistence context options' do
329
+ expect(persistence_context.collection_name).to eq(:other)
330
+ end
331
+ end
332
+ end
333
+ end
334
+
335
+ context 'when storage options are not set on the object' do
336
+
337
+ context 'when there are options passed to the Persistence Context' do
338
+
339
+ let(:options) do
340
+ { collection: 'other' }
341
+ end
342
+
343
+ it 'uses the persistence context options' do
344
+ expect(persistence_context.collection_name).to eq(:other)
345
+ end
346
+ end
347
+ end
348
+ end
349
+
350
+ describe '#database_name' do
351
+
352
+ let(:persistence_context) do
353
+ described_class.new(object, options)
354
+ end
355
+
356
+ let(:options) do
357
+ { database: 'other' }
358
+ end
359
+
360
+ context 'when storage options are set on the object' do
361
+
362
+ context 'when there are no options passed to the Persistence Context' do
363
+
364
+ let(:options) do
365
+ { }
366
+ end
367
+
368
+ after do
369
+ object.reset_storage_options!
370
+ end
371
+
372
+ context 'when there is a database override' do
373
+
374
+ before do
375
+ object.store_in database: :musique
376
+ end
377
+
378
+ before do
379
+ Mongoid::Threaded.database_override = :other
380
+ end
381
+
382
+ after do
383
+ Mongoid::Threaded.database_override = nil
384
+ end
385
+
386
+ it 'uses the override' do
387
+ expect(persistence_context.database_name).to eq(:other)
388
+ end
389
+ end
390
+
391
+ context 'when the storage options is static' do
392
+
393
+ before do
394
+ object.store_in database: :musique
395
+ end
396
+
397
+ it 'uses the storage options' do
398
+ expect(persistence_context.database_name).to eq(:musique)
399
+ end
400
+ end
401
+
402
+ context 'when the storage options is a block' do
403
+
404
+ before do
405
+ object.store_in database: ->{ :musique }
406
+ end
407
+ it 'uses the storage options' do
408
+ expect(persistence_context.database_name).to eq(:musique)
409
+ end
410
+ end
411
+ end
412
+
413
+ context 'when there are options passed to the Persistence Context' do
414
+
415
+ let(:options) do
416
+ { database: 'musique' }
417
+ end
418
+
419
+ context 'when there is a database override' do
420
+
421
+ before do
422
+ Mongoid::Threaded.database_override = :other
423
+ end
424
+
425
+ after do
426
+ Mongoid::Threaded.database_override = nil
427
+ end
428
+
429
+ it 'uses the persistence context options' do
430
+ expect(persistence_context.database_name).to eq(:musique)
431
+ end
432
+ end
433
+
434
+ context 'when the storage options is static' do
435
+
436
+ before do
437
+ object.store_in database: :sounds
438
+ end
439
+
440
+ after do
441
+ object.reset_storage_options!
442
+ end
443
+
444
+ it 'uses the persistence context options' do
445
+ expect(persistence_context.database_name).to eq(:musique)
446
+ end
447
+ end
448
+
449
+ context 'when the storage options is a block' do
450
+
451
+ before do
452
+ object.store_in database: ->{ :sounds }
453
+ end
454
+
455
+ after do
456
+ object.reset_storage_options!
457
+ end
458
+
459
+ it 'uses the persistence context options' do
460
+ expect(persistence_context.database_name).to eq(:musique)
461
+ end
462
+ end
463
+ end
464
+ end
465
+
466
+ context 'when storage options are not set on the object' do
467
+
468
+ context 'when there are options passed to the Persistence Context' do
469
+
470
+ let(:options) do
471
+ { database: 'musique' }
472
+ end
473
+
474
+ it 'uses the persistence context options' do
475
+ expect(persistence_context.database_name).to eq(:musique)
476
+ end
477
+
478
+ context 'when there is a database override' do
479
+
480
+ before do
481
+ Mongoid::Threaded.database_override = :other
482
+ end
483
+
484
+ after do
485
+ Mongoid::Threaded.database_override = nil
486
+ end
487
+
488
+ it 'uses the persistence context options' do
489
+ expect(persistence_context.database_name).to eq(:musique)
490
+ end
491
+ end
492
+ end
493
+
494
+ context 'when there are no options passed to the Persistence Context' do
495
+
496
+ context 'when there is a database override' do
497
+
498
+ before do
499
+ Mongoid::Threaded.database_override = :other
500
+ end
501
+
502
+ after do
503
+ Mongoid::Threaded.database_override = nil
504
+ end
505
+
506
+ it 'uses the database override options' do
507
+ expect(persistence_context.database_name).to eq(Mongoid::Threaded.database_override)
508
+ end
509
+ end
510
+ end
511
+ end
512
+ end
513
+
514
+ describe '#client' do
515
+
516
+ let(:persistence_context) do
517
+ described_class.new(object, options)
518
+ end
519
+
520
+ let(:options) do
521
+ { }
522
+ end
523
+
524
+ before do
525
+ Mongoid.clients[:alternative] = { database: :mongoid_test, hosts: [ "#{HOST}:#{PORT}" ] }
526
+ end
527
+
528
+ after do
529
+ Mongoid.clients.delete(:alternative)
530
+ end
531
+
532
+ context 'when the client is set in the options' do
533
+
534
+ let(:options) do
535
+ { client: :alternative }
536
+ end
537
+
538
+ after do
539
+ persistence_context.client.close
540
+ end
541
+
542
+ it 'uses the client option' do
543
+ expect(persistence_context.client).to eq(Mongoid::Clients.with_name(:alternative))
544
+ end
545
+
546
+ context 'when there is a client override' do
547
+
548
+ before do
549
+ Mongoid::Threaded.client_override = :other
550
+ end
551
+
552
+ after do
553
+ persistence_context.client.close
554
+ Mongoid::Threaded.client_override = nil
555
+ end
556
+
557
+ it 'uses the client option' do
558
+ expect(persistence_context.client).to eq(Mongoid::Clients.with_name(:alternative))
559
+ end
560
+ end
561
+
562
+ context 'when there are storage options set' do
563
+
564
+ after do
565
+ object.reset_storage_options!
566
+ end
567
+
568
+ context 'when the storage options is static' do
569
+
570
+ before do
571
+ object.store_in client: :other
572
+ end
573
+
574
+ it 'uses the persistence context options' do
575
+ expect(persistence_context.client).to eq(Mongoid::Clients.with_name(:alternative))
576
+ end
577
+ end
578
+
579
+ context 'when the storage options is a block' do
580
+
581
+ before do
582
+ object.store_in client: ->{ :other }
583
+ end
584
+
585
+ it 'uses the persistence context options' do
586
+ expect(persistence_context.client).to eq(Mongoid::Clients.with_name(:alternative))
587
+ end
588
+ end
589
+ end
590
+ end
591
+
592
+ context 'when there is no client option set' do
593
+
594
+ let(:options) do
595
+ { }
596
+ end
597
+
598
+ context 'when there is a client override' do
599
+
600
+ before do
601
+ Mongoid::Threaded.client_override = :alternative
602
+ end
603
+
604
+ after do
605
+ Mongoid::Threaded.client_override = nil
606
+ end
607
+
608
+ it 'uses the client override' do
609
+ expect(persistence_context.client).to eq(Mongoid::Clients.with_name(:alternative))
610
+ end
611
+ end
612
+
613
+ context 'when there are storage options set' do
614
+
615
+ after do
616
+ object.reset_storage_options!
617
+ end
618
+
619
+ context 'when the storage options is static' do
620
+
621
+ before do
622
+ object.store_in client: :alternative
623
+ end
624
+
625
+ it 'uses the client storage option' do
626
+ expect(persistence_context.client).to eq(Mongoid::Clients.with_name(:alternative))
627
+ end
628
+ end
629
+
630
+ context 'when the storage options is a block' do
631
+
632
+ before do
633
+ object.store_in client: ->{ :alternative }
634
+ end
635
+
636
+ it 'uses the client storage option' do
637
+ expect(persistence_context.client).to eq(Mongoid::Clients.with_name(:alternative))
638
+ end
639
+ end
640
+
641
+ context 'when there is a client override' do
642
+
643
+ before do
644
+ Mongoid::Threaded.client_override = :alternative
645
+ end
646
+
647
+ after do
648
+ Mongoid::Threaded.client_override = nil
649
+ end
650
+
651
+ it 'uses the client override' do
652
+ expect(persistence_context.client).to eq(Mongoid::Clients.with_name(:alternative))
653
+ end
654
+ end
655
+ end
656
+ end
657
+
658
+ context 'when there are client options set' do
659
+
660
+ let(:options) do
661
+ { connect_timeout: 3 }
662
+ end
663
+
664
+ it 'applies the options to the client' do
665
+ expect(persistence_context.client.options[:connect_timeout]).to eq(options[:connect_timeout])
666
+ end
667
+ end
668
+
669
+ context 'when there is a database name set in the options' do
670
+
671
+ let(:options) do
672
+ { database: 'other' }
673
+ end
674
+
675
+ it 'uses the database from the options' do
676
+ expect(persistence_context.client.database.name).to eq(options[:database])
677
+ end
678
+ end
679
+ end
680
+ end