mongoid 1.9.5 → 2.0.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (213) hide show
  1. data/.gitignore +6 -0
  2. data/.watchr +29 -0
  3. data/Rakefile +52 -0
  4. data/VERSION +1 -0
  5. data/caliper.yml +4 -0
  6. data/lib/mongoid.rb +20 -9
  7. data/lib/mongoid/associations.rb +100 -123
  8. data/lib/mongoid/associations/belongs_to_related.rb +3 -2
  9. data/lib/mongoid/associations/{embeds_many.rb → embed_many.rb} +29 -90
  10. data/lib/mongoid/associations/{embeds_one.rb → embed_one.rb} +7 -8
  11. data/lib/mongoid/associations/embedded_in.rb +4 -12
  12. data/lib/mongoid/associations/has_many_related.rb +4 -52
  13. data/lib/mongoid/associations/has_one_related.rb +4 -8
  14. data/lib/mongoid/associations/meta_data.rb +1 -2
  15. data/lib/mongoid/associations/options.rb +1 -6
  16. data/lib/mongoid/associations/proxy.rb +21 -14
  17. data/lib/mongoid/attributes.rb +13 -33
  18. data/lib/mongoid/callbacks.rb +11 -16
  19. data/lib/mongoid/collection.rb +3 -4
  20. data/lib/mongoid/collections/master.rb +2 -3
  21. data/lib/mongoid/collections/mimic.rb +46 -0
  22. data/lib/mongoid/collections/slaves.rb +2 -3
  23. data/lib/mongoid/commands.rb +161 -0
  24. data/lib/mongoid/commands/create.rb +19 -0
  25. data/lib/mongoid/commands/delete.rb +16 -0
  26. data/lib/mongoid/commands/delete_all.rb +23 -0
  27. data/lib/mongoid/commands/deletion.rb +18 -0
  28. data/lib/mongoid/commands/destroy.rb +17 -0
  29. data/lib/mongoid/commands/destroy_all.rb +23 -0
  30. data/lib/mongoid/commands/save.rb +29 -0
  31. data/lib/mongoid/components.rb +6 -4
  32. data/lib/mongoid/config.rb +15 -134
  33. data/lib/mongoid/contexts.rb +1 -1
  34. data/lib/mongoid/contexts/enumerable.rb +1 -1
  35. data/lib/mongoid/contexts/mongo.rb +1 -1
  36. data/lib/mongoid/contexts/paging.rb +2 -10
  37. data/lib/mongoid/criterion/inclusion.rb +0 -17
  38. data/lib/mongoid/criterion/optional.rb +1 -1
  39. data/lib/mongoid/cursor.rb +1 -0
  40. data/lib/mongoid/document.rb +45 -49
  41. data/lib/mongoid/errors.rb +1 -32
  42. data/lib/mongoid/extensions.rb +10 -12
  43. data/lib/mongoid/extensions/array/conversions.rb +6 -8
  44. data/lib/mongoid/extensions/big_decimal/conversions.rb +2 -2
  45. data/lib/mongoid/extensions/boolean/conversions.rb +2 -8
  46. data/lib/mongoid/extensions/date/conversions.rb +4 -13
  47. data/lib/mongoid/extensions/datetime/conversions.rb +6 -1
  48. data/lib/mongoid/extensions/float/conversions.rb +1 -5
  49. data/lib/mongoid/extensions/hash/assimilation.rb +3 -12
  50. data/lib/mongoid/extensions/hash/conversions.rb +4 -34
  51. data/lib/mongoid/extensions/integer/conversions.rb +1 -5
  52. data/lib/mongoid/extensions/nil/assimilation.rb +0 -4
  53. data/lib/mongoid/extensions/object/conversions.rb +2 -8
  54. data/lib/mongoid/extensions/objectid/conversions.rb +1 -1
  55. data/lib/mongoid/extensions/string/conversions.rb +1 -1
  56. data/lib/mongoid/extensions/symbol/inflections.rb +1 -1
  57. data/lib/mongoid/extensions/time/conversions.rb +18 -0
  58. data/lib/mongoid/factory.rb +1 -2
  59. data/lib/mongoid/field.rb +2 -9
  60. data/lib/mongoid/fields.rb +7 -11
  61. data/lib/mongoid/finders.rb +2 -2
  62. data/lib/mongoid/identity.rb +4 -4
  63. data/lib/mongoid/indexes.rb +7 -10
  64. data/lib/mongoid/memoization.rb +2 -8
  65. data/lib/mongoid/named_scope.rb +5 -0
  66. data/lib/mongoid/observable.rb +1 -1
  67. data/lib/mongoid/paths.rb +22 -30
  68. data/lib/mongoid/state.rb +21 -28
  69. data/lib/mongoid/timestamps.rb +1 -1
  70. data/lib/mongoid/validations.rb +51 -0
  71. data/lib/mongoid/validations/associated.rb +32 -0
  72. data/lib/mongoid/validations/locale/en.yml +4 -0
  73. data/lib/mongoid/validations/uniqueness.rb +22 -0
  74. data/lib/mongoid/versioning.rb +1 -2
  75. data/mongoid.gemspec +408 -0
  76. data/perf/benchmark.rb +77 -0
  77. data/spec/integration/mongoid/associations_spec.rb +340 -0
  78. data/spec/integration/mongoid/attributes_spec.rb +22 -0
  79. data/spec/integration/mongoid/commands_spec.rb +227 -0
  80. data/spec/integration/mongoid/contexts/enumerable_spec.rb +33 -0
  81. data/spec/integration/mongoid/criteria_spec.rb +272 -0
  82. data/spec/integration/mongoid/document_spec.rb +650 -0
  83. data/spec/integration/mongoid/extensions_spec.rb +22 -0
  84. data/spec/integration/mongoid/finders_spec.rb +119 -0
  85. data/spec/integration/mongoid/inheritance_spec.rb +137 -0
  86. data/spec/integration/mongoid/named_scope_spec.rb +46 -0
  87. data/spec/models/address.rb +39 -0
  88. data/spec/models/animal.rb +6 -0
  89. data/spec/models/callbacks.rb +18 -0
  90. data/spec/models/comment.rb +8 -0
  91. data/spec/models/country_code.rb +6 -0
  92. data/spec/models/employer.rb +5 -0
  93. data/spec/models/game.rb +7 -0
  94. data/spec/models/inheritance.rb +56 -0
  95. data/spec/models/location.rb +5 -0
  96. data/spec/models/mixed_drink.rb +4 -0
  97. data/spec/models/name.rb +13 -0
  98. data/spec/models/namespacing.rb +11 -0
  99. data/spec/models/patient.rb +4 -0
  100. data/spec/models/person.rb +99 -0
  101. data/spec/models/pet.rb +7 -0
  102. data/spec/models/pet_owner.rb +6 -0
  103. data/spec/models/phone.rb +7 -0
  104. data/spec/models/post.rb +15 -0
  105. data/spec/models/translation.rb +5 -0
  106. data/spec/models/vet_visit.rb +5 -0
  107. data/spec/spec.opts +3 -0
  108. data/spec/spec_helper.rb +31 -0
  109. data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +145 -0
  110. data/spec/unit/mongoid/associations/embed_many_spec.rb +516 -0
  111. data/spec/unit/mongoid/associations/embed_one_spec.rb +282 -0
  112. data/spec/unit/mongoid/associations/embedded_in_spec.rb +193 -0
  113. data/spec/unit/mongoid/associations/has_many_related_spec.rb +418 -0
  114. data/spec/unit/mongoid/associations/has_one_related_spec.rb +179 -0
  115. data/spec/unit/mongoid/associations/meta_data_spec.rb +88 -0
  116. data/spec/unit/mongoid/associations/options_spec.rb +192 -0
  117. data/spec/unit/mongoid/associations_spec.rb +595 -0
  118. data/spec/unit/mongoid/attributes_spec.rb +507 -0
  119. data/spec/unit/mongoid/callbacks_spec.rb +55 -0
  120. data/spec/unit/mongoid/collection_spec.rb +187 -0
  121. data/spec/unit/mongoid/collections/cyclic_iterator_spec.rb +75 -0
  122. data/spec/unit/mongoid/collections/master_spec.rb +41 -0
  123. data/spec/unit/mongoid/collections/mimic_spec.rb +43 -0
  124. data/spec/unit/mongoid/collections/slaves_spec.rb +81 -0
  125. data/spec/unit/mongoid/commands/create_spec.rb +31 -0
  126. data/spec/unit/mongoid/commands/delete_all_spec.rb +58 -0
  127. data/spec/unit/mongoid/commands/delete_spec.rb +38 -0
  128. data/spec/unit/mongoid/commands/destroy_all_spec.rb +21 -0
  129. data/spec/unit/mongoid/commands/destroy_spec.rb +51 -0
  130. data/spec/unit/mongoid/commands/save_spec.rb +107 -0
  131. data/spec/unit/mongoid/commands_spec.rb +270 -0
  132. data/spec/unit/mongoid/config_spec.rb +172 -0
  133. data/spec/unit/mongoid/contexts/enumerable_spec.rb +421 -0
  134. data/spec/unit/mongoid/contexts/mongo_spec.rb +682 -0
  135. data/spec/unit/mongoid/contexts_spec.rb +25 -0
  136. data/spec/unit/mongoid/criteria_spec.rb +824 -0
  137. data/spec/unit/mongoid/criterion/complex_spec.rb +19 -0
  138. data/spec/unit/mongoid/criterion/exclusion_spec.rb +91 -0
  139. data/spec/unit/mongoid/criterion/inclusion_spec.rb +219 -0
  140. data/spec/unit/mongoid/criterion/optional_spec.rb +319 -0
  141. data/spec/unit/mongoid/cursor_spec.rb +74 -0
  142. data/spec/unit/mongoid/deprecation_spec.rb +24 -0
  143. data/spec/unit/mongoid/document_spec.rb +818 -0
  144. data/spec/unit/mongoid/errors_spec.rb +103 -0
  145. data/spec/unit/mongoid/extensions/array/accessors_spec.rb +50 -0
  146. data/spec/unit/mongoid/extensions/array/assimilation_spec.rb +24 -0
  147. data/spec/unit/mongoid/extensions/array/conversions_spec.rb +35 -0
  148. data/spec/unit/mongoid/extensions/array/parentization_spec.rb +20 -0
  149. data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +22 -0
  150. data/spec/unit/mongoid/extensions/binary/conversions_spec.rb +22 -0
  151. data/spec/unit/mongoid/extensions/boolean/conversions_spec.rb +49 -0
  152. data/spec/unit/mongoid/extensions/date/conversions_spec.rb +102 -0
  153. data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +67 -0
  154. data/spec/unit/mongoid/extensions/float/conversions_spec.rb +61 -0
  155. data/spec/unit/mongoid/extensions/hash/accessors_spec.rb +184 -0
  156. data/spec/unit/mongoid/extensions/hash/assimilation_spec.rb +46 -0
  157. data/spec/unit/mongoid/extensions/hash/conversions_spec.rb +21 -0
  158. data/spec/unit/mongoid/extensions/hash/criteria_helpers_spec.rb +17 -0
  159. data/spec/unit/mongoid/extensions/hash/scoping_spec.rb +14 -0
  160. data/spec/unit/mongoid/extensions/integer/conversions_spec.rb +61 -0
  161. data/spec/unit/mongoid/extensions/nil/assimilation_spec.rb +24 -0
  162. data/spec/unit/mongoid/extensions/object/conversions_spec.rb +57 -0
  163. data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +34 -0
  164. data/spec/unit/mongoid/extensions/string/conversions_spec.rb +17 -0
  165. data/spec/unit/mongoid/extensions/string/inflections_spec.rb +208 -0
  166. data/spec/unit/mongoid/extensions/symbol/inflections_spec.rb +91 -0
  167. data/spec/unit/mongoid/extensions/time/conversions_spec.rb +70 -0
  168. data/spec/unit/mongoid/extras_spec.rb +102 -0
  169. data/spec/unit/mongoid/factory_spec.rb +31 -0
  170. data/spec/unit/mongoid/field_spec.rb +143 -0
  171. data/spec/unit/mongoid/fields_spec.rb +181 -0
  172. data/spec/unit/mongoid/finders_spec.rb +404 -0
  173. data/spec/unit/mongoid/identity_spec.rb +109 -0
  174. data/spec/unit/mongoid/indexes_spec.rb +93 -0
  175. data/spec/unit/mongoid/javascript_spec.rb +48 -0
  176. data/spec/unit/mongoid/matchers/all_spec.rb +27 -0
  177. data/spec/unit/mongoid/matchers/default_spec.rb +27 -0
  178. data/spec/unit/mongoid/matchers/exists_spec.rb +56 -0
  179. data/spec/unit/mongoid/matchers/gt_spec.rb +39 -0
  180. data/spec/unit/mongoid/matchers/gte_spec.rb +49 -0
  181. data/spec/unit/mongoid/matchers/in_spec.rb +27 -0
  182. data/spec/unit/mongoid/matchers/lt_spec.rb +39 -0
  183. data/spec/unit/mongoid/matchers/lte_spec.rb +49 -0
  184. data/spec/unit/mongoid/matchers/ne_spec.rb +27 -0
  185. data/spec/unit/mongoid/matchers/nin_spec.rb +27 -0
  186. data/spec/unit/mongoid/matchers/size_spec.rb +27 -0
  187. data/spec/unit/mongoid/matchers_spec.rb +329 -0
  188. data/spec/unit/mongoid/memoization_spec.rb +75 -0
  189. data/spec/unit/mongoid/named_scope_spec.rb +123 -0
  190. data/spec/unit/mongoid/observable_spec.rb +46 -0
  191. data/spec/unit/mongoid/paths_spec.rb +124 -0
  192. data/spec/unit/mongoid/scope_spec.rb +240 -0
  193. data/spec/unit/mongoid/state_spec.rb +83 -0
  194. data/spec/unit/mongoid/timestamps_spec.rb +25 -0
  195. data/spec/unit/mongoid/validations/associated_spec.rb +103 -0
  196. data/spec/unit/mongoid/validations/uniqueness_spec.rb +47 -0
  197. data/spec/unit/mongoid/validations_spec.rb +190 -0
  198. data/spec/unit/mongoid/versioning_spec.rb +41 -0
  199. data/spec/unit/mongoid_spec.rb +46 -0
  200. metadata +316 -110
  201. data/lib/mongoid/collections.rb +0 -41
  202. data/lib/mongoid/concern.rb +0 -31
  203. data/lib/mongoid/dirty.rb +0 -253
  204. data/lib/mongoid/extensions/time_conversions.rb +0 -35
  205. data/lib/mongoid/persistence.rb +0 -222
  206. data/lib/mongoid/persistence/command.rb +0 -39
  207. data/lib/mongoid/persistence/insert.rb +0 -50
  208. data/lib/mongoid/persistence/insert_embedded.rb +0 -38
  209. data/lib/mongoid/persistence/remove.rb +0 -39
  210. data/lib/mongoid/persistence/remove_all.rb +0 -37
  211. data/lib/mongoid/persistence/remove_embedded.rb +0 -50
  212. data/lib/mongoid/persistence/update.rb +0 -63
  213. data/lib/mongoid/version.rb +0 -4
@@ -0,0 +1,74 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Cursor do
4
+
5
+ let(:collection) do
6
+ stub.quacks_like(Mongoid::Collection.allocate)
7
+ end
8
+
9
+ let(:proxy) do
10
+ stub.quacks_like(Mongo::Cursor.allocate)
11
+ end
12
+
13
+ let(:cursor) do
14
+ Mongoid::Cursor.new(Person, collection, proxy)
15
+ end
16
+
17
+ (Mongoid::Cursor::OPERATIONS - [ :timeout ]).each do |name|
18
+
19
+ describe "##{name}" do
20
+
21
+ before do
22
+ proxy.expects(name)
23
+ end
24
+
25
+ it "delegates to the proxy" do
26
+ cursor.send(name)
27
+ end
28
+ end
29
+ end
30
+
31
+ describe "#collection" do
32
+
33
+ it "returns the mongoid collection" do
34
+ cursor.collection.should == collection
35
+ end
36
+ end
37
+
38
+ describe "#each" do
39
+
40
+ before do
41
+ proxy.expects(:each).yields({ "_type" => "Person" })
42
+ end
43
+
44
+ it "yields to the next document" do
45
+ cursor.each do |doc|
46
+ doc.attributes.except(:_id).should == Person.new.attributes.except(:_id)
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "#next_document" do
52
+
53
+ before do
54
+ proxy.expects(:next_document).returns({ "_type" => "Person" })
55
+ end
56
+
57
+ it "returns the next document from the proxied cursor" do
58
+ doc = cursor.next_document
59
+ doc.attributes.except(:_id).should == Person.new.attributes.except(:_id)
60
+ end
61
+ end
62
+
63
+ describe "#to_a" do
64
+
65
+ before do
66
+ proxy.expects(:to_a).returns([{ "_type" => "Person" }])
67
+ end
68
+
69
+ it "converts the proxy cursor to an array of documents" do
70
+ docs = cursor.to_a
71
+ docs[0].attributes.except(:_id).should == Person.new.attributes.except(:_id)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Deprecation do
4
+
5
+ let(:logger) do
6
+ stub.quacks_like(Logger.allocate)
7
+ end
8
+
9
+ before do
10
+ Logger.expects(:new).with($stdout).returns(logger)
11
+ end
12
+
13
+ describe "#alert" do
14
+
15
+ let(:deprecation) do
16
+ Mongoid::Deprecation.instance
17
+ end
18
+
19
+ it "calls warn on the memoized logger" do
20
+ logger.expects(:warn).with("Deprecation: testing")
21
+ deprecation.alert("testing")
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,818 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Document do
4
+
5
+ before do
6
+ @database = mock
7
+ @collection = stub(:name => "people")
8
+ @canvas_collection = stub(:name => "canvases")
9
+ Mongoid::Collection.stubs(:new).with(Person, "people").returns(@collection)
10
+ Mongoid::Collection.stubs(:new).with(Canvas, "canvases").returns(@canvas_collection)
11
+ @collection.stubs(:create_index).with(:_type, false)
12
+ @canvas_collection.stubs(:create_index).with(:_type, false)
13
+ end
14
+
15
+ after do
16
+ Person._collection = nil
17
+ Canvas._collection = nil
18
+ end
19
+
20
+ describe "#==" do
21
+
22
+ context "when other object is a Document" do
23
+
24
+ context "when attributes are equal" do
25
+
26
+ before do
27
+ @document = Person.new(:_id => 1, :title => "Sir")
28
+ @other = Person.new(:_id => 1, :title => "Sir")
29
+ end
30
+
31
+ it "returns true" do
32
+ @document.should == @other
33
+ end
34
+
35
+ end
36
+
37
+ context "when attributes are not equal" do
38
+
39
+ before do
40
+ @document = Person.new(:title => "Sir")
41
+ @other = Person.new(:title => "Madam")
42
+ end
43
+
44
+ it "returns false" do
45
+ @document.should_not == @other
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+
52
+ context "when other object is not a Document" do
53
+
54
+ it "returns false" do
55
+ Person.new.==("Test").should be_false
56
+ end
57
+
58
+ end
59
+
60
+ context "when comapring parent to its subclass" do
61
+
62
+ it "returns false" do
63
+ Canvas.new.should_not == Firefox.new
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+
70
+ describe "#eql?" do
71
+
72
+ context "when other object is a Document" do
73
+
74
+ context "when attributes are equal" do
75
+
76
+ before do
77
+ @document = Person.new(:_id => 1, :title => "Sir")
78
+ @other = Person.new(:_id => 1, :title => "Sir")
79
+ end
80
+
81
+ it "returns true" do
82
+ @document.eql?(@other).should be_true
83
+ end
84
+
85
+ end
86
+
87
+ context "when attributes are not equal" do
88
+
89
+ before do
90
+ @document = Person.new(:title => "Sir")
91
+ @other = Person.new(:title => "Madam")
92
+ end
93
+
94
+ it "returns false" do
95
+ @document.eql?(@other).should_not be_true
96
+ end
97
+
98
+ end
99
+
100
+ end
101
+
102
+ context "when other object is not a Document" do
103
+
104
+ it "returns false" do
105
+ Person.new.eql?("Test").should be_false
106
+ end
107
+
108
+ end
109
+
110
+ context "when comapring parent to its subclass" do
111
+
112
+ it "returns false" do
113
+ Canvas.new.eql?(Firefox.new).should_not be_true
114
+ end
115
+
116
+ end
117
+
118
+ end
119
+
120
+ describe "#hash" do
121
+
122
+ before do
123
+ @document = Person.new(:_id => 1, :title => "Sir")
124
+ @other = Person.new(:_id => 2, :title => "Sir")
125
+ end
126
+
127
+ it "deligates to id" do
128
+ @document.hash.should == @document.id.hash
129
+ end
130
+
131
+ it "has unique hash per id" do
132
+ @document.hash.should_not == @other.hash
133
+ end
134
+
135
+ end
136
+
137
+ describe "#alias_method_chain" do
138
+
139
+ context "on a field setter" do
140
+
141
+ before do
142
+ @person = Person.new
143
+ end
144
+
145
+ it "chains the method properly" do
146
+ @person.score = 10
147
+ @person.rescored.should == 30
148
+ end
149
+
150
+ end
151
+
152
+ end
153
+
154
+ describe "#assimilate" do
155
+
156
+ before do
157
+ @child = Name.new(:first_name => "Hank", :last_name => "Moody")
158
+ @parent = Person.new(:title => "Mr.")
159
+ @options = Mongoid::Associations::Options.new(:name => :name)
160
+ end
161
+
162
+ it "sets up all associations in the object graph" do
163
+ @child.assimilate(@parent, @options)
164
+ @parent.name.should == @child
165
+ end
166
+
167
+ end
168
+
169
+ describe ".db" do
170
+
171
+ before do
172
+ @db = stub
173
+ @collection.expects(:db).returns(@db)
174
+ end
175
+
176
+ it "returns the database from the collection" do
177
+ Person.db.should == @db
178
+ end
179
+ end
180
+
181
+ describe "#clone" do
182
+
183
+ before do
184
+ @comment = Comment.new(:text => "Woooooo")
185
+ @clone = @comment.clone
186
+ end
187
+
188
+ it "returns a new document sans id and versions" do
189
+ @clone.id.should_not == @comment.id
190
+ @clone.versions.should be_empty
191
+ end
192
+
193
+ end
194
+
195
+ describe ".collection" do
196
+
197
+ before do
198
+ @person = Person.new
199
+ end
200
+
201
+ it "sets the collection name to the class pluralized" do
202
+ Person.collection.name.should == "people"
203
+ end
204
+
205
+ context "when document is embedded" do
206
+
207
+ before do
208
+ @address = Address.new
209
+ end
210
+
211
+ it "raises an error" do
212
+ lambda { Address.collection }.should raise_error
213
+ end
214
+
215
+ end
216
+
217
+ end
218
+
219
+ describe ".collection_name=" do
220
+
221
+ context "on a parent class" do
222
+
223
+ it "sets the collection name on the document class" do
224
+ Patient.collection_name = "pats"
225
+ Patient.collection_name.should == "pats"
226
+ end
227
+
228
+ end
229
+
230
+ context "on a subclass" do
231
+
232
+ after do
233
+ Canvas.collection_name = "canvases"
234
+ end
235
+
236
+ it "sets the collection name for the entire hierarchy" do
237
+ Firefox.collection_name = "browsers"
238
+ Canvas.collection_name.should == "browsers"
239
+ end
240
+
241
+ end
242
+
243
+ end
244
+
245
+ describe ".embedded" do
246
+
247
+ context "when the document is embedded" do
248
+
249
+ it "returns true" do
250
+ address = Address.new
251
+ address.embedded.should be_true
252
+ end
253
+
254
+ end
255
+
256
+ context "when the document is not embedded" do
257
+
258
+ it "returns false" do
259
+ person = Person.new
260
+ person.embedded.should be_false
261
+ end
262
+
263
+ end
264
+
265
+ context "when a subclass is embedded" do
266
+
267
+ it "returns true" do
268
+ circle = Circle.new
269
+ circle.embedded.should be_true
270
+ end
271
+
272
+ end
273
+
274
+ end
275
+
276
+ describe ".hereditary" do
277
+
278
+ context "when the class is part of a hierarchy" do
279
+
280
+ it "returns true" do
281
+ Canvas.hereditary.should be_true
282
+ end
283
+
284
+ end
285
+
286
+ context "when the class is not part of a hierarchy" do
287
+
288
+ it "returns false" do
289
+ Game.hereditary.should be_false
290
+ end
291
+
292
+ end
293
+
294
+ end
295
+
296
+ describe ".human_name" do
297
+
298
+ it "returns the class name underscored and humanized" do
299
+ MixedDrink.model_name.human.should == "Mixed drink"
300
+ end
301
+
302
+ end
303
+
304
+ describe ".initialize" do
305
+
306
+ context "when passed a block" do
307
+
308
+ it "yields self to the block" do
309
+ person = Person.new do |p|
310
+ p.title = "Sir"
311
+ p.age = 60
312
+ end
313
+ person.title.should == "Sir"
314
+ person.age.should == 60
315
+ end
316
+
317
+ end
318
+
319
+ context "with no attributes" do
320
+
321
+ it "sets default attributes" do
322
+ person = Person.new
323
+ person.attributes.empty?.should be_false
324
+ person.age.should == 100
325
+ person.blood_alcohol_content.should == 0.0
326
+ end
327
+
328
+ end
329
+
330
+ context "with nil attributes" do
331
+
332
+ before do
333
+ @person = Person.new(nil)
334
+ end
335
+
336
+ it "sets default attributes" do
337
+ @person.attributes.empty?.should be_false
338
+ @person.age.should == 100
339
+ @person.blood_alcohol_content.should == 0.0
340
+ end
341
+
342
+ end
343
+
344
+ context "with attributes" do
345
+
346
+ before do
347
+ @attributes = {
348
+ :_id => "1",
349
+ :title => "value",
350
+ :age => "30",
351
+ :terms => "true",
352
+ :name => {
353
+ :_id => "2", :first_name => "Test", :last_name => "User"
354
+ },
355
+ :addresses => [
356
+ { :_id => "3", :street => "First Street" },
357
+ { :_id => "4", :street => "Second Street" }
358
+ ]
359
+ }
360
+ end
361
+
362
+ it "sets the attributes hash on the object properly casted" do
363
+ person = Person.new(@attributes)
364
+ person.attributes[:age].should == 30
365
+ person.attributes[:terms].should be_true
366
+ end
367
+
368
+ end
369
+
370
+ context "with a primary key" do
371
+
372
+ context "when the value for the key exists" do
373
+
374
+ before do
375
+ Address.key :street
376
+ @address = Address.new(:street => "Test")
377
+ end
378
+
379
+ it "sets the primary key" do
380
+ @address.id.should == "test"
381
+ end
382
+
383
+ end
384
+
385
+ end
386
+
387
+ context "without a type specified" do
388
+
389
+ it "sets the type" do
390
+ Person.new._type.should == "Person"
391
+ end
392
+
393
+ end
394
+
395
+ end
396
+
397
+ describe ".instantiate" do
398
+
399
+ context "when attributes have an id" do
400
+
401
+ before do
402
+ @attributes = { "_id" => "1", "_type" => "Person", "title" => "Sir", "age" => 30 }
403
+ end
404
+
405
+ it "sets the attributes directly" do
406
+ person = Person.instantiate(@attributes)
407
+ person._id.should == "1"
408
+ person._type.should == "Person"
409
+ person.title.should == "Sir"
410
+ person.age.should == 30
411
+ end
412
+
413
+ end
414
+
415
+ context "with nil attributes" do
416
+
417
+ it "sets the attributes directly" do
418
+ person = Person.instantiate(nil)
419
+ person.id.should_not be_nil
420
+ end
421
+
422
+ end
423
+
424
+ end
425
+
426
+ describe ".key" do
427
+
428
+ context "when key is single field" do
429
+
430
+ before do
431
+ Address.key :street
432
+ @address = Address.new(:street => "Testing Street Name")
433
+ @address.expects(:collection).returns(@collection)
434
+ @collection.expects(:save)
435
+ end
436
+
437
+ it "adds the callback for primary key generation" do
438
+ @address.save
439
+ @address.id.should == "testing-street-name"
440
+ end
441
+
442
+ end
443
+
444
+ context "when key is composite" do
445
+
446
+ before do
447
+ Address.key :street, :post_code
448
+ @address = Address.new(:street => "Testing Street Name", :post_code => "94123")
449
+ @address.expects(:collection).returns(@collection)
450
+ @collection.expects(:save)
451
+ end
452
+
453
+ it "combines all fields" do
454
+ @address.save
455
+ @address.id.should == "testing-street-name-94123"
456
+ end
457
+
458
+ end
459
+
460
+ context "when key is on a subclass" do
461
+
462
+ before do
463
+ Firefox.key :name
464
+ end
465
+
466
+ it "sets the key for the entire hierarchy" do
467
+ Canvas.primary_key.should == [:name]
468
+ end
469
+
470
+ end
471
+
472
+ end
473
+
474
+ describe "#_parent" do
475
+
476
+ before do
477
+ @attributes = { :title => "Sir",
478
+ :addresses => [
479
+ { :street => "Street 1" },
480
+ { :street => "Street 2" } ] }
481
+ @person = Person.new(@attributes)
482
+ end
483
+
484
+ context "when document is embedded" do
485
+
486
+ it "returns the parent document" do
487
+ @person.addresses.first._parent.should == @person
488
+ end
489
+
490
+ end
491
+
492
+ context "when document is root" do
493
+
494
+ it "returns nil" do
495
+ @person._parent.should be_nil
496
+ end
497
+
498
+ end
499
+
500
+ end
501
+
502
+ describe "#parentize" do
503
+
504
+ before do
505
+ @parent = Person.new
506
+ @child = Name.new
507
+ end
508
+
509
+ it "sets the parent on each element" do
510
+ @child.parentize(@parent, :child)
511
+ @child._parent.should == @parent
512
+ end
513
+
514
+ end
515
+
516
+ describe "#reload" do
517
+
518
+ before do
519
+ @attributes = { "title" => "Herr" }
520
+ @person = Person.new(:_id => Mongo::ObjectID.new.to_s)
521
+ @collection.expects(:find_one).with(:_id => @person.id).returns(@attributes)
522
+ end
523
+
524
+ it "reloads the object attribtues from the database" do
525
+ @person.reload
526
+ @person.attributes.should == @attributes
527
+ end
528
+
529
+ it 'should return a person object' do
530
+ @person.reload.should be_kind_of(Person)
531
+ end
532
+
533
+ end
534
+
535
+ describe "#remove" do
536
+
537
+ context "when removing an element from a has many" do
538
+
539
+ before do
540
+ @person = Person.new
541
+ @address = Address.new(:street => "Testing")
542
+ @person.addresses << @address
543
+ end
544
+
545
+ it "removes the child document attributes" do
546
+ @person.remove(@address)
547
+ @person.addresses.size.should == 0
548
+ end
549
+
550
+ end
551
+
552
+ context "when removing a has one" do
553
+
554
+ before do
555
+ @person = Person.new
556
+ @name = Name.new(:first_name => "Neytiri")
557
+ @person.name = @name
558
+ end
559
+
560
+ it "removes the child document attributes" do
561
+ @person.remove(@name)
562
+ @person.name.should be_nil
563
+ end
564
+ end
565
+ end
566
+
567
+ describe "#_root" do
568
+
569
+ before do
570
+ @person = Person.new(:title => "Mr")
571
+ @phone_number = Phone.new(:number => "415-555-1212")
572
+ @country_code = CountryCode.new(:code => 1)
573
+ @phone_number.country_code = @country_code
574
+ @person.phone_numbers << @phone_number
575
+ end
576
+
577
+ context "when document is the root" do
578
+
579
+ it "returns self" do
580
+ @person._root.should == @person
581
+ end
582
+ end
583
+
584
+ context "when document is embedded one level" do
585
+
586
+ it "returns the parent" do
587
+ @phone_number._root.should == @person
588
+ end
589
+ end
590
+
591
+ context "when document is embedded multiple levels" do
592
+
593
+ it "returns the top level parent" do
594
+ @country_code._root.should == @person
595
+ end
596
+ end
597
+ end
598
+
599
+ describe ".store_in" do
600
+
601
+ context "on a parent class" do
602
+
603
+ it "sets the collection name and collection for the document" do
604
+ Mongoid::Collection.expects(:new).with(Patient, "population").returns(@collection)
605
+ Patient.store_in :population
606
+ Patient.collection_name.should == "population"
607
+ end
608
+
609
+ end
610
+
611
+ context "on a subclass" do
612
+
613
+ after do
614
+ Mongoid::Collection.expects(:new).with(Firefox, "canvases")
615
+ Firefox.store_in :canvases
616
+ end
617
+
618
+ it "changes the collection name for the entire hierarchy" do
619
+ Mongoid::Collection.expects(:new).with(Firefox, "browsers").returns(@collection)
620
+ Firefox.store_in :browsers
621
+ Canvas.collection_name.should == "browsers"
622
+ end
623
+
624
+ end
625
+
626
+ end
627
+
628
+ describe "._types" do
629
+
630
+ it "returns all subclasses for the class plus the class" do
631
+ types = Canvas._types
632
+ types.size.should == 3
633
+ types.should include("Firefox")
634
+ types.should include("Browser")
635
+ types.should include("Canvas")
636
+ end
637
+
638
+ it "does not return parent classes" do
639
+ types = Browser._types
640
+ types.size.should == 2
641
+ types.should include("Firefox")
642
+ types.should include("Browser")
643
+ end
644
+
645
+ end
646
+
647
+ describe "#to_a" do
648
+
649
+ it "returns an array with the document in it" do
650
+ person = Person.new
651
+ person.to_a.should == [ person ]
652
+ end
653
+
654
+ end
655
+
656
+ describe "#to_param" do
657
+
658
+ it "returns the id" do
659
+ id = Mongo::ObjectID.new.to_s
660
+ Person.new(:_id => id).to_param.should == id.to_s
661
+ end
662
+
663
+ end
664
+
665
+ context "validations" do
666
+
667
+ context "when running validations" do
668
+
669
+ before do
670
+ @person = Person.new
671
+ @canvas = Canvas.new
672
+ @firefox = Firefox.new
673
+ end
674
+
675
+ describe "#validates_acceptance_of" do
676
+
677
+ it "fails if field not accepted" do
678
+ Person.class_eval do
679
+ validates_acceptance_of :terms, :allow_nil => false
680
+ end
681
+ @person.valid?.should be_false
682
+ @person.errors[:terms].should_not be_nil
683
+ end
684
+
685
+ end
686
+
687
+ describe "#validates_associated" do
688
+
689
+ context "when association is a has_many" do
690
+
691
+ it "fails when any association fails validation" do
692
+ Person.class_eval do
693
+ validates_associated :addresses
694
+ end
695
+ Address.class_eval do
696
+ validates_presence_of :street
697
+ end
698
+ @person.addresses << Address.new
699
+ @person.valid?.should be_false
700
+ @person.errors[:addresses].should_not be_nil
701
+ end
702
+
703
+ end
704
+
705
+ context "when association is a has_one" do
706
+
707
+ context "when the associated is not nil" do
708
+
709
+ it "fails when the association fails validation" do
710
+ Person.class_eval do
711
+ validates_associated :name
712
+ end
713
+ Name.class_eval do
714
+ validates_presence_of :first_name
715
+ end
716
+ @person.name = Name.new
717
+ @person.valid?.should be_false
718
+ @person.errors[:name].should_not be_nil
719
+ end
720
+
721
+ end
722
+
723
+ context "when the associated is nil" do
724
+
725
+ it "returns true" do
726
+ Person.class_eval do
727
+ validates_associated :name
728
+ end
729
+ @person.valid?
730
+ @person.errors[:name].should be_empty
731
+ end
732
+
733
+ end
734
+
735
+ end
736
+
737
+ end
738
+
739
+ describe "#validates_format_of" do
740
+
741
+ it "fails if the field is in the wrong format" do
742
+ Person.class_eval do
743
+ validates_format_of :title, :with => /[A-Za-z]/
744
+ end
745
+ @person.title = 10
746
+ @person.valid?.should be_false
747
+ @person.errors[:title].should_not be_nil
748
+ end
749
+
750
+ end
751
+
752
+ describe "#validates_length_of" do
753
+
754
+ it "fails if the field is the wrong length" do
755
+ Person.class_eval do
756
+ validates_length_of :title, :minimum => 10
757
+ end
758
+ @person.title = "Testing"
759
+ @person.valid?.should be_false
760
+ @person.errors[:title].should_not be_nil
761
+ end
762
+
763
+ end
764
+
765
+ describe "#validates_numericality_of" do
766
+
767
+ it "fails if the field is not a number" do
768
+ Person.class_eval do
769
+ validates_numericality_of :age
770
+ end
771
+ @person.age = "foo"
772
+ @person.valid?.should be_false
773
+ @person.errors[:age].should_not be_nil
774
+ end
775
+
776
+ end
777
+
778
+ describe "#validates_presence_of" do
779
+
780
+ context "on a parent class" do
781
+
782
+ it "fails if the field is nil on the parent" do
783
+ Person.class_eval do
784
+ validates_presence_of :title
785
+ end
786
+ @person.valid?.should be_false
787
+ @person.errors[:title].should_not be_nil
788
+ end
789
+
790
+ it "fails if the field is nil on a subclass" do
791
+ Canvas.class_eval do
792
+ validates_presence_of :name
793
+ end
794
+ @firefox.valid?.should be_false
795
+ @firefox.errors[:name].should_not be_nil
796
+ end
797
+
798
+ end
799
+
800
+ context "on a subclass" do
801
+
802
+ it "parent class does not get subclass validations" do
803
+ Firefox.class_eval do
804
+ validates_presence_of :version
805
+ end
806
+ @canvas.name = "Testing"
807
+ @canvas.valid?.should be_true
808
+ end
809
+
810
+ end
811
+
812
+ end
813
+
814
+ end
815
+
816
+ end
817
+
818
+ end