mongoid-list 0.1.8 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Guardfile +4 -8
- data/lib/mongoid/list.rb +5 -1
- data/lib/mongoid/list/collection.rb +3 -5
- data/lib/mongoid/list/embedded.rb +2 -3
- data/lib/mongoid/list/version.rb +1 -1
- data/mongoid-list.gemspec +11 -11
- data/mongoid.yml +12 -0
- data/spec/mongoid/list/collection_spec.rb +147 -0
- data/spec/mongoid/list/embedded_spec.rb +181 -0
- data/spec/mongoid/list_spec.rb +844 -0
- data/spec/spec_helper.rb +66 -0
- data/spec/support/models/container.rb +8 -0
- data/{test → spec}/support/models/embedded.rb +0 -0
- data/{test → spec}/support/models/embedded_deeply.rb +0 -0
- data/{test → spec}/support/models/scoped.rb +0 -0
- data/{test → spec}/support/models/scoped_embedded.rb +0 -0
- data/{test → spec}/support/models/simple.rb +0 -0
- metadata +47 -62
- data/test/mongoid/list/collection_test.rb +0 -141
- data/test/mongoid/list/embedded_test.rb +0 -174
- data/test/mongoid/list_test.rb +0 -869
- data/test/support/models/container.rb +0 -8
- data/test/test_helper.rb +0 -27
@@ -1,174 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
describe Mongoid::List::Embedded do
|
4
|
-
|
5
|
-
describe "#initialization" do
|
6
|
-
|
7
|
-
let :container do
|
8
|
-
Container.create
|
9
|
-
end
|
10
|
-
|
11
|
-
let :item do
|
12
|
-
container.items.create
|
13
|
-
end
|
14
|
-
|
15
|
-
let :embedded do
|
16
|
-
Mongoid::List::Embedded.new(item)
|
17
|
-
end
|
18
|
-
|
19
|
-
should "assign @item to :obj" do
|
20
|
-
assert_equal item, embedded.obj
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
describe "#count" do
|
28
|
-
|
29
|
-
context "when unscoped" do
|
30
|
-
|
31
|
-
let :container do
|
32
|
-
Container.create!
|
33
|
-
end
|
34
|
-
|
35
|
-
let :container2 do
|
36
|
-
Container.create!
|
37
|
-
end
|
38
|
-
|
39
|
-
let :item do
|
40
|
-
container.items.build
|
41
|
-
end
|
42
|
-
|
43
|
-
let :embedded do
|
44
|
-
Mongoid::List::Embedded.new(item)
|
45
|
-
end
|
46
|
-
|
47
|
-
setup do
|
48
|
-
3.times { container.items.create! }
|
49
|
-
2.times { container2.items.create! }
|
50
|
-
end
|
51
|
-
|
52
|
-
should "be 3" do
|
53
|
-
assert_equal 3, embedded.count
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
|
59
|
-
context "when scoped" do
|
60
|
-
|
61
|
-
let :container do
|
62
|
-
Container.create!
|
63
|
-
end
|
64
|
-
|
65
|
-
setup do
|
66
|
-
3.times { container.scoped_items.create!(group: "alien") }
|
67
|
-
2.times { container.scoped_items.create!(group: "aliens") }
|
68
|
-
end
|
69
|
-
|
70
|
-
context "group 1" do
|
71
|
-
|
72
|
-
let :item do
|
73
|
-
container.scoped_items.build(group: "alien")
|
74
|
-
end
|
75
|
-
|
76
|
-
let :embedded do
|
77
|
-
Mongoid::List::Embedded.new(item)
|
78
|
-
end
|
79
|
-
|
80
|
-
should "be 3" do
|
81
|
-
assert_equal 3, embedded.count
|
82
|
-
end
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
context "group 2" do
|
87
|
-
|
88
|
-
let :item do
|
89
|
-
container.scoped_items.build(group: "aliens")
|
90
|
-
end
|
91
|
-
|
92
|
-
let :embedded do
|
93
|
-
Mongoid::List::Embedded.new(item)
|
94
|
-
end
|
95
|
-
|
96
|
-
should "be 2" do
|
97
|
-
assert_equal 2, embedded.count
|
98
|
-
end
|
99
|
-
|
100
|
-
end
|
101
|
-
|
102
|
-
end
|
103
|
-
|
104
|
-
end
|
105
|
-
|
106
|
-
|
107
|
-
describe "#update_positions_in_list!" do
|
108
|
-
|
109
|
-
let :container do
|
110
|
-
Container.create!
|
111
|
-
end
|
112
|
-
|
113
|
-
context "unscoped" do
|
114
|
-
|
115
|
-
setup do
|
116
|
-
@obj1 = container.items.create!
|
117
|
-
@obj2 = container.items.create!
|
118
|
-
@obj3 = container.items.create!
|
119
|
-
container.items.update_positions_in_list!([ @obj2.id, @obj1.id, @obj3.id ])
|
120
|
-
end
|
121
|
-
|
122
|
-
should "change @obj1 from :position of 1 to 2" do
|
123
|
-
assert_equal 1, @obj1.position
|
124
|
-
assert_equal 2, @obj1.reload.position
|
125
|
-
end
|
126
|
-
|
127
|
-
should "change @obj2 from :position of 2 to 1" do
|
128
|
-
assert_equal 2, @obj2.position
|
129
|
-
assert_equal 1, @obj2.reload.position
|
130
|
-
end
|
131
|
-
|
132
|
-
should "not change @obj3 from :position of 3" do
|
133
|
-
assert_equal 3, @obj3.position
|
134
|
-
assert_equal 3, @obj3.reload.position
|
135
|
-
end
|
136
|
-
|
137
|
-
end
|
138
|
-
|
139
|
-
context "scoped" do
|
140
|
-
|
141
|
-
setup do
|
142
|
-
@obj1 = container.scoped_items.create!(group: "hell's angels")
|
143
|
-
@obj2 = container.scoped_items.create!(group: "hell's angels")
|
144
|
-
@obj3 = container.scoped_items.create!(group: "hell's angels")
|
145
|
-
@other = container.scoped_items.create!(group: "charlie's angels")
|
146
|
-
|
147
|
-
container.scoped_items.update_positions_in_list!([ @obj3.id, @obj2.id, @obj1.id ])
|
148
|
-
end
|
149
|
-
|
150
|
-
should "change @obj1 from :position of 1 to 3" do
|
151
|
-
assert_equal 1, @obj1.position
|
152
|
-
assert_equal 3, @obj1.reload.position
|
153
|
-
end
|
154
|
-
|
155
|
-
should "not change @obj2 from :position of 2" do
|
156
|
-
assert_equal 2, @obj2.position
|
157
|
-
assert_equal 2, @obj2.reload.position
|
158
|
-
end
|
159
|
-
|
160
|
-
should "change @obj3 from :position of 3 to 1" do
|
161
|
-
assert_equal 3, @obj3.position
|
162
|
-
assert_equal 1, @obj3.reload.position
|
163
|
-
end
|
164
|
-
|
165
|
-
should "not have touched @other scoped" do
|
166
|
-
assert_equal 1, @other.position
|
167
|
-
assert_equal 1, @other.reload.position
|
168
|
-
end
|
169
|
-
|
170
|
-
end
|
171
|
-
|
172
|
-
end
|
173
|
-
|
174
|
-
end
|
data/test/mongoid/list_test.rb
DELETED
@@ -1,869 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
|
4
|
-
describe Mongoid::List do
|
5
|
-
|
6
|
-
context "Setting Initial Position" do
|
7
|
-
setup do
|
8
|
-
@list1 = Simple.create
|
9
|
-
@list2 = Simple.create
|
10
|
-
end
|
11
|
-
|
12
|
-
should "have set @list1's position to 1" do
|
13
|
-
assert_equal 1, @list1.position
|
14
|
-
end
|
15
|
-
|
16
|
-
should "have set @list2's position to 2" do
|
17
|
-
assert_equal 2, @list2.position
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
context "Setting Initial Position in Embedded Collection" do
|
24
|
-
setup do
|
25
|
-
@container = Container.create
|
26
|
-
@list1 = @container.items.create
|
27
|
-
@list2 = @container.items.create
|
28
|
-
@list3 = @container.items.create
|
29
|
-
@unrelated_list = Container.create.items.create
|
30
|
-
end
|
31
|
-
|
32
|
-
should "have created the container" do
|
33
|
-
assert_equal 3, @container.items.count
|
34
|
-
end
|
35
|
-
|
36
|
-
|
37
|
-
should "have set the initial positions for each list item" do
|
38
|
-
assert_equal 1, @list1.position
|
39
|
-
assert_equal 2, @list2.position
|
40
|
-
assert_equal 3, @list3.position
|
41
|
-
end
|
42
|
-
|
43
|
-
should "have also set the @unrelated_list's position independently" do
|
44
|
-
assert_equal 1, @unrelated_list.position
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
context "Updating List Position" do
|
51
|
-
|
52
|
-
setup do
|
53
|
-
@list1 = Simple.create
|
54
|
-
@list2 = Simple.create
|
55
|
-
@list3 = Simple.create
|
56
|
-
@list4 = Simple.create
|
57
|
-
end
|
58
|
-
|
59
|
-
should "have set initial positions" do
|
60
|
-
assert_equal 1, @list1.position
|
61
|
-
assert_equal 2, @list2.position
|
62
|
-
assert_equal 3, @list3.position
|
63
|
-
assert_equal 4, @list4.position
|
64
|
-
end
|
65
|
-
|
66
|
-
context "for @list2 going to position of 1" do
|
67
|
-
|
68
|
-
setup do
|
69
|
-
@list2.update_attributes(:position => 1)
|
70
|
-
@list1.reload; @list2.reload; @list3.reload; @list4.reload
|
71
|
-
end
|
72
|
-
|
73
|
-
should "have updated everyone's :position" do
|
74
|
-
assert_equal 2, @list1.position
|
75
|
-
assert_equal 1, @list2.position
|
76
|
-
assert_equal 3, @list3.position
|
77
|
-
assert_equal 4, @list4.position
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
context "for @list3 going to position of 1" do
|
82
|
-
|
83
|
-
setup do
|
84
|
-
@list3.update_attributes(:position => 1)
|
85
|
-
@list1.reload; @list2.reload; @list3.reload; @list4.reload
|
86
|
-
end
|
87
|
-
|
88
|
-
should "have updated everyone's :position" do
|
89
|
-
assert_equal 2, @list1.position
|
90
|
-
assert_equal 3, @list2.position
|
91
|
-
assert_equal 1, @list3.position
|
92
|
-
assert_equal 4, @list4.position
|
93
|
-
end
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
context "for @list1 going to position of 2" do
|
98
|
-
|
99
|
-
setup do
|
100
|
-
@list1.update_attributes(:position => 2)
|
101
|
-
@list1.reload; @list2.reload; @list3.reload; @list4.reload
|
102
|
-
end
|
103
|
-
|
104
|
-
should "have updated everyone's :position" do
|
105
|
-
assert_equal 2, @list1.position
|
106
|
-
assert_equal 1, @list2.position
|
107
|
-
assert_equal 3, @list3.position
|
108
|
-
assert_equal 4, @list4.position
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
|
-
context "for @list1 going to position of 3" do
|
114
|
-
|
115
|
-
setup do
|
116
|
-
@list1.update_attributes(:position => 3)
|
117
|
-
@list1.reload; @list2.reload; @list3.reload; @list4.reload
|
118
|
-
end
|
119
|
-
|
120
|
-
should "have updated everyone's :position" do
|
121
|
-
assert_equal 3, @list1.position
|
122
|
-
assert_equal 1, @list2.position
|
123
|
-
assert_equal 2, @list3.position
|
124
|
-
assert_equal 4, @list4.position
|
125
|
-
end
|
126
|
-
|
127
|
-
end
|
128
|
-
|
129
|
-
end
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
context "Updating List Position in Embedded Collection" do
|
134
|
-
|
135
|
-
setup do
|
136
|
-
@container = Container.create
|
137
|
-
@list1 = @container.items.create
|
138
|
-
@list2 = @container.items.create
|
139
|
-
@list3 = @container.items.create
|
140
|
-
@list4 = @container.items.create
|
141
|
-
end
|
142
|
-
|
143
|
-
should "have set initial positions" do
|
144
|
-
assert_equal 1, @list1.position
|
145
|
-
assert_equal 2, @list2.position
|
146
|
-
assert_equal 3, @list3.position
|
147
|
-
assert_equal 4, @list4.position
|
148
|
-
end
|
149
|
-
|
150
|
-
context "for @list2 going to position of 1" do
|
151
|
-
|
152
|
-
setup do
|
153
|
-
@list2.update_attributes(:position => 1)
|
154
|
-
@container.reload
|
155
|
-
end
|
156
|
-
|
157
|
-
should "have updated everyone's :position" do
|
158
|
-
assert_equal 2, @container.items.find(@list1.id).position
|
159
|
-
assert_equal 1, @container.items.find(@list2.id).position
|
160
|
-
assert_equal 3, @container.items.find(@list3.id).position
|
161
|
-
assert_equal 4, @container.items.find(@list4.id).position
|
162
|
-
end
|
163
|
-
|
164
|
-
end
|
165
|
-
|
166
|
-
context "for @list3 going to position of 1" do
|
167
|
-
|
168
|
-
setup do
|
169
|
-
@list3.update_attributes(:position => 1)
|
170
|
-
@container.reload
|
171
|
-
end
|
172
|
-
|
173
|
-
should "have updated everyone's :position" do
|
174
|
-
assert_equal 2, @container.items.find(@list1.id).position
|
175
|
-
assert_equal 3, @container.items.find(@list2.id).position
|
176
|
-
assert_equal 1, @container.items.find(@list3.id).position
|
177
|
-
assert_equal 4, @container.items.find(@list4.id).position
|
178
|
-
end
|
179
|
-
|
180
|
-
end
|
181
|
-
|
182
|
-
context "for @list1 going to position of 2" do
|
183
|
-
|
184
|
-
setup do
|
185
|
-
@list1.update_attributes(:position => 2)
|
186
|
-
@container.reload
|
187
|
-
end
|
188
|
-
|
189
|
-
should "have updated everyone's :position" do
|
190
|
-
assert_equal 2, @container.items.find(@list1.id).position
|
191
|
-
assert_equal 1, @container.items.find(@list2.id).position
|
192
|
-
assert_equal 3, @container.items.find(@list3.id).position
|
193
|
-
assert_equal 4, @container.items.find(@list4.id).position
|
194
|
-
end
|
195
|
-
|
196
|
-
end
|
197
|
-
|
198
|
-
context "for @list1 going to position of 3" do
|
199
|
-
|
200
|
-
setup do
|
201
|
-
@list1.update_attributes(:position => 3)
|
202
|
-
@container.reload
|
203
|
-
end
|
204
|
-
|
205
|
-
should "have updated everyone's :position" do
|
206
|
-
assert_equal 3, @container.items.find(@list1.id).position
|
207
|
-
assert_equal 1, @container.items.find(@list2.id).position
|
208
|
-
assert_equal 2, @container.items.find(@list3.id).position
|
209
|
-
assert_equal 4, @container.items.find(@list4.id).position
|
210
|
-
end
|
211
|
-
|
212
|
-
end
|
213
|
-
|
214
|
-
end
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
context "#destroy" do
|
219
|
-
|
220
|
-
setup do
|
221
|
-
@list1 = Simple.create
|
222
|
-
@list2 = Simple.create
|
223
|
-
@list3 = Simple.create
|
224
|
-
@list4 = Simple.create
|
225
|
-
end
|
226
|
-
|
227
|
-
context "from the first position" do
|
228
|
-
setup do
|
229
|
-
@list1.destroy
|
230
|
-
@list2.reload; @list3.reload; @list4.reload
|
231
|
-
end
|
232
|
-
|
233
|
-
should "have moved all other items up" do
|
234
|
-
assert_equal 1, @list2.position
|
235
|
-
assert_equal 2, @list3.position
|
236
|
-
assert_equal 3, @list4.position
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
context "removing from the 2nd position" do
|
241
|
-
setup do
|
242
|
-
@list2.destroy
|
243
|
-
@list1.reload; @list3.reload; @list4.reload
|
244
|
-
end
|
245
|
-
|
246
|
-
should "have kept @list1 where it was" do
|
247
|
-
assert_equal 1, @list1.position
|
248
|
-
end
|
249
|
-
|
250
|
-
should "have moved lower items up" do
|
251
|
-
assert_equal 2, @list3.position
|
252
|
-
assert_equal 3, @list4.position
|
253
|
-
end
|
254
|
-
|
255
|
-
end
|
256
|
-
|
257
|
-
context "removing from the 4th and last position" do
|
258
|
-
|
259
|
-
setup do
|
260
|
-
@list4.destroy
|
261
|
-
@list1.reload; @list2.reload; @list3.reload
|
262
|
-
end
|
263
|
-
|
264
|
-
should "have kept everything else where it was" do
|
265
|
-
assert_equal 1, @list1.position
|
266
|
-
assert_equal 2, @list2.position
|
267
|
-
assert_equal 3, @list3.position
|
268
|
-
end
|
269
|
-
|
270
|
-
end
|
271
|
-
end
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
context "#destroy Item from an Embedded List" do
|
276
|
-
|
277
|
-
setup do
|
278
|
-
@container = Container.create
|
279
|
-
@list1 = @container.items.create
|
280
|
-
@list2 = @container.items.create
|
281
|
-
@list3 = @container.items.create
|
282
|
-
@list4 = @container.items.create
|
283
|
-
end
|
284
|
-
|
285
|
-
context "from the first position" do
|
286
|
-
|
287
|
-
setup do
|
288
|
-
@list1.destroy
|
289
|
-
@container.reload
|
290
|
-
end
|
291
|
-
|
292
|
-
should "have moved all other items up" do
|
293
|
-
assert_equal 1, @container.items.find(@list2.id).position
|
294
|
-
assert_equal 2, @container.items.find(@list3.id).position
|
295
|
-
assert_equal 3, @container.items.find(@list4.id).position
|
296
|
-
end
|
297
|
-
|
298
|
-
end
|
299
|
-
|
300
|
-
context "removing from the 2nd position" do
|
301
|
-
|
302
|
-
setup do
|
303
|
-
@list2.destroy
|
304
|
-
@container.reload
|
305
|
-
end
|
306
|
-
|
307
|
-
should "have kept @list1 where it was" do
|
308
|
-
assert_equal 1, @container.items.find(@list1.id).position
|
309
|
-
end
|
310
|
-
|
311
|
-
should "have moved @list3 up to :position 2" do
|
312
|
-
assert_equal 2, @container.items.find(@list3.id).position
|
313
|
-
end
|
314
|
-
|
315
|
-
should "have moved @list4 up to :position 3" do
|
316
|
-
assert_equal 3, @container.items.find(@list4.id).position
|
317
|
-
end
|
318
|
-
|
319
|
-
end
|
320
|
-
|
321
|
-
context "removing from the 4th and last position" do
|
322
|
-
|
323
|
-
setup do
|
324
|
-
@list4.destroy
|
325
|
-
@container.reload
|
326
|
-
end
|
327
|
-
|
328
|
-
should "have kept everything else where it was" do
|
329
|
-
assert_equal 1, @container.items.find(@list1.id).position
|
330
|
-
assert_equal 2, @container.items.find(@list2.id).position
|
331
|
-
assert_equal 3, @container.items.find(@list3.id).position
|
332
|
-
end
|
333
|
-
|
334
|
-
end
|
335
|
-
|
336
|
-
end
|
337
|
-
|
338
|
-
|
339
|
-
context "#destroy Item from Deeply Embedded List" do
|
340
|
-
|
341
|
-
setup do
|
342
|
-
@container = Container.create
|
343
|
-
@list = @container.items.create
|
344
|
-
@item1 = @list.items.create
|
345
|
-
@item2 = @list.items.create
|
346
|
-
@item3 = @list.items.create
|
347
|
-
end
|
348
|
-
|
349
|
-
context "from 1st position" do
|
350
|
-
|
351
|
-
setup do
|
352
|
-
@item1.destroy
|
353
|
-
@container.reload
|
354
|
-
end
|
355
|
-
|
356
|
-
should "have moved all other items up" do
|
357
|
-
assert_equal 1, @container.items.first.items.find(@item2.id).position
|
358
|
-
assert_equal 2, @container.items.first.items.find(@item3.id).position
|
359
|
-
end
|
360
|
-
|
361
|
-
end
|
362
|
-
|
363
|
-
context "from middle position" do
|
364
|
-
|
365
|
-
setup do
|
366
|
-
@item2.destroy
|
367
|
-
@container.reload
|
368
|
-
end
|
369
|
-
|
370
|
-
should "have moved last items up" do
|
371
|
-
assert_equal 1, @container.items.first.items.find(@item1.id).position
|
372
|
-
assert_equal 2, @container.items.first.items.find(@item3.id).position
|
373
|
-
end
|
374
|
-
|
375
|
-
end
|
376
|
-
|
377
|
-
end
|
378
|
-
|
379
|
-
|
380
|
-
context "#list_scoped?" do
|
381
|
-
|
382
|
-
|
383
|
-
context "for Simple (unscoped)" do
|
384
|
-
|
385
|
-
let(:obj) do
|
386
|
-
Simple.new
|
387
|
-
end
|
388
|
-
|
389
|
-
should "be nil" do
|
390
|
-
assert !obj.list_scoped?
|
391
|
-
end
|
392
|
-
|
393
|
-
end
|
394
|
-
|
395
|
-
|
396
|
-
context "for Scoped" do
|
397
|
-
|
398
|
-
let(:obj) do
|
399
|
-
Scoped.new
|
400
|
-
end
|
401
|
-
|
402
|
-
should "be :group" do
|
403
|
-
assert obj.list_scoped?
|
404
|
-
end
|
405
|
-
|
406
|
-
end
|
407
|
-
|
408
|
-
end
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
describe "#list_scope_field" do
|
414
|
-
|
415
|
-
let :obj do
|
416
|
-
Scoped.create
|
417
|
-
end
|
418
|
-
|
419
|
-
should "be :group" do
|
420
|
-
assert_equal :group, obj.list_scope_field
|
421
|
-
end
|
422
|
-
|
423
|
-
end
|
424
|
-
|
425
|
-
|
426
|
-
describe "#list_scope_conditions" do
|
427
|
-
|
428
|
-
context "when unscoped" do
|
429
|
-
|
430
|
-
let :obj do
|
431
|
-
Simple.new
|
432
|
-
end
|
433
|
-
|
434
|
-
let :expected_conditions do
|
435
|
-
{ }
|
436
|
-
end
|
437
|
-
|
438
|
-
should "be an empty hash" do
|
439
|
-
assert_equal expected_conditions, obj.list_scope_conditions
|
440
|
-
end
|
441
|
-
|
442
|
-
end
|
443
|
-
|
444
|
-
context "when scoped" do
|
445
|
-
|
446
|
-
let :obj do
|
447
|
-
Scoped.new(group: "a grouping")
|
448
|
-
end
|
449
|
-
|
450
|
-
let :expected_conditions do
|
451
|
-
{ group: "a grouping" }
|
452
|
-
end
|
453
|
-
|
454
|
-
should "have be for a :group of 'a grouping'" do
|
455
|
-
assert_equal expected_conditions, obj.list_scope_conditions
|
456
|
-
end
|
457
|
-
end
|
458
|
-
|
459
|
-
end
|
460
|
-
|
461
|
-
|
462
|
-
describe "Initializing in multiple scopes" do
|
463
|
-
|
464
|
-
context "on a Collection" do
|
465
|
-
|
466
|
-
setup do
|
467
|
-
@group1_1 = Scoped.create(group: 1)
|
468
|
-
@group2_1 = Scoped.create(group: 2)
|
469
|
-
@group2_2 = Scoped.create(group: 2)
|
470
|
-
end
|
471
|
-
|
472
|
-
should "default @group1_1 to a :position of 1" do
|
473
|
-
assert_equal 1, @group1_1.position
|
474
|
-
end
|
475
|
-
|
476
|
-
should "default @group2_1 to a :position of 1" do
|
477
|
-
assert_equal 1, @group2_1.position
|
478
|
-
end
|
479
|
-
|
480
|
-
should "default @group2_2 to a :position of 2" do
|
481
|
-
assert_equal 2, @group2_2.position
|
482
|
-
end
|
483
|
-
|
484
|
-
end
|
485
|
-
|
486
|
-
context "on Embedded Documents" do
|
487
|
-
|
488
|
-
setup do
|
489
|
-
@container = Container.create
|
490
|
-
@group1_1 = @container.scoped_items.create(group: 1)
|
491
|
-
@group1_2 = @container.scoped_items.create(group: 1)
|
492
|
-
@group2_1 = @container.scoped_items.create(group: 2)
|
493
|
-
@group2_2 = @container.scoped_items.create(group: 2)
|
494
|
-
@group2_3 = @container.scoped_items.create(group: 2)
|
495
|
-
@group3_1 = @container.scoped_items.create(group: 3)
|
496
|
-
end
|
497
|
-
|
498
|
-
should "default @group1_1 to a :position of 1" do
|
499
|
-
assert_equal 1, @group1_1.position
|
500
|
-
end
|
501
|
-
|
502
|
-
should "default @group1_2 to a :position of 2" do
|
503
|
-
assert_equal 2, @group1_2.position
|
504
|
-
end
|
505
|
-
|
506
|
-
should "default @group2_1 to a :position of 1" do
|
507
|
-
assert_equal 1, @group2_1.position
|
508
|
-
end
|
509
|
-
|
510
|
-
should "default @group2_2 to a :position of 2" do
|
511
|
-
assert_equal 2, @group2_2.position
|
512
|
-
end
|
513
|
-
|
514
|
-
should "default @group2_3 to a :position of 3" do
|
515
|
-
assert_equal 3, @group2_3.position
|
516
|
-
end
|
517
|
-
|
518
|
-
should "default @group3_1 to a :position of 1" do
|
519
|
-
assert_equal 1, @group3_1.position
|
520
|
-
end
|
521
|
-
|
522
|
-
end
|
523
|
-
end
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
describe "#update :position" do
|
528
|
-
|
529
|
-
context "for Collection List" do
|
530
|
-
|
531
|
-
setup do
|
532
|
-
@group1_1 = Scoped.create(group: 1)
|
533
|
-
@group1_2 = Scoped.create(group: 1)
|
534
|
-
@group2_1 = Scoped.create(group: 2)
|
535
|
-
@group2_2 = Scoped.create(group: 2)
|
536
|
-
@group2_3 = Scoped.create(group: 2)
|
537
|
-
end
|
538
|
-
|
539
|
-
context "@group1_2 to :position 1" do
|
540
|
-
|
541
|
-
setup do
|
542
|
-
@group1_2.update_attribute(:position, 1)
|
543
|
-
end
|
544
|
-
|
545
|
-
should "have updated @group1_1 to :position of 2" do
|
546
|
-
assert_equal 1, @group1_1.position
|
547
|
-
assert_equal 2, @group1_1.reload.position
|
548
|
-
end
|
549
|
-
|
550
|
-
should "not have updated :group2_1's :position" do
|
551
|
-
assert_equal @group2_1.position, @group2_1.reload.position
|
552
|
-
end
|
553
|
-
|
554
|
-
should "not have updated :group2_2's :position" do
|
555
|
-
assert_equal @group2_2.position, @group2_2.reload.position
|
556
|
-
end
|
557
|
-
|
558
|
-
should "not have updated :group2_3's :position" do
|
559
|
-
assert_equal @group2_3.position, @group2_3.reload.position
|
560
|
-
end
|
561
|
-
|
562
|
-
end
|
563
|
-
|
564
|
-
|
565
|
-
context "@group2_2 to :position 3" do
|
566
|
-
|
567
|
-
setup do
|
568
|
-
@group2_2.update_attribute(:position, 3)
|
569
|
-
end
|
570
|
-
|
571
|
-
should "not have updated @group1_1's :position" do
|
572
|
-
assert_equal @group1_1.position, @group1_1.reload.position
|
573
|
-
end
|
574
|
-
|
575
|
-
should "not have updated @group1_2's :position" do
|
576
|
-
assert_equal @group1_2.position, @group1_2.reload.position
|
577
|
-
end
|
578
|
-
|
579
|
-
should "not have updated @group2_1's :position" do
|
580
|
-
assert_equal @group2_1.position, @group2_1.reload.position
|
581
|
-
end
|
582
|
-
|
583
|
-
should "have updated @group2_2's :position to 3" do
|
584
|
-
assert_equal 3, @group2_2.position
|
585
|
-
end
|
586
|
-
|
587
|
-
should "have updated @group2_3's :position to 2" do
|
588
|
-
assert_equal 3, @group2_3.position
|
589
|
-
assert_equal 2, @group2_3.reload.position
|
590
|
-
end
|
591
|
-
end
|
592
|
-
|
593
|
-
end
|
594
|
-
|
595
|
-
|
596
|
-
context "for an Embedded List" do
|
597
|
-
|
598
|
-
setup do
|
599
|
-
@container = Container.create
|
600
|
-
@group1_1 = @container.scoped_items.create(group: 1)
|
601
|
-
@group1_2 = @container.scoped_items.create(group: 1)
|
602
|
-
@group2_1 = @container.scoped_items.create(group: 2)
|
603
|
-
@group2_2 = @container.scoped_items.create(group: 2)
|
604
|
-
@group2_3 = @container.scoped_items.create(group: 2)
|
605
|
-
end
|
606
|
-
|
607
|
-
context "moving @group1_1 to :position 2" do
|
608
|
-
|
609
|
-
setup do
|
610
|
-
@group1_1.update_attributes(position: 2)
|
611
|
-
end
|
612
|
-
|
613
|
-
should "update @group1_1's :position to 2" do
|
614
|
-
assert_equal 2, @group1_1.position
|
615
|
-
end
|
616
|
-
|
617
|
-
should "update @group1_2's :position to 1" do
|
618
|
-
assert_equal 2, @group1_2.position
|
619
|
-
assert_equal 1, @group1_2.reload.position
|
620
|
-
end
|
621
|
-
|
622
|
-
should "not update @group2_1's :position" do
|
623
|
-
assert_equal 1, @group2_1.position
|
624
|
-
assert_equal 1, @group2_1.reload.position
|
625
|
-
end
|
626
|
-
|
627
|
-
should "not update @group2_2's :position" do
|
628
|
-
assert_equal 2, @group2_2.position
|
629
|
-
assert_equal 2, @group2_2.reload.position
|
630
|
-
end
|
631
|
-
|
632
|
-
should "not update @group2_3's :position" do
|
633
|
-
assert_equal 3, @group2_3.position
|
634
|
-
assert_equal 3, @group2_3.reload.position
|
635
|
-
end
|
636
|
-
end
|
637
|
-
|
638
|
-
|
639
|
-
context "moving @group2_3 to :position 1" do
|
640
|
-
|
641
|
-
setup do
|
642
|
-
@group2_3.update_attributes(position: 1)
|
643
|
-
end
|
644
|
-
|
645
|
-
should "not update @group1_1's :position" do
|
646
|
-
assert_equal 1, @group1_1.position
|
647
|
-
assert_equal 1, @group1_1.reload.position
|
648
|
-
end
|
649
|
-
|
650
|
-
should "not update @group1_2's :position" do
|
651
|
-
assert_equal 2, @group1_2.position
|
652
|
-
assert_equal 2, @group1_2.reload.position
|
653
|
-
end
|
654
|
-
|
655
|
-
should "update @group2_1's :position to 2" do
|
656
|
-
assert_equal 1, @group2_1.position
|
657
|
-
assert_equal 2, @group2_1.reload.position
|
658
|
-
end
|
659
|
-
|
660
|
-
should "update @group2_2's :position to 3" do
|
661
|
-
assert_equal 2, @group2_2.position
|
662
|
-
assert_equal 3, @group2_2.reload.position
|
663
|
-
end
|
664
|
-
|
665
|
-
should "update @group2_3's :position to 1" do
|
666
|
-
assert_equal 1, @group2_3.position
|
667
|
-
assert_equal 1, @group2_3.reload.position
|
668
|
-
end
|
669
|
-
end
|
670
|
-
|
671
|
-
end
|
672
|
-
|
673
|
-
end
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
describe "#destroy" do
|
678
|
-
|
679
|
-
context "from a Collection" do
|
680
|
-
|
681
|
-
setup do
|
682
|
-
@group1_1 = Scoped.create(group: 1)
|
683
|
-
@group1_2 = Scoped.create(group: 1)
|
684
|
-
@group2_1 = Scoped.create(group: 2)
|
685
|
-
@group2_2 = Scoped.create(group: 2)
|
686
|
-
@group2_3 = Scoped.create(group: 2)
|
687
|
-
end
|
688
|
-
|
689
|
-
context "for @group2_1" do
|
690
|
-
|
691
|
-
setup do
|
692
|
-
@group2_1.destroy
|
693
|
-
end
|
694
|
-
|
695
|
-
should "not update @group1_1's :position" do
|
696
|
-
assert_equal 1, @group1_1.position
|
697
|
-
assert_equal 1, @group1_1.reload.position
|
698
|
-
end
|
699
|
-
|
700
|
-
should "not update @group1_2's :position" do
|
701
|
-
assert_equal 2, @group1_2.position
|
702
|
-
assert_equal 2, @group1_2.reload.position
|
703
|
-
end
|
704
|
-
|
705
|
-
should "update @group2_2's :position to 1" do
|
706
|
-
assert_equal 2, @group2_2.position
|
707
|
-
assert_equal 1, @group2_2.reload.position
|
708
|
-
end
|
709
|
-
|
710
|
-
should "update @group2_3's :position to 2" do
|
711
|
-
assert_equal 3, @group2_3.position
|
712
|
-
assert_equal 2, @group2_3.reload.position
|
713
|
-
end
|
714
|
-
end
|
715
|
-
|
716
|
-
end
|
717
|
-
|
718
|
-
|
719
|
-
context "from an Embedded Collection" do
|
720
|
-
|
721
|
-
setup do
|
722
|
-
@container = Container.create
|
723
|
-
@group1_1 = @container.scoped_items.create(group: 1)
|
724
|
-
@group1_2 = @container.scoped_items.create(group: 1)
|
725
|
-
@group2_1 = @container.scoped_items.create(group: 2)
|
726
|
-
@group2_2 = @container.scoped_items.create(group: 2)
|
727
|
-
@group2_3 = @container.scoped_items.create(group: 2)
|
728
|
-
end
|
729
|
-
|
730
|
-
context "for @group2_2" do
|
731
|
-
|
732
|
-
setup do
|
733
|
-
@group2_2.destroy
|
734
|
-
end
|
735
|
-
|
736
|
-
should "not update @group1_1's :position" do
|
737
|
-
assert_equal 1, @group1_1.position
|
738
|
-
assert_equal 1, @group1_1.reload.position
|
739
|
-
end
|
740
|
-
|
741
|
-
should "not update @group1_2's :position" do
|
742
|
-
assert_equal 2, @group1_2.position
|
743
|
-
assert_equal 2, @group1_2.reload.position
|
744
|
-
end
|
745
|
-
|
746
|
-
should "not update @group2_1's :position" do
|
747
|
-
assert_equal 1, @group2_1.position
|
748
|
-
assert_equal 1, @group2_1.reload.position
|
749
|
-
end
|
750
|
-
|
751
|
-
should "update @group2_3's :position to 2" do
|
752
|
-
assert_equal 3, @group2_3.position
|
753
|
-
assert_equal 2, @container.reload.scoped_items.find(@group2_3.id).position
|
754
|
-
end
|
755
|
-
end
|
756
|
-
|
757
|
-
end
|
758
|
-
|
759
|
-
end
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
describe "#update_positions_in_list!" do
|
764
|
-
|
765
|
-
context "on a Collection" do
|
766
|
-
|
767
|
-
setup do
|
768
|
-
@obj1 = Simple.create
|
769
|
-
@obj2 = Simple.create
|
770
|
-
@obj3 = Simple.create
|
771
|
-
Simple.update_positions_in_list!([ @obj2.id, @obj1.id, @obj3.id ])
|
772
|
-
end
|
773
|
-
|
774
|
-
should "change @obj1 from :position of 1 to 2" do
|
775
|
-
assert_equal 1, @obj1.position
|
776
|
-
assert_equal 2, @obj1.reload.position
|
777
|
-
end
|
778
|
-
|
779
|
-
should "change @obj2 from :position of 2 to 1" do
|
780
|
-
assert_equal 2, @obj2.position
|
781
|
-
assert_equal 1, @obj2.reload.position
|
782
|
-
end
|
783
|
-
|
784
|
-
should "not change @obj3 from :position of 3" do
|
785
|
-
assert_equal 3, @obj3.position
|
786
|
-
assert_equal 3, @obj3.reload.position
|
787
|
-
end
|
788
|
-
|
789
|
-
end
|
790
|
-
|
791
|
-
context "on an Embedded Collection" do
|
792
|
-
|
793
|
-
let :container do
|
794
|
-
Container.create!
|
795
|
-
end
|
796
|
-
|
797
|
-
setup do
|
798
|
-
@obj1 = container.items.create!
|
799
|
-
@obj2 = container.items.create!
|
800
|
-
@obj3 = container.items.create!
|
801
|
-
@obj4 = container.items.create!
|
802
|
-
container.items.update_positions_in_list!([ @obj2.id, @obj1.id, @obj4.id, @obj3.id ])
|
803
|
-
end
|
804
|
-
|
805
|
-
should "change @obj1 from :position of 1 to 2" do
|
806
|
-
assert_equal 1, @obj1.position
|
807
|
-
assert_equal 2, @obj1.reload.position
|
808
|
-
end
|
809
|
-
|
810
|
-
should "change @obj2 from :position of 2 to 1" do
|
811
|
-
assert_equal 2, @obj2.position
|
812
|
-
assert_equal 1, @obj2.reload.position
|
813
|
-
end
|
814
|
-
|
815
|
-
should "change @obj3 from :position of 3 to 4" do
|
816
|
-
assert_equal 3, @obj3.position
|
817
|
-
assert_equal 4, @obj3.reload.position
|
818
|
-
end
|
819
|
-
|
820
|
-
should "change @obj4 from :position of 4 to 3" do
|
821
|
-
assert_equal 4, @obj4.position
|
822
|
-
assert_equal 3, @obj4.reload.position
|
823
|
-
end
|
824
|
-
|
825
|
-
end
|
826
|
-
|
827
|
-
context "on a Deeply Embedded Collection" do
|
828
|
-
|
829
|
-
let :root do
|
830
|
-
Container.create!
|
831
|
-
end
|
832
|
-
|
833
|
-
let :embedded do
|
834
|
-
root.items.create!
|
835
|
-
end
|
836
|
-
|
837
|
-
setup do
|
838
|
-
@obj1 = embedded.items.create!
|
839
|
-
@obj2 = embedded.items.create!
|
840
|
-
@obj3 = embedded.items.create!
|
841
|
-
@obj4 = embedded.items.create!
|
842
|
-
embedded.items.update_positions_in_list!([ @obj3.id, @obj4.id, @obj1.id, @obj2.id ])
|
843
|
-
end
|
844
|
-
|
845
|
-
should "change @obj1 from :position of 1 to 3" do
|
846
|
-
assert_equal 1, @obj1.position
|
847
|
-
assert_equal 3, @obj1.reload.position
|
848
|
-
end
|
849
|
-
|
850
|
-
should "change @obj2 from :position of 2 to 4" do
|
851
|
-
assert_equal 2, @obj2.position
|
852
|
-
assert_equal 4, @obj2.reload.position
|
853
|
-
end
|
854
|
-
|
855
|
-
should "change @obj3 from :position of 3 to 1" do
|
856
|
-
assert_equal 3, @obj3.position
|
857
|
-
assert_equal 1, @obj3.reload.position
|
858
|
-
end
|
859
|
-
|
860
|
-
should "change @obj4 from :position of 4 to 2" do
|
861
|
-
assert_equal 4, @obj4.position
|
862
|
-
assert_equal 2, @obj4.reload.position
|
863
|
-
end
|
864
|
-
|
865
|
-
end
|
866
|
-
|
867
|
-
end
|
868
|
-
|
869
|
-
end
|