dm-is-awesome_set 0.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/LICENSE +23 -0
- data/README +16 -0
- data/Rakefile +33 -0
- data/TODO +4 -0
- data/VERSION +1 -0
- data/dm-is-awesome_set.gemspec +65 -0
- data/lib/dm-is-awesome_set.rb +5 -0
- data/lib/dm-is-awesome_set/is/awesome_set.rb +428 -0
- data/spec/dm-is-awesome_set_spec.rb +650 -0
- data/spec/spec_helper.rb +105 -0
- metadata +109 -0
@@ -0,0 +1,650 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
scope = {:scope => 1, :scope_2 => 2}
|
5
|
+
scope2 = {:scope => 1, :scope_2 => 5}
|
6
|
+
|
7
|
+
describe DataMapper::Is::AwesomeSet do
|
8
|
+
|
9
|
+
before(:all) do
|
10
|
+
DataMapper.auto_migrate!
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "without active DM Identity Map" do
|
14
|
+
|
15
|
+
before :each do
|
16
|
+
Category.auto_migrate!
|
17
|
+
Discrim1.auto_migrate!
|
18
|
+
Discrim2.auto_migrate!
|
19
|
+
end
|
20
|
+
|
21
|
+
it "puts itself as the last root in the defined scope on initial save" do
|
22
|
+
|
23
|
+
c1 = Category.create(scope)
|
24
|
+
c2 = Category.create(scope)
|
25
|
+
c1.pos.should eql([1,2])
|
26
|
+
c2.pos.should eql([3,4])
|
27
|
+
|
28
|
+
c3 = Category.create(scope2)
|
29
|
+
c4 = Category.create(scope2)
|
30
|
+
c3.pos.should eql([1,2])
|
31
|
+
c4.pos.should eql([3,4])
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
it "moves itself into a parent" do
|
36
|
+
|
37
|
+
c1 = Category.create(scope)
|
38
|
+
c2 = Category.create(scope)
|
39
|
+
c3 = Category.create(scope)
|
40
|
+
c2.move(:into => c1)
|
41
|
+
|
42
|
+
[c1,c2,c3].each { |c| c.reload }
|
43
|
+
c1.pos.should eql([1,4])
|
44
|
+
c2.pos.should eql([2,3])
|
45
|
+
c3.pos.should eql([5,6])
|
46
|
+
|
47
|
+
|
48
|
+
c2.move(:into => c3)
|
49
|
+
[c1,c2,c3].each { |c| c.reload }
|
50
|
+
c1.pos.should eql([1,2])
|
51
|
+
c3.pos.should eql([3,6])
|
52
|
+
c2.pos.should eql([4,5])
|
53
|
+
|
54
|
+
# This is to ensure that
|
55
|
+
c4 = Category.new
|
56
|
+
c4.move(:into => c1)
|
57
|
+
[c1,c2,c3, c4].each { |c| c.reload }
|
58
|
+
c4.sco.should eql(c1.sco)
|
59
|
+
c1.pos.should eql([1,4])
|
60
|
+
c4.pos.should eql([2,3])
|
61
|
+
c3.pos.should eql([5,8])
|
62
|
+
c2.pos.should eql([6,7])
|
63
|
+
end
|
64
|
+
|
65
|
+
it "moves around properly" do
|
66
|
+
c1 = Category.create(scope)
|
67
|
+
c2 = Category.create(scope)
|
68
|
+
c3 = Category.create(scope)
|
69
|
+
c4 = Category.new(scope)
|
70
|
+
|
71
|
+
c2.move(:to => 1)
|
72
|
+
[c1,c2,c3].each { |c| c.reload }
|
73
|
+
c2.pos.should eql([1,2])
|
74
|
+
c1.pos.should eql([3,4])
|
75
|
+
c3.pos.should eql([5,6])
|
76
|
+
|
77
|
+
c3.move(:higher)
|
78
|
+
[c1,c2,c3].each { |c| c.reload }
|
79
|
+
c2.pos.should eql([1,2])
|
80
|
+
c3.pos.should eql([3,4])
|
81
|
+
c1.pos.should eql([5,6])
|
82
|
+
|
83
|
+
c3.move(:lower)
|
84
|
+
[c1,c2,c3].each { |c| c.reload }
|
85
|
+
c2.pos.should eql([1,2])
|
86
|
+
c1.pos.should eql([3,4])
|
87
|
+
c3.pos.should eql([5,6])
|
88
|
+
|
89
|
+
c1.move(:lowest)
|
90
|
+
[c1,c2,c3].each { |c| c.reload }
|
91
|
+
c2.pos.should eql([1,2])
|
92
|
+
c3.pos.should eql([3,4])
|
93
|
+
c1.pos.should eql([5,6])
|
94
|
+
|
95
|
+
c1.move(:highest)
|
96
|
+
[c1,c2,c3].each { |c| c.reload }
|
97
|
+
c1.pos.should eql([1,2])
|
98
|
+
c2.pos.should eql([3,4])
|
99
|
+
c3.pos.should eql([5,6])
|
100
|
+
|
101
|
+
c4.move(:highest)
|
102
|
+
[c1,c2,c3].each { |c| c.reload }
|
103
|
+
|
104
|
+
c4.pos.should eql([1,2])
|
105
|
+
c1.pos.should eql([3,4])
|
106
|
+
c2.pos.should eql([5,6])
|
107
|
+
c3.pos.should eql([7,8])
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
it "puts in proper places for above and below with scope" do
|
112
|
+
c1 = Category.create(scope)
|
113
|
+
c2 = Category.create(scope)
|
114
|
+
c3 = Category.create(scope)
|
115
|
+
|
116
|
+
c2.move(:into => c1)
|
117
|
+
[c1,c2,c3].each { |c| c.reload }
|
118
|
+
c1.pos.should eql([1,4])
|
119
|
+
c2.pos.should eql([2,3])
|
120
|
+
c3.pos.should eql([5,6])
|
121
|
+
|
122
|
+
|
123
|
+
c3.move(:above => c2)
|
124
|
+
[c1,c2,c3].each { |c| c.reload }
|
125
|
+
c1.pos.should eql([1,6])
|
126
|
+
c2.pos.should eql([4,5])
|
127
|
+
c3.pos.should eql([2,3])
|
128
|
+
|
129
|
+
c3.move(:below => c1)
|
130
|
+
[c1,c2,c3].each { |c| c.reload }
|
131
|
+
c1.pos.should eql([1,4])
|
132
|
+
c2.pos.should eql([2,3])
|
133
|
+
c3.pos.should eql([5,6])
|
134
|
+
|
135
|
+
c2.move(:below => c1)
|
136
|
+
[c1,c2,c3].each { |c| c.reload }
|
137
|
+
c1.pos.should eql([1,2])
|
138
|
+
c2.pos.should eql([3,4])
|
139
|
+
c3.pos.should eql([5,6])
|
140
|
+
end
|
141
|
+
|
142
|
+
it "puts in proper places for above and below without scope" do
|
143
|
+
c1 = Category.create
|
144
|
+
c2 = Category.create
|
145
|
+
c3 = Category.create
|
146
|
+
|
147
|
+
c2.move(:into => c1)
|
148
|
+
[c1,c2,c3].each { |c| c.reload }
|
149
|
+
c1.pos.should eql([1,4])
|
150
|
+
c2.pos.should eql([2,3])
|
151
|
+
c3.pos.should eql([5,6])
|
152
|
+
|
153
|
+
|
154
|
+
c3.move(:above => c2)
|
155
|
+
[c1,c2,c3].each { |c| c.reload }
|
156
|
+
c1.pos.should eql([1,6])
|
157
|
+
c2.pos.should eql([4,5])
|
158
|
+
c3.pos.should eql([2,3])
|
159
|
+
|
160
|
+
c3.move(:below => c1)
|
161
|
+
[c1,c2,c3].each { |c| c.reload }
|
162
|
+
c1.pos.should eql([1,4])
|
163
|
+
c2.pos.should eql([2,3])
|
164
|
+
c3.pos.should eql([5,6])
|
165
|
+
|
166
|
+
c2.move(:below => c1)
|
167
|
+
[c1,c2,c3].each { |c| c.reload }
|
168
|
+
c1.pos.should eql([1,2])
|
169
|
+
c2.pos.should eql([3,4])
|
170
|
+
c3.pos.should eql([5,6])
|
171
|
+
end
|
172
|
+
|
173
|
+
it "gets the parent" do
|
174
|
+
c1 = Category.create(scope)
|
175
|
+
c2 = Category.create(scope)
|
176
|
+
c2.move(:into => c1)
|
177
|
+
c2.parent.should_not be_nil
|
178
|
+
c2.parent.id.should eql(c1.id)
|
179
|
+
end
|
180
|
+
|
181
|
+
it "identifies the root" do
|
182
|
+
|
183
|
+
c1 = Category.create(scope)
|
184
|
+
c2 = Category.create(scope)
|
185
|
+
c3 = Category.create(scope)
|
186
|
+
c2.move :into => c1
|
187
|
+
c3.move :into => c2
|
188
|
+
c3.root.should_not be_nil
|
189
|
+
c3.root.id.should eql(c1.id)
|
190
|
+
end
|
191
|
+
|
192
|
+
it "gets all roots in the current scope" do
|
193
|
+
c1 = Category.create(scope)
|
194
|
+
c2 = Category.create(scope)
|
195
|
+
c2.roots.size.should eql(2)
|
196
|
+
|
197
|
+
c3 = Category.create(scope2)
|
198
|
+
c4 = Category.create(scope2)
|
199
|
+
c3.roots.size.should eql(2)
|
200
|
+
end
|
201
|
+
|
202
|
+
it "gets all ancestors" do
|
203
|
+
c1 = Category.create(scope)
|
204
|
+
c2 = Category.create(scope)
|
205
|
+
c3 = Category.create(scope)
|
206
|
+
|
207
|
+
c2.move(:into => c1)
|
208
|
+
c3.move(:into => c2)
|
209
|
+
c3.ancestors.size.should eql(2)
|
210
|
+
c3.ancestors.should include(c2)
|
211
|
+
c3.ancestors.should include(c1)
|
212
|
+
c3.self_and_ancestors.size.should eql(3)
|
213
|
+
c3.self_and_ancestors.first.should eql c1
|
214
|
+
c3.self_and_ancestors.should include(c3)
|
215
|
+
c3.self_and_ancestors.should include(c2)
|
216
|
+
c3.self_and_ancestors.should include(c1)
|
217
|
+
c3.self_and_ancestors.last.should eql c3
|
218
|
+
end
|
219
|
+
|
220
|
+
it "gets all descendants" do
|
221
|
+
c1 = Category.create(scope)
|
222
|
+
c2 = Category.create(scope)
|
223
|
+
c3 = Category.create(scope)
|
224
|
+
c2.move(:into => c1)
|
225
|
+
c3.move(:into => c2)
|
226
|
+
c1.reload
|
227
|
+
c1.descendants.size.should eql(2)
|
228
|
+
c1.self_and_descendants.size.should eql(3)
|
229
|
+
c1.descendants.should include(c2)
|
230
|
+
c1.descendants.should include(c3)
|
231
|
+
c1.descendants.should_not include(c1)
|
232
|
+
c1.self_and_descendants.should include(c1)
|
233
|
+
c1.self_and_descendants.should include(c2)
|
234
|
+
c1.self_and_descendants.should include(c3)
|
235
|
+
end
|
236
|
+
|
237
|
+
it "gets all siblings" do
|
238
|
+
c1 = Category.create(scope)
|
239
|
+
c2 = Category.create(scope)
|
240
|
+
c3 = Category.create(scope)
|
241
|
+
|
242
|
+
c2.move(:into => c1)
|
243
|
+
c3.move(:into => c1)
|
244
|
+
c3.siblings.size.should eql(1)
|
245
|
+
end
|
246
|
+
|
247
|
+
it "moves scope properly" do
|
248
|
+
c1 = Category.create(scope)
|
249
|
+
c2 = Category.create(scope)
|
250
|
+
c3 = Category.create(scope)
|
251
|
+
|
252
|
+
c4 = Category.create(scope2)
|
253
|
+
c5 = Category.create(scope2)
|
254
|
+
c6 = Category.create(scope2)
|
255
|
+
|
256
|
+
c1.move(:into => c4)
|
257
|
+
[c1,c2,c3,c4,c5,c6].each { |c| c.reload }
|
258
|
+
c1.sco.should eql(scope2)
|
259
|
+
|
260
|
+
c2.pos.should eql([1,2])
|
261
|
+
c3.pos.should eql([3,4])
|
262
|
+
|
263
|
+
c4.pos.should eql([1,4])
|
264
|
+
c1.pos.should eql([2,3])
|
265
|
+
c5.pos.should eql([5,6])
|
266
|
+
c6.pos.should eql([7,8])
|
267
|
+
|
268
|
+
c4.move(:into => c2)
|
269
|
+
[c1,c2,c3,c4,c5,c6].each { |c| c.reload }
|
270
|
+
c1.sco.should eql(scope)
|
271
|
+
c4.sco.should eql(scope)
|
272
|
+
|
273
|
+
c2.pos.should eql([1,6])
|
274
|
+
c4.pos.should eql([2,5])
|
275
|
+
c1.pos.should eql([3,4])
|
276
|
+
c3.pos.should eql([7,8])
|
277
|
+
|
278
|
+
|
279
|
+
c5.pos.should eql([1,2])
|
280
|
+
c6.pos.should eql([3,4])
|
281
|
+
|
282
|
+
# You can move into the root of a different scope by passing an object from that scope
|
283
|
+
# or a hash that represents that scope
|
284
|
+
c5.move(:root => scope)
|
285
|
+
[c1,c2,c3,c4,c5,c6].each { |c| c.reload }
|
286
|
+
c5.sco.should eql(scope)
|
287
|
+
|
288
|
+
c2.pos.should eql([1,6])
|
289
|
+
c4.pos.should eql([2,5])
|
290
|
+
c1.pos.should eql([3,4])
|
291
|
+
c3.pos.should eql([7,8])
|
292
|
+
c5.pos.should eql([9,10])
|
293
|
+
|
294
|
+
c6.pos.should eql([1,2])
|
295
|
+
|
296
|
+
end
|
297
|
+
|
298
|
+
it "should get all rows in the database if the discrimator is not part of scope" do
|
299
|
+
c1 = CatD11.create(scope)
|
300
|
+
c2 = CatD11.create(scope)
|
301
|
+
c3 = CatD12.create(scope)
|
302
|
+
c4 = CatD12.create(scope)
|
303
|
+
CatD12.roots(scope).size.should eql(4)
|
304
|
+
end
|
305
|
+
|
306
|
+
it "should get only the same object types if discriminator is part of scope" do
|
307
|
+
c1 = CatD21.create(scope)
|
308
|
+
c2 = CatD21.create(scope)
|
309
|
+
c3 = CatD22.create(scope2)
|
310
|
+
c4 = CatD22.create(scope2)
|
311
|
+
Discrim2.roots(scope.merge(:type => 'CatD21')).size.should eql(2)
|
312
|
+
Discrim2.roots(scope2.merge(:type => 'CatD22')).size.should eql(2)
|
313
|
+
end
|
314
|
+
|
315
|
+
end
|
316
|
+
|
317
|
+
describe "with active DM Identity Map" do
|
318
|
+
|
319
|
+
before :each do
|
320
|
+
Category.auto_migrate!
|
321
|
+
Discrim1.auto_migrate!
|
322
|
+
Discrim2.auto_migrate!
|
323
|
+
end
|
324
|
+
|
325
|
+
it "puts itself as the last root in the defined scope on initial save when using the identity map" do
|
326
|
+
repository do
|
327
|
+
c1 = Category.create(scope)
|
328
|
+
c2 = Category.create(scope)
|
329
|
+
c1.pos.should eql([1,2])
|
330
|
+
c2.pos.should eql([3,4])
|
331
|
+
|
332
|
+
c3 = Category.create(scope2)
|
333
|
+
c4 = Category.create(scope2)
|
334
|
+
c3.pos.should eql([1,2])
|
335
|
+
c4.pos.should eql([3,4])
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
it "moves itself into a parent when using the identity map" do
|
340
|
+
repository do
|
341
|
+
c1 = Category.create(scope)
|
342
|
+
c2 = Category.create(scope)
|
343
|
+
c3 = Category.create(scope)
|
344
|
+
c1.pos.should eql([1,2]) # Check to make sure our assumptions about starting
|
345
|
+
c2.pos.should eql([3,4]) # positions is correct (added after found
|
346
|
+
c3.pos.should eql([5,6]) # the before(:each) auto_migrate! block was missing)
|
347
|
+
c2.parent.should be_nil
|
348
|
+
|
349
|
+
c2.move(:into => c1)
|
350
|
+
c2.parent.should eql c1
|
351
|
+
c3.parent.should_not eql c1
|
352
|
+
[c1,c2,c3].each { |c| c.reload }
|
353
|
+
c1.pos.should eql([1,4])
|
354
|
+
c3.pos.should eql([5,6])
|
355
|
+
c2.pos.should eql([2,3])
|
356
|
+
|
357
|
+
|
358
|
+
c2.move(:into => c3)
|
359
|
+
[c1,c2,c3].each { |c| c.reload }
|
360
|
+
c1.pos.should eql([1,2])
|
361
|
+
c3.pos.should eql([3,6])
|
362
|
+
c2.pos.should eql([4,5])
|
363
|
+
|
364
|
+
# This is to ensure that
|
365
|
+
c4 = Category.new
|
366
|
+
c4.move(:into => c1)
|
367
|
+
[c1,c2,c3, c4].each { |c| c.reload }
|
368
|
+
c4.sco.should eql(c1.sco)
|
369
|
+
c1.pos.should eql([1,4])
|
370
|
+
c4.pos.should eql([2,3])
|
371
|
+
c3.pos.should eql([5,8])
|
372
|
+
c2.pos.should eql([6,7])
|
373
|
+
end
|
374
|
+
end
|
375
|
+
|
376
|
+
it "moves around properly when using the identity map" do
|
377
|
+
repository do
|
378
|
+
c1 = Category.create(scope)
|
379
|
+
c2 = Category.create(scope)
|
380
|
+
c3 = Category.create(scope)
|
381
|
+
c4 = Category.new(scope)
|
382
|
+
|
383
|
+
c2.move(:to => 1)
|
384
|
+
[c1,c2,c3].each { |c| c.reload }
|
385
|
+
c2.pos.should eql([1,2])
|
386
|
+
c1.pos.should eql([3,4])
|
387
|
+
c3.pos.should eql([5,6])
|
388
|
+
|
389
|
+
c3.move(:higher)
|
390
|
+
[c1,c2,c3].each { |c| c.reload }
|
391
|
+
c2.pos.should eql([1,2])
|
392
|
+
c3.pos.should eql([3,4])
|
393
|
+
c1.pos.should eql([5,6])
|
394
|
+
|
395
|
+
c3.move(:lower)
|
396
|
+
[c1,c2,c3].each { |c| c.reload }
|
397
|
+
c2.pos.should eql([1,2])
|
398
|
+
c1.pos.should eql([3,4])
|
399
|
+
c3.pos.should eql([5,6])
|
400
|
+
|
401
|
+
c1.move(:lowest)
|
402
|
+
[c1,c2,c3].each { |c| c.reload }
|
403
|
+
c2.pos.should eql([1,2])
|
404
|
+
c3.pos.should eql([3,4])
|
405
|
+
c1.pos.should eql([5,6])
|
406
|
+
|
407
|
+
c1.move(:highest)
|
408
|
+
[c1,c2,c3].each { |c| c.reload }
|
409
|
+
c1.pos.should eql([1,2])
|
410
|
+
c2.pos.should eql([3,4])
|
411
|
+
c3.pos.should eql([5,6])
|
412
|
+
|
413
|
+
c4.move(:highest)
|
414
|
+
[c1,c2,c3].each { |c| c.reload }
|
415
|
+
|
416
|
+
c4.pos.should eql([1,2])
|
417
|
+
c1.pos.should eql([3,4])
|
418
|
+
c2.pos.should eql([5,6])
|
419
|
+
c3.pos.should eql([7,8])
|
420
|
+
end
|
421
|
+
end
|
422
|
+
|
423
|
+
it "puts in proper places for above and below with scope when using the identity map" do
|
424
|
+
repository do
|
425
|
+
c1 = Category.create(scope)
|
426
|
+
c2 = Category.create(scope)
|
427
|
+
c3 = Category.create(scope)
|
428
|
+
|
429
|
+
c2.move(:into => c1)
|
430
|
+
[c1,c2,c3].each { |c| c.reload }
|
431
|
+
c1.pos.should eql([1,4])
|
432
|
+
c2.pos.should eql([2,3])
|
433
|
+
c3.pos.should eql([5,6])
|
434
|
+
|
435
|
+
|
436
|
+
c3.move(:above => c2)
|
437
|
+
[c1,c2,c3].each { |c| c.reload }
|
438
|
+
c1.pos.should eql([1,6])
|
439
|
+
c2.pos.should eql([4,5])
|
440
|
+
c3.pos.should eql([2,3])
|
441
|
+
|
442
|
+
c3.move(:below => c1)
|
443
|
+
[c1,c2,c3].each { |c| c.reload }
|
444
|
+
c1.pos.should eql([1,4])
|
445
|
+
c2.pos.should eql([2,3])
|
446
|
+
c3.pos.should eql([5,6])
|
447
|
+
|
448
|
+
c2.move(:below => c1)
|
449
|
+
[c1,c2,c3].each { |c| c.reload }
|
450
|
+
c1.pos.should eql([1,2])
|
451
|
+
c2.pos.should eql([3,4])
|
452
|
+
c3.pos.should eql([5,6])
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
it "puts in proper places for above and below without scope when using the identity map" do
|
457
|
+
repository do
|
458
|
+
c1 = Category.create
|
459
|
+
c2 = Category.create
|
460
|
+
c3 = Category.create
|
461
|
+
|
462
|
+
c2.move(:into => c1)
|
463
|
+
[c1,c2,c3].each { |c| c.reload }
|
464
|
+
c1.pos.should eql([1,4])
|
465
|
+
c2.pos.should eql([2,3])
|
466
|
+
c3.pos.should eql([5,6])
|
467
|
+
|
468
|
+
|
469
|
+
c3.move(:above => c2)
|
470
|
+
[c1,c2,c3].each { |c| c.reload }
|
471
|
+
c1.pos.should eql([1,6])
|
472
|
+
c2.pos.should eql([4,5])
|
473
|
+
c3.pos.should eql([2,3])
|
474
|
+
|
475
|
+
c3.move(:below => c1)
|
476
|
+
[c1,c2,c3].each { |c| c.reload }
|
477
|
+
c1.pos.should eql([1,4])
|
478
|
+
c2.pos.should eql([2,3])
|
479
|
+
c3.pos.should eql([5,6])
|
480
|
+
|
481
|
+
c2.move(:below => c1)
|
482
|
+
[c1,c2,c3].each { |c| c.reload }
|
483
|
+
c1.pos.should eql([1,2])
|
484
|
+
c2.pos.should eql([3,4])
|
485
|
+
c3.pos.should eql([5,6])
|
486
|
+
end
|
487
|
+
end
|
488
|
+
|
489
|
+
it "gets the parent when using the identity map" do
|
490
|
+
repository do
|
491
|
+
c1 = Category.create(scope)
|
492
|
+
c2 = Category.create(scope)
|
493
|
+
c2.move(:into => c1)
|
494
|
+
c2.parent.should_not be_nil
|
495
|
+
c2.parent.id.should eql(c1.id)
|
496
|
+
end
|
497
|
+
end
|
498
|
+
|
499
|
+
it "identifies the root when using the identity map" do
|
500
|
+
repository do
|
501
|
+
c1 = Category.create(scope)
|
502
|
+
c2 = Category.create(scope)
|
503
|
+
c3 = Category.create(scope)
|
504
|
+
c2.move :into => c1
|
505
|
+
c3.move :into => c2
|
506
|
+
c3.root.should_not be_nil
|
507
|
+
c3.root.id.should eql(c1.id)
|
508
|
+
end
|
509
|
+
end
|
510
|
+
|
511
|
+
it "gets all roots in the current scope when using the identity map" do
|
512
|
+
repository do
|
513
|
+
c1 = Category.create(scope)
|
514
|
+
c2 = Category.create(scope)
|
515
|
+
c2.roots.size.should eql(2)
|
516
|
+
|
517
|
+
c3 = Category.create(scope2)
|
518
|
+
c4 = Category.create(scope2)
|
519
|
+
c3.roots.size.should eql(2)
|
520
|
+
end
|
521
|
+
end
|
522
|
+
|
523
|
+
it "gets all ancestors when using the identity map" do
|
524
|
+
repository do
|
525
|
+
c1 = Category.create(scope)
|
526
|
+
c2 = Category.create(scope)
|
527
|
+
c3 = Category.create(scope)
|
528
|
+
|
529
|
+
c2.ancestors.should_not include(c1)
|
530
|
+
c2.move(:into => c1)
|
531
|
+
c2.ancestors.should include(c1)
|
532
|
+
c3.move(:into => c2)
|
533
|
+
c3.ancestors.should include(c2)
|
534
|
+
c3.ancestors.should include(c1)
|
535
|
+
c1.level.should eql(0)
|
536
|
+
c2.level.should eql(1)
|
537
|
+
c3.level.should eql(2)
|
538
|
+
end
|
539
|
+
end
|
540
|
+
|
541
|
+
|
542
|
+
it "gets all descendants when using identity map" do
|
543
|
+
repository do
|
544
|
+
c1 = Category.create(scope)
|
545
|
+
c2 = Category.create(scope)
|
546
|
+
c3 = Category.create(scope)
|
547
|
+
c2.move(:into => c1)
|
548
|
+
c3.move(:into => c2)
|
549
|
+
c1.reload
|
550
|
+
c1.descendants.size.should eql(2)
|
551
|
+
c1.self_and_descendants.size.should eql(3)
|
552
|
+
c1.descendants.should include(c2)
|
553
|
+
c1.descendants.should include(c3)
|
554
|
+
c1.descendants.should_not include(c1)
|
555
|
+
c1.self_and_descendants.should include(c1)
|
556
|
+
c1.self_and_descendants.should include(c2)
|
557
|
+
c1.self_and_descendants.should include(c3)
|
558
|
+
end
|
559
|
+
end
|
560
|
+
|
561
|
+
it "gets all siblings when using the identity map" do
|
562
|
+
repository do
|
563
|
+
c1 = Category.create(scope)
|
564
|
+
c2 = Category.create(scope)
|
565
|
+
c3 = Category.create(scope)
|
566
|
+
|
567
|
+
c2.move(:into => c1)
|
568
|
+
c3.move(:into => c1)
|
569
|
+
c3.siblings.size.should eql(1)
|
570
|
+
end
|
571
|
+
end
|
572
|
+
|
573
|
+
it "moves scope properly when using the identity map" do
|
574
|
+
repository do
|
575
|
+
c1 = Category.create(scope)
|
576
|
+
c2 = Category.create(scope)
|
577
|
+
c3 = Category.create(scope)
|
578
|
+
|
579
|
+
c4 = Category.create(scope2)
|
580
|
+
c5 = Category.create(scope2)
|
581
|
+
c6 = Category.create(scope2)
|
582
|
+
|
583
|
+
c1.move(:into => c4)
|
584
|
+
[c1,c2,c3,c4,c5,c6].each { |c| c.reload }
|
585
|
+
|
586
|
+
c1.sco.should eql(scope2)
|
587
|
+
|
588
|
+
c2.pos.should eql([1,2])
|
589
|
+
c3.pos.should eql([3,4])
|
590
|
+
|
591
|
+
c4.pos.should eql([1,4])
|
592
|
+
c1.pos.should eql([2,3])
|
593
|
+
c5.pos.should eql([5,6])
|
594
|
+
c6.pos.should eql([7,8])
|
595
|
+
|
596
|
+
c4.move(:into => c2)
|
597
|
+
[c1,c2,c3,c4,c5,c6].each { |c| c.reload }
|
598
|
+
c1.sco.should eql(scope)
|
599
|
+
c4.sco.should eql(scope)
|
600
|
+
|
601
|
+
c2.pos.should eql([1,6])
|
602
|
+
c4.pos.should eql([2,5])
|
603
|
+
c1.pos.should eql([3,4])
|
604
|
+
c3.pos.should eql([7,8])
|
605
|
+
|
606
|
+
|
607
|
+
c5.pos.should eql([1,2])
|
608
|
+
c6.pos.should eql([3,4])
|
609
|
+
|
610
|
+
# You can move into the root of a different scope by passing an object from that scope
|
611
|
+
# or a hash that represents that scope
|
612
|
+
c5.move(:root => scope)
|
613
|
+
[c1,c2,c3,c4,c5,c6].each { |c| c.reload }
|
614
|
+
c5.sco.should eql(scope)
|
615
|
+
|
616
|
+
c2.pos.should eql([1,6])
|
617
|
+
c4.pos.should eql([2,5])
|
618
|
+
c1.pos.should eql([3,4])
|
619
|
+
c3.pos.should eql([7,8])
|
620
|
+
c5.pos.should eql([9,10])
|
621
|
+
|
622
|
+
c6.pos.should eql([1,2])
|
623
|
+
end
|
624
|
+
end
|
625
|
+
|
626
|
+
it "should get all rows in the database if the discrimator is not part of scope when using the identity map" do
|
627
|
+
repository do
|
628
|
+
c1 = CatD11.create(scope)
|
629
|
+
c2 = CatD11.create(scope)
|
630
|
+
c3 = CatD12.create(scope)
|
631
|
+
c4 = CatD12.create(scope)
|
632
|
+
CatD12.roots(scope).size.should eql(4)
|
633
|
+
end
|
634
|
+
end
|
635
|
+
|
636
|
+
it "should get only the same object types if discriminator is part of scope when using the identity map" do
|
637
|
+
repository do
|
638
|
+
c1 = CatD21.create(scope)
|
639
|
+
c2 = CatD21.create(scope)
|
640
|
+
c3 = CatD22.create(scope2)
|
641
|
+
c4 = CatD22.create(scope2)
|
642
|
+
Discrim2.roots(scope.merge(:type => 'CatD21')).size.should eql(2)
|
643
|
+
Discrim2.roots(scope2.merge(:type => 'CatD22')).size.should eql(2)
|
644
|
+
end
|
645
|
+
end
|
646
|
+
|
647
|
+
end
|
648
|
+
|
649
|
+
|
650
|
+
end
|