mongoid_orderable 5.0.0 → 5.1.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.
@@ -1,7 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Mongoid::Orderable do
4
-
5
4
  class SimpleOrderable
6
5
  include Mongoid::Document
7
6
  include Mongoid::Orderable
@@ -23,16 +22,16 @@ describe Mongoid::Orderable do
23
22
  field :group_id
24
23
  belongs_to :scoped_group
25
24
 
26
- orderable :scope => :group
25
+ orderable scope: :group
27
26
  end
28
27
 
29
28
  class StringScopedOrderable
30
29
  include Mongoid::Document
31
30
  include Mongoid::Orderable
32
31
 
33
- field :some_scope, :type => Integer
32
+ field :some_scope, type: Integer
34
33
 
35
- orderable :scope => 'some_scope'
34
+ orderable scope: 'some_scope'
36
35
  end
37
36
 
38
37
  class EmbedsOrderable
@@ -54,28 +53,28 @@ describe Mongoid::Orderable do
54
53
  include Mongoid::Document
55
54
  include Mongoid::Orderable
56
55
 
57
- orderable :column => :pos, :as => :my_position
56
+ orderable column: :pos, as: :my_position
58
57
  end
59
58
 
60
59
  class NoIndexOrderable
61
60
  include Mongoid::Document
62
61
  include Mongoid::Orderable
63
62
 
64
- orderable :index => false
63
+ orderable index: false
65
64
  end
66
65
 
67
66
  class ZeroBasedOrderable
68
67
  include Mongoid::Document
69
68
  include Mongoid::Orderable
70
69
 
71
- orderable :base => 0
70
+ orderable base: 0
72
71
  end
73
72
 
74
73
  class Fruit
75
74
  include Mongoid::Document
76
75
  include Mongoid::Orderable
77
76
 
78
- orderable :inherited => true
77
+ orderable inherited: true
79
78
  end
80
79
 
81
80
  class Apple < Fruit
@@ -88,10 +87,10 @@ describe Mongoid::Orderable do
88
87
  include Mongoid::Document
89
88
  include Mongoid::Orderable
90
89
 
91
- belongs_to :different_scope, :class_name => "ForeignKeyDiffersOrderable",
92
- :foreign_key => "different_orderable_id"
90
+ belongs_to :different_scope, class_name: 'ForeignKeyDiffersOrderable',
91
+ foreign_key: 'different_orderable_id'
93
92
 
94
- orderable :scope => :different_scope
93
+ orderable scope: :different_scope
95
94
  end
96
95
 
97
96
  class MultipleColumnsOrderable
@@ -102,9 +101,9 @@ describe Mongoid::Orderable do
102
101
 
103
102
  belongs_to :scoped_group
104
103
 
105
- orderable :column => :pos, :base => 0, :index => false, :as => :position
106
- orderable :column => :serial_no, :default => true
107
- orderable :column => :groups, :scope => :group
104
+ orderable column: :pos, base: 0, index: false, as: :position
105
+ orderable column: :serial_no, default: true
106
+ orderable column: :groups, scope: :group
108
107
  end
109
108
 
110
109
  class MultipleScopedOrderable
@@ -114,12 +113,11 @@ describe Mongoid::Orderable do
114
113
  belongs_to :apple
115
114
  belongs_to :orange
116
115
 
117
- orderable :column => :posa, :scope => :apple_id
118
- orderable :column => :poso, :scope => :orange_id
116
+ orderable column: :posa, scope: :apple_id
117
+ orderable column: :poso, scope: :orange_id
119
118
  end
120
119
 
121
120
  describe SimpleOrderable do
122
-
123
121
  before :each do
124
122
  SimpleOrderable.delete_all
125
123
  5.times do
@@ -137,12 +135,10 @@ describe Mongoid::Orderable do
137
135
  end
138
136
 
139
137
  it 'should have index on position column' do
140
- if ::Mongoid::Compatibility::Version.mongoid2?
141
- expect(SimpleOrderable.index_options[[[:position, 1]]]).not_to be_nil
142
- elsif ::Mongoid::Compatibility::Version.mongoid3?
143
- expect(SimpleOrderable.index_options[{:position => 1}]).not_to be_nil
138
+ if ::Mongoid::Compatibility::Version.mongoid3?
139
+ expect(SimpleOrderable.index_options[{ position: 1 }]).not_to be_nil
144
140
  else
145
- expect(SimpleOrderable.index_specifications.detect { |spec| spec.key == {:position => 1} }).not_to be_nil
141
+ expect(SimpleOrderable.index_specifications.detect { |spec| spec.key == { position: 1 } }).not_to be_nil
146
142
  end
147
143
  end
148
144
 
@@ -155,102 +151,99 @@ describe Mongoid::Orderable do
155
151
  end
156
152
 
157
153
  describe 'removement' do
158
-
159
154
  it 'top' do
160
- SimpleOrderable.where(:position => 1).destroy
155
+ SimpleOrderable.where(position: 1).destroy
161
156
  expect(positions).to eq([1, 2, 3, 4])
162
157
  end
163
158
 
164
159
  it 'bottom' do
165
- SimpleOrderable.where(:position => 5).destroy
160
+ SimpleOrderable.where(position: 5).destroy
166
161
  expect(positions).to eq([1, 2, 3, 4])
167
162
  end
168
163
 
169
164
  it 'middle' do
170
- SimpleOrderable.where(:position => 3).destroy
165
+ SimpleOrderable.where(position: 3).destroy
171
166
  expect(positions).to eq([1, 2, 3, 4])
172
167
  end
173
168
  end
174
169
 
175
170
  describe 'inserting' do
176
-
177
171
  it 'top' do
178
- newbie = SimpleOrderable.create! :move_to => :top
172
+ newbie = SimpleOrderable.create! move_to: :top
179
173
  expect(positions).to eq([1, 2, 3, 4, 5, 6])
180
174
  expect(newbie.position).to eq(1)
181
175
  end
182
176
 
183
177
  it 'bottom' do
184
- newbie = SimpleOrderable.create! :move_to => :bottom
178
+ newbie = SimpleOrderable.create! move_to: :bottom
185
179
  expect(positions).to eq([1, 2, 3, 4, 5, 6])
186
180
  expect(newbie.position).to eq(6)
187
181
  end
188
182
 
189
183
  it 'middle' do
190
- newbie = SimpleOrderable.create! :move_to => 4
184
+ newbie = SimpleOrderable.create! move_to: 4
191
185
  expect(positions).to eq([1, 2, 3, 4, 5, 6])
192
186
  expect(newbie.position).to eq(4)
193
187
  end
194
188
 
195
189
  it 'middle (with a numeric string)' do
196
- newbie = SimpleOrderable.create! :move_to => '4'
190
+ newbie = SimpleOrderable.create! move_to: '4'
197
191
  expect(positions).to eq([1, 2, 3, 4, 5, 6])
198
192
  expect(newbie.position).to eq(4)
199
193
  end
200
194
 
201
195
  it 'middle (with a non-numeric string)' do
202
196
  expect do
203
- SimpleOrderable.create! :move_to => 'four'
197
+ SimpleOrderable.create! move_to: 'four'
204
198
  end.to raise_error Mongoid::Orderable::Errors::InvalidTargetPosition
205
199
  end
206
200
  end
207
201
 
208
202
  describe 'movement' do
209
-
210
203
  it 'higher from top' do
211
- record = SimpleOrderable.where(:position => 1).first
212
- record.update_attributes :move_to => :higher
204
+ record = SimpleOrderable.where(position: 1).first
205
+ record.update_attributes move_to: :higher
213
206
  expect(positions).to eq([1, 2, 3, 4, 5])
214
207
  expect(record.reload.position).to eq(1)
215
208
  end
216
209
 
217
210
  it 'higher from bottom' do
218
- record = SimpleOrderable.where(:position => 5).first
219
- record.update_attributes :move_to => :higher
211
+ record = SimpleOrderable.where(position: 5).first
212
+ record.update_attributes move_to: :higher
220
213
  expect(positions).to eq([1, 2, 3, 4, 5])
221
214
  expect(record.reload.position).to eq(4)
222
215
  end
223
216
 
224
217
  it 'higher from middle' do
225
- record = SimpleOrderable.where(:position => 3).first
226
- record.update_attributes :move_to => :higher
218
+ record = SimpleOrderable.where(position: 3).first
219
+ record.update_attributes move_to: :higher
227
220
  expect(positions).to eq([1, 2, 3, 4, 5])
228
221
  expect(record.reload.position).to eq(2)
229
222
  end
230
223
 
231
224
  it 'lower from top' do
232
- record = SimpleOrderable.where(:position => 1).first
233
- record.update_attributes :move_to => :lower
225
+ record = SimpleOrderable.where(position: 1).first
226
+ record.update_attributes move_to: :lower
234
227
  expect(positions).to eq([1, 2, 3, 4, 5])
235
228
  expect(record.reload.position).to eq(2)
236
229
  end
237
230
 
238
231
  it 'lower from bottom' do
239
- record = SimpleOrderable.where(:position => 5).first
240
- record.update_attributes :move_to => :lower
232
+ record = SimpleOrderable.where(position: 5).first
233
+ record.update_attributes move_to: :lower
241
234
  expect(positions).to eq([1, 2, 3, 4, 5])
242
235
  expect(record.reload.position).to eq(5)
243
236
  end
244
237
 
245
238
  it 'lower from middle' do
246
- record = SimpleOrderable.where(:position => 3).first
247
- record.update_attributes :move_to => :lower
239
+ record = SimpleOrderable.where(position: 3).first
240
+ record.update_attributes move_to: :lower
248
241
  expect(positions).to eq([1, 2, 3, 4, 5])
249
242
  expect(record.reload.position).to eq(4)
250
243
  end
251
244
 
252
245
  it 'does nothing if position not change' do
253
- record = SimpleOrderable.where(:position => 3).first
246
+ record = SimpleOrderable.where(position: 3).first
254
247
  record.save
255
248
  expect(positions).to eq([1, 2, 3, 4, 5])
256
249
  expect(record.reload.position).to eq(3)
@@ -258,30 +251,28 @@ describe Mongoid::Orderable do
258
251
  end
259
252
 
260
253
  describe 'utiity methods' do
261
-
262
- it "should return a collection of items lower/higher on the list for next_items/previous_items" do
263
- record_1 = SimpleOrderable.where(:position => 1).first
264
- record_2 = SimpleOrderable.where(:position => 2).first
265
- record_3 = SimpleOrderable.where(:position => 3).first
266
- record_4 = SimpleOrderable.where(:position => 4).first
267
- record_5 = SimpleOrderable.where(:position => 5).first
268
- expect(record_1.next_items.to_a).to eq([record_2, record_3, record_4, record_5])
269
- expect(record_5.previous_items.to_a).to eq([record_1, record_2, record_3, record_4])
270
- expect(record_3.previous_items.to_a).to eq([record_1, record_2])
271
- expect(record_3.next_items.to_a).to eq([record_4, record_5])
254
+ it 'should return a collection of items lower/higher on the list for next_items/previous_items' do
255
+ record1 = SimpleOrderable.where(position: 1).first
256
+ record2 = SimpleOrderable.where(position: 2).first
257
+ record3 = SimpleOrderable.where(position: 3).first
258
+ record4 = SimpleOrderable.where(position: 4).first
259
+ record5 = SimpleOrderable.where(position: 5).first
260
+ expect(record1.next_items.to_a).to eq([record2, record3, record4, record5])
261
+ expect(record5.previous_items.to_a).to eq([record1, record2, record3, record4])
262
+ expect(record3.previous_items.to_a).to eq([record1, record2])
263
+ expect(record3.next_items.to_a).to eq([record4, record5])
272
264
  end
273
265
  end
274
266
  end
275
267
 
276
268
  describe ScopedOrderable do
277
-
278
269
  before :each do
279
270
  ScopedOrderable.delete_all
280
271
  2.times do
281
- ScopedOrderable.create! :group_id => 1
272
+ ScopedOrderable.create! group_id: 1
282
273
  end
283
274
  3.times do
284
- ScopedOrderable.create! :group_id => 2
275
+ ScopedOrderable.create! group_id: 2
285
276
  end
286
277
  end
287
278
 
@@ -294,118 +285,109 @@ describe Mongoid::Orderable do
294
285
  end
295
286
 
296
287
  describe 'removement' do
297
-
298
288
  it 'top' do
299
- ScopedOrderable.where(:position => 1, :group_id => 1).destroy
289
+ ScopedOrderable.where(position: 1, group_id: 1).destroy
300
290
  expect(positions).to eq([1, 1, 2, 3])
301
291
  end
302
292
 
303
293
  it 'bottom' do
304
- ScopedOrderable.where(:position => 3, :group_id => 2).destroy
294
+ ScopedOrderable.where(position: 3, group_id: 2).destroy
305
295
  expect(positions).to eq([1, 2, 1, 2])
306
296
  end
307
297
 
308
298
  it 'middle' do
309
- ScopedOrderable.where(:position => 2, :group_id => 2).destroy
299
+ ScopedOrderable.where(position: 2, group_id: 2).destroy
310
300
  expect(positions).to eq([1, 2, 1, 2])
311
301
  end
312
302
  end
313
303
 
314
304
  describe 'inserting' do
315
-
316
305
  it 'top' do
317
- newbie = ScopedOrderable.create! :move_to => :top, :group_id => 1
306
+ newbie = ScopedOrderable.create! move_to: :top, group_id: 1
318
307
  expect(positions).to eq([1, 2, 3, 1, 2, 3])
319
308
  expect(newbie.position).to eq(1)
320
309
  end
321
310
 
322
311
  it 'bottom' do
323
- newbie = ScopedOrderable.create! :move_to => :bottom, :group_id => 2
312
+ newbie = ScopedOrderable.create! move_to: :bottom, group_id: 2
324
313
  expect(positions).to eq([1, 2, 1, 2, 3, 4])
325
314
  expect(newbie.position).to eq(4)
326
315
  end
327
316
 
328
317
  it 'middle' do
329
- newbie = ScopedOrderable.create! :move_to => 2, :group_id => 2
318
+ newbie = ScopedOrderable.create! move_to: 2, group_id: 2
330
319
  expect(positions).to eq([1, 2, 1, 2, 3, 4])
331
320
  expect(newbie.position).to eq(2)
332
321
  end
333
322
 
334
323
  it 'middle (with a numeric string)' do
335
- newbie = ScopedOrderable.create! :move_to => '2', :group_id => 2
324
+ newbie = ScopedOrderable.create! move_to: '2', group_id: 2
336
325
  expect(positions).to eq([1, 2, 1, 2, 3, 4])
337
326
  expect(newbie.position).to eq(2)
338
327
  end
339
328
 
340
329
  it 'middle (with a non-numeric string)' do
341
330
  expect do
342
- ScopedOrderable.create! :move_to => 'two', :group_id => 2
331
+ ScopedOrderable.create! move_to: 'two', group_id: 2
343
332
  end.to raise_error Mongoid::Orderable::Errors::InvalidTargetPosition
344
333
  end
345
334
  end
346
335
 
347
336
  describe 'index' do
348
-
349
337
  it 'is not on position alone' do
350
- if ::Mongoid::Compatibility::Version.mongoid2?
351
- expect(ScopedOrderable.index_options[[[:position, 1]]]).to be_nil
352
- elsif ::Mongoid::Compatibility::Version.mongoid3?
353
- expect(ScopedOrderable.index_options[{:position => 1}]).to be_nil
338
+ if ::Mongoid::Compatibility::Version.mongoid3?
339
+ expect(ScopedOrderable.index_options[{ position: 1 }]).to be_nil
354
340
  else
355
- expect(ScopedOrderable.index_specifications.detect { |spec| spec.key == {:position => 1} }).to be_nil
341
+ expect(ScopedOrderable.index_specifications.detect { |spec| spec.key == { position: 1 } }).to be_nil
356
342
  end
357
343
  end
358
344
 
359
345
  it 'is on compound fields' do
360
- if ::Mongoid::Compatibility::Version.mongoid2?
361
- expect(ScopedOrderable.index_options[[[:group_id, 1], [:position, 1]]]).to_not be_nil
362
- elsif ::Mongoid::Compatibility::Version.mongoid3?
363
- expect(ScopedOrderable.index_options[{:group_id => 1, :position => 1}]).to_not be_nil
346
+ if ::Mongoid::Compatibility::Version.mongoid3?
347
+ expect(ScopedOrderable.index_options[{ group_id: 1, position: 1 }]).to_not be_nil
364
348
  else
365
- expect(ScopedOrderable.index_specifications.detect { |spec| spec.key == {:group_id => 1, :position => 1} }).to_not be_nil
349
+ expect(ScopedOrderable.index_specifications.detect { |spec| spec.key == { group_id: 1, position: 1 } }).to_not be_nil
366
350
  end
367
351
  end
368
352
  end
369
353
 
370
354
  describe 'scope movement' do
371
-
372
- let(:record){ ScopedOrderable.where(:group_id => 2, :position => 2).first }
355
+ let(:record) { ScopedOrderable.where(group_id: 2, position: 2).first }
373
356
 
374
357
  it 'to a new scope group' do
375
- record.update_attributes :group_id => 3
358
+ record.update_attributes group_id: 3
376
359
  expect(positions).to eq([1, 2, 1, 2, 1])
377
360
  expect(record.position).to eq(1)
378
361
  end
379
362
 
380
363
  context 'when moving to an existing scope group' do
381
-
382
364
  it 'without a position' do
383
- record.update_attributes :group_id => 1
365
+ record.update_attributes group_id: 1
384
366
  expect(positions).to eq([1, 2, 3, 1, 2])
385
367
  expect(record.reload.position).to eq(3)
386
368
  end
387
369
 
388
370
  it 'with symbol position' do
389
- record.update_attributes :group_id => 1, :move_to => :top
371
+ record.update_attributes group_id: 1, move_to: :top
390
372
  expect(positions).to eq([1, 2, 3, 1, 2])
391
373
  expect(record.reload.position).to eq(1)
392
374
  end
393
375
 
394
376
  it 'with point position' do
395
- record.update_attributes :group_id => 1, :move_to => 2
377
+ record.update_attributes group_id: 1, move_to: 2
396
378
  expect(positions).to eq([1, 2, 3, 1, 2])
397
379
  expect(record.reload.position).to eq(2)
398
380
  end
399
381
 
400
382
  it 'with point position (with a numeric string)' do
401
- record.update_attributes :group_id => 1, :move_to => '2'
383
+ record.update_attributes group_id: 1, move_to: '2'
402
384
  expect(positions).to eq([1, 2, 3, 1, 2])
403
385
  expect(record.reload.position).to eq(2)
404
386
  end
405
387
 
406
388
  it 'with point position (with a non-numeric string)' do
407
389
  expect do
408
- record.update_attributes :group_id => 1, :move_to => 'two'
390
+ record.update_attributes group_id: 1, move_to: 'two'
409
391
  end.to raise_error Mongoid::Orderable::Errors::InvalidTargetPosition
410
392
  end
411
393
  end
@@ -414,8 +396,7 @@ describe Mongoid::Orderable do
414
396
  if defined?(Mongoid::IdentityMap)
415
397
 
416
398
  context 'when identity map is enabled' do
417
-
418
- let(:record){ ScopedOrderable.where(:group_id => 2, :position => 2).first }
399
+ let(:record) { ScopedOrderable.where(group_id: 2, position: 2).first }
419
400
 
420
401
  before do
421
402
  Mongoid.identity_map_enabled = true
@@ -427,63 +408,61 @@ describe Mongoid::Orderable do
427
408
  end
428
409
 
429
410
  it 'to a new scope group' do
430
- record.update_attributes :group_id => 3
411
+ record.update_attributes group_id: 3
431
412
  expect(positions).to eq([1, 2, 1, 2, 1])
432
413
  expect(record.position).to eq(1)
433
414
  end
434
415
 
435
416
  it 'to an existing scope group' do
436
- record.update_attributes :group_id => 1, :move_to => 2
417
+ record.update_attributes group_id: 1, move_to: 2
437
418
  expect(positions).to eq([1, 2, 3, 1, 2])
438
419
  expect(record.reload.position).to eq(2)
439
420
  end
440
421
 
441
422
  it 'to an existing scope group (with a numeric string)' do
442
- record.update_attributes :group_id => 1, :move_to => '2'
423
+ record.update_attributes group_id: 1, move_to: '2'
443
424
  expect(positions).to eq([1, 2, 3, 1, 2])
444
425
  expect(record.reload.position).to eq(2)
445
426
  end
446
427
 
447
428
  it 'to an existing scope group (with a non-numeric string)' do
448
429
  expect do
449
- record.update_attributes :group_id => 1, :move_to => 'two'
430
+ record.update_attributes group_id: 1, move_to: 'two'
450
431
  end.to raise_error Mongoid::Orderable::Errors::InvalidTargetPosition
451
432
  end
452
433
  end
453
434
  end
454
435
 
455
436
  describe 'utility methods' do
456
-
457
437
  before do
458
438
  ScopedOrderable.delete_all
459
439
  5.times { ScopedOrderable.create! }
460
440
  end
461
441
 
462
- it "should return a collection of items lower/higher on the list for next_items/previous_items" do
463
- record_1 = ScopedOrderable.where(:position => 1).first
464
- record_2 = ScopedOrderable.where(:position => 2).first
465
- record_3 = ScopedOrderable.where(:position => 3).first
466
- record_4 = ScopedOrderable.where(:position => 4).first
467
- record_5 = ScopedOrderable.where(:position => 5).first
468
- expect(record_1.next_items.to_a).to eq([record_2, record_3, record_4, record_5])
469
- expect(record_5.previous_items.to_a).to eq([record_1, record_2, record_3, record_4])
470
- expect(record_3.previous_items.to_a).to eq([record_1, record_2])
471
- expect(record_3.next_items.to_a).to eq([record_4, record_5])
442
+ it 'should return a collection of items lower/higher on the list for next_items/previous_items' do
443
+ record1 = ScopedOrderable.where(position: 1).first
444
+ record2 = ScopedOrderable.where(position: 2).first
445
+ record3 = ScopedOrderable.where(position: 3).first
446
+ record4 = ScopedOrderable.where(position: 4).first
447
+ record5 = ScopedOrderable.where(position: 5).first
448
+ expect(record1.next_items.to_a).to eq([record2, record3, record4, record5])
449
+ expect(record5.previous_items.to_a).to eq([record1, record2, record3, record4])
450
+ expect(record3.previous_items.to_a).to eq([record1, record2])
451
+ expect(record3.next_items.to_a).to eq([record4, record5])
472
452
  # next_item & previous_item testing
473
- expect(record_1.next_item).to eq(record_2)
474
- expect(record_2.previous_item).to eq(record_1)
475
- expect(record_1.previous_item).to eq(nil)
476
- expect(record_5.next_item).to eq(nil)
453
+ expect(record1.next_item).to eq(record2)
454
+ expect(record2.previous_item).to eq(record1)
455
+ expect(record1.previous_item).to eq(nil)
456
+ expect(record5.next_item).to eq(nil)
477
457
  end
478
458
  end
479
459
  end
480
460
 
481
461
  describe StringScopedOrderable do
482
-
483
462
  it 'uses the foreign key of the relationship as scope' do
484
- orderable1 = StringScopedOrderable.create!(:some_scope => 1)
485
- orderable2 = StringScopedOrderable.create!(:some_scope => 1)
486
- orderable3 = StringScopedOrderable.create!(:some_scope => 2)
463
+ orderable1 = StringScopedOrderable.create!(some_scope: 1)
464
+ orderable2 = StringScopedOrderable.create!(some_scope: 1)
465
+ orderable3 = StringScopedOrderable.create!(some_scope: 2)
487
466
  expect(orderable1.position).to eq 1
488
467
  expect(orderable2.position).to eq 2
489
468
  expect(orderable3.position).to eq 1
@@ -491,7 +470,6 @@ describe Mongoid::Orderable do
491
470
  end
492
471
 
493
472
  describe EmbeddedOrderable do
494
-
495
473
  before :each do
496
474
  EmbedsOrderable.delete_all
497
475
  eo = EmbedsOrderable.create!
@@ -505,7 +483,7 @@ describe Mongoid::Orderable do
505
483
  end
506
484
 
507
485
  def positions
508
- EmbedsOrderable.order_by(:position => 1).all.map { |eo| eo.embedded_orderables.map(&:position).sort }
486
+ EmbedsOrderable.order_by(position: 1).all.map { |eo| eo.embedded_orderables.map(&:position).sort }
509
487
  end
510
488
 
511
489
  it 'sets proper position while creation' do
@@ -513,15 +491,14 @@ describe Mongoid::Orderable do
513
491
  end
514
492
 
515
493
  it 'moves an item returned by a query to position' do
516
- embedded_orderable_1 = EmbedsOrderable.first.embedded_orderables.where(:position => 1).first
517
- embedded_orderable_2 = EmbedsOrderable.first.embedded_orderables.where(:position => 2).first
518
- embedded_orderable_1.move_to! 2
519
- expect(embedded_orderable_2.reload.position).to eq(1)
494
+ embedded_orderable1 = EmbedsOrderable.first.embedded_orderables.where(position: 1).first
495
+ embedded_orderable2 = EmbedsOrderable.first.embedded_orderables.where(position: 2).first
496
+ embedded_orderable1.move_to! 2
497
+ expect(embedded_orderable2.reload.position).to eq(1)
520
498
  end
521
499
  end
522
500
 
523
501
  describe CustomizedOrderable do
524
-
525
502
  it 'does not have default position field' do
526
503
  expect(CustomizedOrderable.fields).not_to have_key('position')
527
504
  end
@@ -538,9 +515,8 @@ describe Mongoid::Orderable do
538
515
  end
539
516
 
540
517
  describe NoIndexOrderable do
541
-
542
518
  it 'should not have index on position column' do
543
- if ::Mongoid::Compatibility::Version.mongoid2? || ::Mongoid::Compatibility::Version.mongoid3?
519
+ if ::Mongoid::Compatibility::Version.mongoid3?
544
520
  expect(NoIndexOrderable.index_options[[[:position, 1]]]).to be_nil
545
521
  else
546
522
  expect(NoIndexOrderable.index_specifications.detect { |spec| spec.key == :position }).to be_nil
@@ -549,7 +525,6 @@ describe Mongoid::Orderable do
549
525
  end
550
526
 
551
527
  describe ZeroBasedOrderable do
552
-
553
528
  before :each do
554
529
  ZeroBasedOrderable.delete_all
555
530
  5.times do
@@ -570,7 +545,7 @@ describe Mongoid::Orderable do
570
545
  end
571
546
 
572
547
  describe 'reset position' do
573
- before{ ZeroBasedOrderable.update_all({:position => nil}) }
548
+ before { ZeroBasedOrderable.update_all(position: nil) }
574
549
  it 'should properly reset position' do
575
550
  ZeroBasedOrderable.all.map(&:save)
576
551
  expect(positions).to eq([0, 1, 2, 3, 4])
@@ -578,102 +553,99 @@ describe Mongoid::Orderable do
578
553
  end
579
554
 
580
555
  describe 'removement' do
581
-
582
556
  it 'top' do
583
- ZeroBasedOrderable.where(:position => 0).destroy
557
+ ZeroBasedOrderable.where(position: 0).destroy
584
558
  expect(positions).to eq([0, 1, 2, 3])
585
559
  end
586
560
 
587
561
  it 'bottom' do
588
- ZeroBasedOrderable.where(:position => 4).destroy
562
+ ZeroBasedOrderable.where(position: 4).destroy
589
563
  expect(positions).to eq([0, 1, 2, 3])
590
564
  end
591
565
 
592
566
  it 'middle' do
593
- ZeroBasedOrderable.where(:position => 2).destroy
567
+ ZeroBasedOrderable.where(position: 2).destroy
594
568
  expect(positions).to eq([0, 1, 2, 3])
595
569
  end
596
570
  end
597
571
 
598
572
  describe 'inserting' do
599
-
600
573
  it 'top' do
601
- newbie = ZeroBasedOrderable.create! :move_to => :top
574
+ newbie = ZeroBasedOrderable.create! move_to: :top
602
575
  expect(positions).to eq([0, 1, 2, 3, 4, 5])
603
576
  expect(newbie.position).to eq(0)
604
577
  end
605
578
 
606
579
  it 'bottom' do
607
- newbie = ZeroBasedOrderable.create! :move_to => :bottom
580
+ newbie = ZeroBasedOrderable.create! move_to: :bottom
608
581
  expect(positions).to eq([0, 1, 2, 3, 4, 5])
609
582
  expect(newbie.position).to eq(5)
610
583
  end
611
584
 
612
585
  it 'middle' do
613
- newbie = ZeroBasedOrderable.create! :move_to => 3
586
+ newbie = ZeroBasedOrderable.create! move_to: 3
614
587
  expect(positions).to eq([0, 1, 2, 3, 4, 5])
615
588
  expect(newbie.position).to eq(3)
616
589
  end
617
590
 
618
591
  it 'middle (with a numeric string)' do
619
- newbie = ZeroBasedOrderable.create! :move_to => '3'
592
+ newbie = ZeroBasedOrderable.create! move_to: '3'
620
593
  expect(positions).to eq([0, 1, 2, 3, 4, 5])
621
594
  expect(newbie.position).to eq(3)
622
595
  end
623
596
 
624
597
  it 'middle (with a non-numeric string)' do
625
598
  expect do
626
- ZeroBasedOrderable.create! :move_to => 'three'
599
+ ZeroBasedOrderable.create! move_to: 'three'
627
600
  end.to raise_error Mongoid::Orderable::Errors::InvalidTargetPosition
628
601
  end
629
602
  end
630
603
 
631
604
  describe 'movement' do
632
-
633
605
  it 'higher from top' do
634
- record = ZeroBasedOrderable.where(:position => 0).first
635
- record.update_attributes :move_to => :higher
606
+ record = ZeroBasedOrderable.where(position: 0).first
607
+ record.update_attributes move_to: :higher
636
608
  expect(positions).to eq([0, 1, 2, 3, 4])
637
609
  expect(record.reload.position).to eq(0)
638
610
  end
639
611
 
640
612
  it 'higher from bottom' do
641
- record = ZeroBasedOrderable.where(:position => 4).first
642
- record.update_attributes :move_to => :higher
613
+ record = ZeroBasedOrderable.where(position: 4).first
614
+ record.update_attributes move_to: :higher
643
615
  expect(positions).to eq([0, 1, 2, 3, 4])
644
616
  expect(record.reload.position).to eq(3)
645
617
  end
646
618
 
647
619
  it 'higher from middle' do
648
- record = ZeroBasedOrderable.where(:position => 3).first
649
- record.update_attributes :move_to => :higher
620
+ record = ZeroBasedOrderable.where(position: 3).first
621
+ record.update_attributes move_to: :higher
650
622
  expect(positions).to eq([0, 1, 2, 3, 4])
651
623
  expect(record.reload.position).to eq(2)
652
624
  end
653
625
 
654
626
  it 'lower from top' do
655
- record = ZeroBasedOrderable.where(:position => 0).first
656
- record.update_attributes :move_to => :lower
627
+ record = ZeroBasedOrderable.where(position: 0).first
628
+ record.update_attributes move_to: :lower
657
629
  expect(positions).to eq([0, 1, 2, 3, 4])
658
630
  expect(record.reload.position).to eq(1)
659
631
  end
660
632
 
661
633
  it 'lower from bottom' do
662
- record = ZeroBasedOrderable.where(:position => 4).first
663
- record.update_attributes :move_to => :lower
634
+ record = ZeroBasedOrderable.where(position: 4).first
635
+ record.update_attributes move_to: :lower
664
636
  expect(positions).to eq([0, 1, 2, 3, 4])
665
637
  expect(record.reload.position).to eq(4)
666
638
  end
667
639
 
668
640
  it 'lower from middle' do
669
- record = ZeroBasedOrderable.where(:position => 2).first
670
- record.update_attributes :move_to => :lower
641
+ record = ZeroBasedOrderable.where(position: 2).first
642
+ record.update_attributes move_to: :lower
671
643
  expect(positions).to eq([0, 1, 2, 3, 4])
672
644
  expect(record.reload.position).to eq(3)
673
645
  end
674
646
 
675
647
  it 'does nothing if position not change' do
676
- record = ZeroBasedOrderable.where(:position => 3).first
648
+ record = ZeroBasedOrderable.where(position: 3).first
677
649
  record.save
678
650
  expect(positions).to eq([0, 1, 2, 3, 4])
679
651
  expect(record.reload.position).to eq(3)
@@ -681,28 +653,26 @@ describe Mongoid::Orderable do
681
653
  end
682
654
 
683
655
  describe 'utiity methods' do
684
-
685
- it "should return a collection of items lower/higher on the list for next_items/previous_items" do
686
- record_1 = SimpleOrderable.where(:position => 1).first
687
- record_2 = SimpleOrderable.where(:position => 2).first
688
- record_3 = SimpleOrderable.where(:position => 3).first
689
- record_4 = SimpleOrderable.where(:position => 4).first
690
- record_5 = SimpleOrderable.where(:position => 5).first
691
- expect(record_1.next_items.to_a).to eq([record_2, record_3, record_4, record_5])
692
- expect(record_5.previous_items.to_a).to eq([record_1, record_2, record_3, record_4])
693
- expect(record_3.previous_items.to_a).to eq([record_1, record_2])
694
- expect(record_3.next_items.to_a).to eq([record_4, record_5])
656
+ it 'should return a collection of items lower/higher on the list for next_items/previous_items' do
657
+ record1 = SimpleOrderable.where(position: 1).first
658
+ record2 = SimpleOrderable.where(position: 2).first
659
+ record3 = SimpleOrderable.where(position: 3).first
660
+ record4 = SimpleOrderable.where(position: 4).first
661
+ record5 = SimpleOrderable.where(position: 5).first
662
+ expect(record1.next_items.to_a).to eq([record2, record3, record4, record5])
663
+ expect(record5.previous_items.to_a).to eq([record1, record2, record3, record4])
664
+ expect(record3.previous_items.to_a).to eq([record1, record2])
665
+ expect(record3.next_items.to_a).to eq([record4, record5])
695
666
  # next_item & previous_item testing
696
- expect(record_1.next_item).to eq(record_2)
697
- expect(record_2.previous_item).to eq(record_1)
698
- expect(record_1.previous_item).to eq(nil)
699
- expect(record_5.next_item).to eq(nil)
667
+ expect(record1.next_item).to eq(record2)
668
+ expect(record2.previous_item).to eq(record1)
669
+ expect(record1.previous_item).to eq(nil)
670
+ expect(record5.next_item).to eq(nil)
700
671
  end
701
672
  end
702
673
  end
703
674
 
704
675
  describe Fruit do
705
-
706
676
  it 'should set proper position' do
707
677
  fruit1 = Apple.create
708
678
  fruit2 = Orange.create
@@ -740,7 +710,7 @@ describe Mongoid::Orderable do
740
710
  describe 'add orderable configurations in inherited class' do
741
711
  it 'does not affect the orderable configurations of parent class and sibling class' do
742
712
  class Apple
743
- orderable :column => :serial
713
+ orderable column: :serial
744
714
  end
745
715
  expect(Fruit.orderable_configurations).not_to eq Apple.orderable_configurations
746
716
  expect(Orange.orderable_configurations).not_to eq Apple.orderable_configurations
@@ -750,15 +720,14 @@ describe Mongoid::Orderable do
750
720
  end
751
721
 
752
722
  describe ForeignKeyDiffersOrderable do
753
-
754
723
  it 'uses the foreign key of the relationship as scope' do
755
724
  orderable1, orderable2, orderable3 = nil
756
725
  parent_scope1 = ForeignKeyDiffersOrderable.create
757
726
  parent_scope2 = ForeignKeyDiffersOrderable.create
758
727
  expect do
759
- orderable1 = ForeignKeyDiffersOrderable.create!(:different_scope => parent_scope1)
760
- orderable2 = ForeignKeyDiffersOrderable.create!(:different_scope => parent_scope1)
761
- orderable3 = ForeignKeyDiffersOrderable.create!(:different_scope => parent_scope2)
728
+ orderable1 = ForeignKeyDiffersOrderable.create!(different_scope: parent_scope1)
729
+ orderable2 = ForeignKeyDiffersOrderable.create!(different_scope: parent_scope1)
730
+ orderable3 = ForeignKeyDiffersOrderable.create!(different_scope: parent_scope2)
762
731
  end.to_not raise_error
763
732
  expect(orderable1.position).to eq 1
764
733
  expect(orderable2.position).to eq 2
@@ -767,7 +736,6 @@ describe Mongoid::Orderable do
767
736
  end
768
737
 
769
738
  describe MultipleColumnsOrderable do
770
-
771
739
  before :each do
772
740
  MultipleColumnsOrderable.delete_all
773
741
  5.times do
@@ -776,10 +744,10 @@ describe Mongoid::Orderable do
776
744
  end
777
745
 
778
746
  context 'default orderable' do
779
- let(:serial_nos){ MultipleColumnsOrderable.all.map(&:serial_no).sort }
747
+ let(:serial_nos) { MultipleColumnsOrderable.all.map(&:serial_no).sort }
780
748
 
781
749
  describe 'inserting' do
782
- let(:newbie){ MultipleColumnsOrderable.create! }
750
+ let(:newbie) { MultipleColumnsOrderable.create! }
783
751
 
784
752
  before { @position = newbie.position }
785
753
 
@@ -807,7 +775,7 @@ describe Mongoid::Orderable do
807
775
 
808
776
  describe 'movement' do
809
777
  it 'higher from top' do
810
- record = MultipleColumnsOrderable.where(:serial_no => 1).first
778
+ record = MultipleColumnsOrderable.where(serial_no: 1).first
811
779
  position = record.position
812
780
  record.move_higher!
813
781
  expect(serial_nos).to eq([1, 2, 3, 4, 5])
@@ -816,7 +784,7 @@ describe Mongoid::Orderable do
816
784
  end
817
785
 
818
786
  it 'higher from bottom' do
819
- record = MultipleColumnsOrderable.where(:serial_no => 5).first
787
+ record = MultipleColumnsOrderable.where(serial_no: 5).first
820
788
  position = record.position
821
789
  record.move_higher!
822
790
  expect(serial_nos).to eq([1, 2, 3, 4, 5])
@@ -825,7 +793,7 @@ describe Mongoid::Orderable do
825
793
  end
826
794
 
827
795
  it 'higher from middle' do
828
- record = MultipleColumnsOrderable.where(:serial_no => 3).first
796
+ record = MultipleColumnsOrderable.where(serial_no: 3).first
829
797
  position = record.position
830
798
  record.move_higher!
831
799
  expect(serial_nos).to eq([1, 2, 3, 4, 5])
@@ -834,7 +802,7 @@ describe Mongoid::Orderable do
834
802
  end
835
803
 
836
804
  it 'lower from top' do
837
- record = MultipleColumnsOrderable.where(:serial_no => 1).first
805
+ record = MultipleColumnsOrderable.where(serial_no: 1).first
838
806
  position = record.position
839
807
  record.move_lower!
840
808
  expect(serial_nos).to eq([1, 2, 3, 4, 5])
@@ -843,7 +811,7 @@ describe Mongoid::Orderable do
843
811
  end
844
812
 
845
813
  it 'lower from bottom' do
846
- record = MultipleColumnsOrderable.where(:serial_no => 5).first
814
+ record = MultipleColumnsOrderable.where(serial_no: 5).first
847
815
  position = record.position
848
816
  record.move_lower!
849
817
  expect(serial_nos).to eq([1, 2, 3, 4, 5])
@@ -852,7 +820,7 @@ describe Mongoid::Orderable do
852
820
  end
853
821
 
854
822
  it 'lower from middle' do
855
- record = MultipleColumnsOrderable.where(:serial_no => 3).first
823
+ record = MultipleColumnsOrderable.where(serial_no: 3).first
856
824
  position = record.position
857
825
  record.move_lower!
858
826
  expect(serial_nos).to eq([1, 2, 3, 4, 5])
@@ -862,38 +830,36 @@ describe Mongoid::Orderable do
862
830
  end
863
831
 
864
832
  describe 'utility methods' do
865
-
866
833
  before do
867
- @record_1 = MultipleColumnsOrderable.where(:serial_no => 1).first
868
- @record_2 = MultipleColumnsOrderable.where(:serial_no => 2).first
869
- @record_3 = MultipleColumnsOrderable.where(:serial_no => 3).first
870
- @record_4 = MultipleColumnsOrderable.where(:serial_no => 4).first
871
- @record_5 = MultipleColumnsOrderable.where(:serial_no => 5).first
834
+ @record1 = MultipleColumnsOrderable.where(serial_no: 1).first
835
+ @record2 = MultipleColumnsOrderable.where(serial_no: 2).first
836
+ @record3 = MultipleColumnsOrderable.where(serial_no: 3).first
837
+ @record4 = MultipleColumnsOrderable.where(serial_no: 4).first
838
+ @record5 = MultipleColumnsOrderable.where(serial_no: 5).first
872
839
  end
873
840
 
874
- it "should return the lower/higher item on the list for next_item/previous_item" do
875
- expect(@record_1.next_item).to eq(@record_2)
876
- expect(@record_3.next_item).to eq(@record_4)
877
- expect(@record_5.next_item).to eq(nil)
878
- expect(@record_1.prev_item).to eq(nil)
879
- expect(@record_3.prev_item).to eq(@record_2)
880
- expect(@record_5.prev_item).to eq(@record_4)
841
+ it 'should return the lower/higher item on the list for next_item/previous_item' do
842
+ expect(@record1.next_item).to eq(@record2)
843
+ expect(@record3.next_item).to eq(@record4)
844
+ expect(@record5.next_item).to eq(nil)
845
+ expect(@record1.prev_item).to eq(nil)
846
+ expect(@record3.prev_item).to eq(@record2)
847
+ expect(@record5.prev_item).to eq(@record4)
881
848
  end
882
849
 
883
- it "should return a collection of items lower/higher on the list for next_items/previous_items" do
884
- expect(@record_1.next_items.to_a).to eq([@record_2, @record_3, @record_4, @record_5])
885
- expect(@record_3.next_items.to_a).to eq([@record_4, @record_5])
886
- expect(@record_5.next_items.to_a).to eq([])
887
- expect(@record_1.previous_items.to_a).to eq([])
888
- expect(@record_3.previous_items.to_a).to eq([@record_1, @record_2])
889
- expect(@record_5.previous_items.to_a).to eq([@record_1, @record_2, @record_3, @record_4])
850
+ it 'should return a collection of items lower/higher on the list for next_items/previous_items' do
851
+ expect(@record1.next_items.to_a).to eq([@record2, @record3, @record4, @record5])
852
+ expect(@record3.next_items.to_a).to eq([@record4, @record5])
853
+ expect(@record5.next_items.to_a).to eq([])
854
+ expect(@record1.previous_items.to_a).to eq([])
855
+ expect(@record3.previous_items.to_a).to eq([@record1, @record2])
856
+ expect(@record5.previous_items.to_a).to eq([@record1, @record2, @record3, @record4])
890
857
  end
891
858
  end
892
859
  end
893
860
 
894
861
  context 'serial_no orderable' do
895
-
896
- let(:serial_nos){ MultipleColumnsOrderable.all.map(&:serial_no).sort }
862
+ let(:serial_nos) { MultipleColumnsOrderable.all.map(&:serial_no).sort }
897
863
 
898
864
  it 'should have proper serial_no column' do
899
865
  expect(MultipleColumnsOrderable.fields.key?('serial_no')).to be true
@@ -901,12 +867,10 @@ describe Mongoid::Orderable do
901
867
  end
902
868
 
903
869
  it 'should have index on serial_no column' do
904
- if ::Mongoid::Compatibility::Version.mongoid2?
905
- expect(MultipleColumnsOrderable.index_options[[[:serial_no, 1]]]).not_to be_nil
906
- elsif ::Mongoid::Compatibility::Version.mongoid3?
907
- expect(MultipleColumnsOrderable.index_options[{:serial_no => 1}]).not_to be_nil
870
+ if ::Mongoid::Compatibility::Version.mongoid3?
871
+ expect(MultipleColumnsOrderable.index_options[{ serial_no: 1 }]).not_to be_nil
908
872
  else
909
- expect(MultipleColumnsOrderable.index_specifications.detect { |spec| spec.key == {:serial_no => 1} }).not_to be_nil
873
+ expect(MultipleColumnsOrderable.index_specifications.detect { |spec| spec.key == { serial_no: 1 } }).not_to be_nil
910
874
  end
911
875
  end
912
876
 
@@ -919,25 +883,24 @@ describe Mongoid::Orderable do
919
883
  end
920
884
 
921
885
  describe 'removement' do
922
-
923
886
  it 'top' do
924
- MultipleColumnsOrderable.where(:serial_no => 1).destroy
887
+ MultipleColumnsOrderable.where(serial_no: 1).destroy
925
888
  expect(serial_nos).to eq([1, 2, 3, 4])
926
889
  end
927
890
 
928
891
  it 'bottom' do
929
- MultipleColumnsOrderable.where(:serial_no => 5).destroy
892
+ MultipleColumnsOrderable.where(serial_no: 5).destroy
930
893
  expect(serial_nos).to eq([1, 2, 3, 4])
931
894
  end
932
895
 
933
896
  it 'middle' do
934
- MultipleColumnsOrderable.where(:serial_no => 3).destroy
897
+ MultipleColumnsOrderable.where(serial_no: 3).destroy
935
898
  expect(serial_nos).to eq([1, 2, 3, 4])
936
899
  end
937
900
  end
938
901
 
939
902
  describe 'inserting' do
940
- let(:newbie){ MultipleColumnsOrderable.create! }
903
+ let(:newbie) { MultipleColumnsOrderable.create! }
941
904
 
942
905
  before { @position = newbie.position }
943
906
 
@@ -965,7 +928,7 @@ describe Mongoid::Orderable do
965
928
 
966
929
  describe 'movement' do
967
930
  it 'higher from top' do
968
- record = MultipleColumnsOrderable.where(:serial_no => 1).first
931
+ record = MultipleColumnsOrderable.where(serial_no: 1).first
969
932
  position = record.position
970
933
  record.move_serial_no_higher!
971
934
  expect(serial_nos).to eq([1, 2, 3, 4, 5])
@@ -974,7 +937,7 @@ describe Mongoid::Orderable do
974
937
  end
975
938
 
976
939
  it 'higher from bottom' do
977
- record = MultipleColumnsOrderable.where(:serial_no => 5).first
940
+ record = MultipleColumnsOrderable.where(serial_no: 5).first
978
941
  position = record.position
979
942
  record.move_serial_no_higher!
980
943
  expect(serial_nos).to eq([1, 2, 3, 4, 5])
@@ -983,7 +946,7 @@ describe Mongoid::Orderable do
983
946
  end
984
947
 
985
948
  it 'higher from middle' do
986
- record = MultipleColumnsOrderable.where(:serial_no => 3).first
949
+ record = MultipleColumnsOrderable.where(serial_no: 3).first
987
950
  position = record.position
988
951
  record.move_serial_no_higher!
989
952
  expect(serial_nos).to eq([1, 2, 3, 4, 5])
@@ -992,7 +955,7 @@ describe Mongoid::Orderable do
992
955
  end
993
956
 
994
957
  it 'lower from top' do
995
- record = MultipleColumnsOrderable.where(:serial_no => 1).first
958
+ record = MultipleColumnsOrderable.where(serial_no: 1).first
996
959
  position = record.position
997
960
  record.move_serial_no_lower!
998
961
  expect(serial_nos).to eq([1, 2, 3, 4, 5])
@@ -1001,7 +964,7 @@ describe Mongoid::Orderable do
1001
964
  end
1002
965
 
1003
966
  it 'lower from bottom' do
1004
- record = MultipleColumnsOrderable.where(:serial_no => 5).first
967
+ record = MultipleColumnsOrderable.where(serial_no: 5).first
1005
968
  position = record.position
1006
969
  record.move_serial_no_lower!
1007
970
  expect(serial_nos).to eq([1, 2, 3, 4, 5])
@@ -1010,7 +973,7 @@ describe Mongoid::Orderable do
1010
973
  end
1011
974
 
1012
975
  it 'lower from middle' do
1013
- record = MultipleColumnsOrderable.where(:serial_no => 3).first
976
+ record = MultipleColumnsOrderable.where(serial_no: 3).first
1014
977
  position = record.position
1015
978
  record.move_serial_no_lower!
1016
979
  expect(serial_nos).to eq([1, 2, 3, 4, 5])
@@ -1020,39 +983,36 @@ describe Mongoid::Orderable do
1020
983
  end
1021
984
 
1022
985
  describe 'utility methods' do
1023
-
1024
986
  before do
1025
- @record_1 = MultipleColumnsOrderable.where(:serial_no => 1).first
1026
- @record_2 = MultipleColumnsOrderable.where(:serial_no => 2).first
1027
- @record_3 = MultipleColumnsOrderable.where(:serial_no => 3).first
1028
- @record_4 = MultipleColumnsOrderable.where(:serial_no => 4).first
1029
- @record_5 = MultipleColumnsOrderable.where(:serial_no => 5).first
987
+ @record1 = MultipleColumnsOrderable.where(serial_no: 1).first
988
+ @record2 = MultipleColumnsOrderable.where(serial_no: 2).first
989
+ @record3 = MultipleColumnsOrderable.where(serial_no: 3).first
990
+ @record4 = MultipleColumnsOrderable.where(serial_no: 4).first
991
+ @record5 = MultipleColumnsOrderable.where(serial_no: 5).first
1030
992
  end
1031
993
 
1032
- it "should return the lower/higher item on the list for next_item/previous_item" do
1033
- expect(@record_1.next_serial_no_item).to eq(@record_2)
1034
- expect(@record_3.next_serial_no_item).to eq(@record_4)
1035
- expect(@record_5.next_serial_no_item).to eq(nil)
1036
- expect(@record_1.prev_serial_no_item).to eq(nil)
1037
- expect(@record_3.prev_serial_no_item).to eq(@record_2)
1038
- expect(@record_5.prev_serial_no_item).to eq(@record_4)
994
+ it 'should return the lower/higher item on the list for next_item/previous_item' do
995
+ expect(@record1.next_serial_no_item).to eq(@record2)
996
+ expect(@record3.next_serial_no_item).to eq(@record4)
997
+ expect(@record5.next_serial_no_item).to eq(nil)
998
+ expect(@record1.prev_serial_no_item).to eq(nil)
999
+ expect(@record3.prev_serial_no_item).to eq(@record2)
1000
+ expect(@record5.prev_serial_no_item).to eq(@record4)
1039
1001
  end
1040
1002
 
1041
- it "should return a collection of items lower/higher on the list for next_items/previous_items" do
1042
- expect(@record_1.next_serial_no_items.to_a).to eq([@record_2, @record_3, @record_4, @record_5])
1043
- expect(@record_3.next_serial_no_items.to_a).to eq([@record_4, @record_5])
1044
- expect(@record_5.next_serial_no_items.to_a).to eq([])
1045
- expect(@record_1.previous_serial_no_items.to_a).to eq([])
1046
- expect(@record_3.previous_serial_no_items.to_a).to eq([@record_1, @record_2])
1047
- expect(@record_5.previous_serial_no_items.to_a).to eq([@record_1, @record_2, @record_3, @record_4])
1003
+ it 'should return a collection of items lower/higher on the list for next_items/previous_items' do
1004
+ expect(@record1.next_serial_no_items.to_a).to eq([@record2, @record3, @record4, @record5])
1005
+ expect(@record3.next_serial_no_items.to_a).to eq([@record4, @record5])
1006
+ expect(@record5.next_serial_no_items.to_a).to eq([])
1007
+ expect(@record1.previous_serial_no_items.to_a).to eq([])
1008
+ expect(@record3.previous_serial_no_items.to_a).to eq([@record1, @record2])
1009
+ expect(@record5.previous_serial_no_items.to_a).to eq([@record1, @record2, @record3, @record4])
1048
1010
  end
1049
1011
  end
1050
-
1051
1012
  end
1052
1013
 
1053
1014
  context 'position orderable' do
1054
-
1055
- let(:positions){ MultipleColumnsOrderable.all.map(&:position).sort }
1015
+ let(:positions) { MultipleColumnsOrderable.all.map(&:position).sort }
1056
1016
 
1057
1017
  it 'should not have default position field' do
1058
1018
  expect(MultipleColumnsOrderable.fields).not_to have_key('position')
@@ -1064,12 +1024,10 @@ describe Mongoid::Orderable do
1064
1024
  end
1065
1025
 
1066
1026
  it 'should have index on position column' do
1067
- if ::Mongoid::Compatibility::Version.mongoid2?
1068
- expect(MultipleColumnsOrderable.index_options[[[:position, 1]]]).to be_nil
1069
- elsif ::Mongoid::Compatibility::Version.mongoid3?
1070
- expect(MultipleColumnsOrderable.index_options[{:position => 1}]).to be_nil
1027
+ if ::Mongoid::Compatibility::Version.mongoid3?
1028
+ expect(MultipleColumnsOrderable.index_options[{ position: 1 }]).to be_nil
1071
1029
  else
1072
- expect(MultipleColumnsOrderable.index_specifications.detect { |spec| spec.key == {:position => 1} }).to be_nil
1030
+ expect(MultipleColumnsOrderable.index_specifications.detect { |spec| spec.key == { position: 1 } }).to be_nil
1073
1031
  end
1074
1032
  end
1075
1033
 
@@ -1082,25 +1040,24 @@ describe Mongoid::Orderable do
1082
1040
  end
1083
1041
 
1084
1042
  describe 'removement' do
1085
-
1086
1043
  it 'top' do
1087
- MultipleColumnsOrderable.where(:pos => 1).destroy
1044
+ MultipleColumnsOrderable.where(pos: 1).destroy
1088
1045
  expect(positions).to eq([0, 1, 2, 3])
1089
1046
  end
1090
1047
 
1091
1048
  it 'bottom' do
1092
- MultipleColumnsOrderable.where(:pos => 4).destroy
1049
+ MultipleColumnsOrderable.where(pos: 4).destroy
1093
1050
  expect(positions).to eq([0, 1, 2, 3])
1094
1051
  end
1095
1052
 
1096
1053
  it 'middle' do
1097
- MultipleColumnsOrderable.where(:pos => 3).destroy
1054
+ MultipleColumnsOrderable.where(pos: 3).destroy
1098
1055
  expect(positions).to eq([0, 1, 2, 3])
1099
1056
  end
1100
1057
  end
1101
1058
 
1102
1059
  describe 'inserting' do
1103
- let(:newbie){ MultipleColumnsOrderable.create! }
1060
+ let(:newbie) { MultipleColumnsOrderable.create! }
1104
1061
 
1105
1062
  before { @serial_no = newbie.serial_no }
1106
1063
 
@@ -1128,7 +1085,7 @@ describe Mongoid::Orderable do
1128
1085
 
1129
1086
  describe 'movement' do
1130
1087
  it 'higher from top' do
1131
- record = MultipleColumnsOrderable.where(:pos => 0).first
1088
+ record = MultipleColumnsOrderable.where(pos: 0).first
1132
1089
  position = record.serial_no
1133
1090
  record.move_position_higher!
1134
1091
  expect(positions).to eq([0, 1, 2, 3, 4])
@@ -1137,7 +1094,7 @@ describe Mongoid::Orderable do
1137
1094
  end
1138
1095
 
1139
1096
  it 'higher from bottom' do
1140
- record = MultipleColumnsOrderable.where(:pos => 4).first
1097
+ record = MultipleColumnsOrderable.where(pos: 4).first
1141
1098
  position = record.serial_no
1142
1099
  record.move_position_higher!
1143
1100
  expect(positions).to eq([0, 1, 2, 3, 4])
@@ -1146,7 +1103,7 @@ describe Mongoid::Orderable do
1146
1103
  end
1147
1104
 
1148
1105
  it 'higher from middle' do
1149
- record = MultipleColumnsOrderable.where(:pos => 3).first
1106
+ record = MultipleColumnsOrderable.where(pos: 3).first
1150
1107
  position = record.serial_no
1151
1108
  record.move_position_higher!
1152
1109
  expect(positions).to eq([0, 1, 2, 3, 4])
@@ -1155,7 +1112,7 @@ describe Mongoid::Orderable do
1155
1112
  end
1156
1113
 
1157
1114
  it 'lower from top' do
1158
- record = MultipleColumnsOrderable.where(:pos => 0).first
1115
+ record = MultipleColumnsOrderable.where(pos: 0).first
1159
1116
  position = record.serial_no
1160
1117
  record.move_position_lower!
1161
1118
  expect(positions).to eq([0, 1, 2, 3, 4])
@@ -1164,7 +1121,7 @@ describe Mongoid::Orderable do
1164
1121
  end
1165
1122
 
1166
1123
  it 'lower from bottom' do
1167
- record = MultipleColumnsOrderable.where(:pos => 4).first
1124
+ record = MultipleColumnsOrderable.where(pos: 4).first
1168
1125
  position = record.serial_no
1169
1126
  record.move_position_lower!
1170
1127
  expect(positions).to eq([0, 1, 2, 3, 4])
@@ -1173,7 +1130,7 @@ describe Mongoid::Orderable do
1173
1130
  end
1174
1131
 
1175
1132
  it 'lower from middle' do
1176
- record = MultipleColumnsOrderable.where(:pos => 3).first
1133
+ record = MultipleColumnsOrderable.where(pos: 3).first
1177
1134
  position = record.serial_no
1178
1135
  record.move_position_lower!
1179
1136
  expect(positions).to eq([0, 1, 2, 3, 4])
@@ -1183,85 +1140,81 @@ describe Mongoid::Orderable do
1183
1140
  end
1184
1141
 
1185
1142
  describe 'utility methods' do
1186
-
1187
1143
  before do
1188
- @record_1 = MultipleColumnsOrderable.where(:pos => 0).first
1189
- @record_2 = MultipleColumnsOrderable.where(:pos => 1).first
1190
- @record_3 = MultipleColumnsOrderable.where(:pos => 2).first
1191
- @record_4 = MultipleColumnsOrderable.where(:pos => 3).first
1192
- @record_5 = MultipleColumnsOrderable.where(:pos => 4).first
1144
+ @record1 = MultipleColumnsOrderable.where(pos: 0).first
1145
+ @record2 = MultipleColumnsOrderable.where(pos: 1).first
1146
+ @record3 = MultipleColumnsOrderable.where(pos: 2).first
1147
+ @record4 = MultipleColumnsOrderable.where(pos: 3).first
1148
+ @record5 = MultipleColumnsOrderable.where(pos: 4).first
1193
1149
  end
1194
1150
 
1195
- it "should return the lower/higher item on the list for next_item/previous_item" do
1196
- expect(@record_1.next_position_item).to eq(@record_2)
1197
- expect(@record_3.next_position_item).to eq(@record_4)
1198
- expect(@record_5.next_position_item).to eq(nil)
1199
- expect(@record_1.prev_position_item).to eq(nil)
1200
- expect(@record_3.prev_position_item).to eq(@record_2)
1201
- expect(@record_5.prev_position_item).to eq(@record_4)
1151
+ it 'should return the lower/higher item on the list for next_item/previous_item' do
1152
+ expect(@record1.next_position_item).to eq(@record2)
1153
+ expect(@record3.next_position_item).to eq(@record4)
1154
+ expect(@record5.next_position_item).to eq(nil)
1155
+ expect(@record1.prev_position_item).to eq(nil)
1156
+ expect(@record3.prev_position_item).to eq(@record2)
1157
+ expect(@record5.prev_position_item).to eq(@record4)
1202
1158
  end
1203
1159
 
1204
- it "should return a collection of items lower/higher on the list for next_items/previous_items" do
1205
- expect(@record_1.next_position_items.to_a).to eq([@record_2, @record_3, @record_4, @record_5])
1206
- expect(@record_3.next_position_items.to_a).to eq([@record_4, @record_5])
1207
- expect(@record_5.next_position_items.to_a).to eq([])
1208
- expect(@record_1.previous_position_items.to_a).to eq([])
1209
- expect(@record_3.previous_position_items.to_a).to eq([@record_1, @record_2])
1210
- expect(@record_5.previous_position_items.to_a).to eq([@record_1, @record_2, @record_3, @record_4])
1160
+ it 'should return a collection of items lower/higher on the list for next_items/previous_items' do
1161
+ expect(@record1.next_position_items.to_a).to eq([@record2, @record3, @record4, @record5])
1162
+ expect(@record3.next_position_items.to_a).to eq([@record4, @record5])
1163
+ expect(@record5.next_position_items.to_a).to eq([])
1164
+ expect(@record1.previous_position_items.to_a).to eq([])
1165
+ expect(@record3.previous_position_items.to_a).to eq([@record1, @record2])
1166
+ expect(@record5.previous_position_items.to_a).to eq([@record1, @record2, @record3, @record4])
1211
1167
  end
1212
1168
  end
1213
-
1214
1169
  end
1215
1170
 
1216
1171
  context 'group_count orderable' do
1217
1172
  before :each do
1218
1173
  MultipleColumnsOrderable.delete_all
1219
- 2.times { MultipleColumnsOrderable.create! :group_id => 1 }
1220
- 3.times { MultipleColumnsOrderable.create! :group_id => 2 }
1174
+ 2.times { MultipleColumnsOrderable.create! group_id: 1 }
1175
+ 3.times { MultipleColumnsOrderable.create! group_id: 2 }
1221
1176
  end
1222
1177
 
1223
- let(:all_groups){ MultipleColumnsOrderable.order_by([:group_id, :asc], [:groups, :asc]).map(&:groups) }
1178
+ let(:all_groups) { MultipleColumnsOrderable.order_by([:group_id, :asc], [:groups, :asc]).map(&:groups) }
1224
1179
 
1225
1180
  it 'should set proper position while creation' do
1226
1181
  expect(all_groups).to eq([1, 2, 1, 2, 3])
1227
1182
  end
1228
1183
 
1229
1184
  describe 'removement' do
1230
-
1231
1185
  it 'top' do
1232
- MultipleColumnsOrderable.where(:groups => 1, :group_id => 1).destroy
1186
+ MultipleColumnsOrderable.where(groups: 1, group_id: 1).destroy
1233
1187
  expect(all_groups).to eq([1, 1, 2, 3])
1234
1188
  end
1235
1189
 
1236
1190
  it 'bottom' do
1237
- MultipleColumnsOrderable.where(:groups => 3, :group_id => 2).destroy
1191
+ MultipleColumnsOrderable.where(groups: 3, group_id: 2).destroy
1238
1192
  expect(all_groups).to eq([1, 2, 1, 2])
1239
1193
  end
1240
1194
 
1241
1195
  it 'middle' do
1242
- MultipleColumnsOrderable.where(:groups => 2, :group_id => 2).destroy
1196
+ MultipleColumnsOrderable.where(groups: 2, group_id: 2).destroy
1243
1197
  expect(all_groups).to eq([1, 2, 1, 2])
1244
1198
  end
1245
1199
  end
1246
1200
 
1247
1201
  describe 'inserting' do
1248
-
1249
1202
  it 'top' do
1250
- newbie = MultipleColumnsOrderable.create! :group_id => 1
1203
+ newbie = MultipleColumnsOrderable.create! group_id: 1
1251
1204
  newbie.move_groups_to! :top
1252
1205
  expect(all_groups).to eq([1, 2, 3, 1, 2, 3])
1253
1206
  expect(newbie.groups).to eq(1)
1254
1207
  end
1255
1208
 
1256
1209
  it 'bottom' do
1257
- newbie = MultipleColumnsOrderable.create! :group_id => 2
1210
+ newbie = MultipleColumnsOrderable.create! group_id: 2
1258
1211
  newbie.move_groups_to! :bottom
1259
1212
  expect(all_groups).to eq([1, 2, 1, 2, 3, 4])
1260
1213
  expect(newbie.groups).to eq(4)
1261
1214
  end
1262
1215
 
1263
1216
  it 'middle' do
1264
- newbie = MultipleColumnsOrderable.create! :group_id => 2
1217
+ newbie = MultipleColumnsOrderable.create! group_id: 2
1265
1218
  newbie.move_groups_to! 2
1266
1219
  expect(all_groups).to eq([1, 2, 1, 2, 3, 4])
1267
1220
  expect(newbie.groups).to eq(2)
@@ -1269,32 +1222,30 @@ describe Mongoid::Orderable do
1269
1222
  end
1270
1223
 
1271
1224
  describe 'scope movement' do
1272
-
1273
- let(:record){ MultipleColumnsOrderable.where(:group_id => 2, :groups => 2).first }
1225
+ let(:record) { MultipleColumnsOrderable.where(group_id: 2, groups: 2).first }
1274
1226
 
1275
1227
  it 'to a new scope group' do
1276
- record.update_attributes :group_id => 3
1228
+ record.update_attributes group_id: 3
1277
1229
  expect(all_groups).to eq([1, 2, 1, 2, 1])
1278
1230
  expect(record.groups).to eq(1)
1279
1231
  end
1280
1232
 
1281
1233
  context 'when moving to an existing scope group' do
1282
-
1283
1234
  it 'without a position' do
1284
- record.update_attributes :group_id => 1
1235
+ record.update_attributes group_id: 1
1285
1236
  expect(all_groups).to eq([1, 2, 3, 1, 2])
1286
1237
  expect(record.reload.groups).to eq(3)
1287
1238
  end
1288
1239
 
1289
1240
  it 'with symbol position' do
1290
- record.update_attributes :group_id => 1
1241
+ record.update_attributes group_id: 1
1291
1242
  record.move_groups_to! :top
1292
1243
  expect(all_groups).to eq([1, 2, 3, 1, 2])
1293
1244
  expect(record.reload.groups).to eq(1)
1294
1245
  end
1295
1246
 
1296
1247
  it 'with point position' do
1297
- record.update_attributes :group_id => 1
1248
+ record.update_attributes group_id: 1
1298
1249
  record.move_groups_to! 2
1299
1250
  expect(all_groups).to eq([1, 2, 3, 1, 2])
1300
1251
  expect(record.reload.groups).to eq(2)
@@ -1305,8 +1256,7 @@ describe Mongoid::Orderable do
1305
1256
  if defined?(Mongoid::IdentityMap)
1306
1257
 
1307
1258
  context 'when identity map is enabled' do
1308
-
1309
- let(:record){ MultipleColumnsOrderable.where(:group_id => 2, :groups => 2).first }
1259
+ let(:record) { MultipleColumnsOrderable.where(group_id: 2, groups: 2).first }
1310
1260
 
1311
1261
  before do
1312
1262
  Mongoid.identity_map_enabled = true
@@ -1316,13 +1266,13 @@ describe Mongoid::Orderable do
1316
1266
  after { Mongoid.identity_map_enabled = false }
1317
1267
 
1318
1268
  it 'to a new scope group' do
1319
- record.update_attributes :group_id => 3
1269
+ record.update_attributes group_id: 3
1320
1270
  expect(all_groups).to eq([1, 2, 1, 2, 1])
1321
1271
  expect(record.groups).to eq(1)
1322
1272
  end
1323
1273
 
1324
1274
  it 'to an existing scope group' do
1325
- record.update_attributes :group_id => 1
1275
+ record.update_attributes group_id: 1
1326
1276
  record.move_groups_to! 2
1327
1277
  expect(all_groups).to eq([1, 2, 3, 1, 2])
1328
1278
  expect(record.groups).to eq(2)
@@ -1331,31 +1281,30 @@ describe Mongoid::Orderable do
1331
1281
  end
1332
1282
 
1333
1283
  describe 'utility methods' do
1334
-
1335
1284
  before do
1336
- @record_1 = MultipleColumnsOrderable.where(:group_id => 2, :groups => 1).first
1337
- @record_2 = MultipleColumnsOrderable.where(:group_id => 2, :groups => 2).first
1338
- @record_3 = MultipleColumnsOrderable.where(:group_id => 2, :groups => 3).first
1339
- @record_4 = MultipleColumnsOrderable.where(:group_id => 1, :groups => 1).first
1340
- @record_5 = MultipleColumnsOrderable.where(:group_id => 1, :groups => 2).first
1285
+ @record1 = MultipleColumnsOrderable.where(group_id: 2, groups: 1).first
1286
+ @record2 = MultipleColumnsOrderable.where(group_id: 2, groups: 2).first
1287
+ @record3 = MultipleColumnsOrderable.where(group_id: 2, groups: 3).first
1288
+ @record4 = MultipleColumnsOrderable.where(group_id: 1, groups: 1).first
1289
+ @record5 = MultipleColumnsOrderable.where(group_id: 1, groups: 2).first
1341
1290
  end
1342
1291
 
1343
- it "should return the lower/higher item on the list for next_item/previous_item" do
1344
- expect(@record_1.next_groups_item).to eq(@record_2)
1345
- expect(@record_4.next_groups_item).to eq(@record_5)
1346
- expect(@record_3.next_groups_item).to eq(nil)
1347
- expect(@record_1.prev_groups_item).to eq(nil)
1348
- expect(@record_3.prev_groups_item).to eq(@record_2)
1349
- expect(@record_5.prev_groups_item).to eq(@record_4)
1292
+ it 'should return the lower/higher item on the list for next_item/previous_item' do
1293
+ expect(@record1.next_groups_item).to eq(@record2)
1294
+ expect(@record4.next_groups_item).to eq(@record5)
1295
+ expect(@record3.next_groups_item).to eq(nil)
1296
+ expect(@record1.prev_groups_item).to eq(nil)
1297
+ expect(@record3.prev_groups_item).to eq(@record2)
1298
+ expect(@record5.prev_groups_item).to eq(@record4)
1350
1299
  end
1351
1300
 
1352
- it "should return a collection of items lower/higher on the list for next_items/previous_items" do
1353
- expect(@record_1.next_groups_items.to_a).to eq([@record_2, @record_3])
1354
- expect(@record_3.next_groups_items.to_a).to eq([])
1355
- expect(@record_4.next_groups_items.to_a).to eq([@record_5])
1356
- expect(@record_1.previous_groups_items.to_a).to eq([])
1357
- expect(@record_3.previous_groups_items.to_a).to eq([@record_1, @record_2])
1358
- expect(@record_5.previous_groups_items.to_a).to eq([@record_4])
1301
+ it 'should return a collection of items lower/higher on the list for next_items/previous_items' do
1302
+ expect(@record1.next_groups_items.to_a).to eq([@record2, @record3])
1303
+ expect(@record3.next_groups_items.to_a).to eq([])
1304
+ expect(@record4.next_groups_items.to_a).to eq([@record5])
1305
+ expect(@record1.previous_groups_items.to_a).to eq([])
1306
+ expect(@record3.previous_groups_items.to_a).to eq([@record1, @record2])
1307
+ expect(@record5.previous_groups_items.to_a).to eq([@record4])
1359
1308
  end
1360
1309
  end
1361
1310
  end
@@ -1363,22 +1312,22 @@ describe Mongoid::Orderable do
1363
1312
 
1364
1313
  describe MultipleScopedOrderable do
1365
1314
  before :each do
1366
- Apple.delete_all; Orange.delete_all;
1315
+ Apple.delete_all; Orange.delete_all
1367
1316
  MultipleScopedOrderable.delete_all
1368
1317
 
1369
1318
  3.times do
1370
1319
  Apple.create; Orange.create
1371
1320
  end
1372
1321
 
1373
- MultipleScopedOrderable.create! :apple_id => 1, :orange_id => 1
1374
- MultipleScopedOrderable.create! :apple_id => 2, :orange_id => 1
1375
- MultipleScopedOrderable.create! :apple_id => 2, :orange_id => 2
1376
- MultipleScopedOrderable.create! :apple_id => 1, :orange_id => 3
1377
- MultipleScopedOrderable.create! :apple_id => 1, :orange_id => 1
1378
- MultipleScopedOrderable.create! :apple_id => 3, :orange_id => 3
1379
- MultipleScopedOrderable.create! :apple_id => 2, :orange_id => 3
1380
- MultipleScopedOrderable.create! :apple_id => 3, :orange_id => 2
1381
- MultipleScopedOrderable.create! :apple_id => 1, :orange_id => 3
1322
+ MultipleScopedOrderable.create! apple_id: 1, orange_id: 1
1323
+ MultipleScopedOrderable.create! apple_id: 2, orange_id: 1
1324
+ MultipleScopedOrderable.create! apple_id: 2, orange_id: 2
1325
+ MultipleScopedOrderable.create! apple_id: 1, orange_id: 3
1326
+ MultipleScopedOrderable.create! apple_id: 1, orange_id: 1
1327
+ MultipleScopedOrderable.create! apple_id: 3, orange_id: 3
1328
+ MultipleScopedOrderable.create! apple_id: 2, orange_id: 3
1329
+ MultipleScopedOrderable.create! apple_id: 3, orange_id: 2
1330
+ MultipleScopedOrderable.create! apple_id: 1, orange_id: 3
1382
1331
  end
1383
1332
 
1384
1333
  def apple_positions
@@ -1409,5 +1358,4 @@ describe Mongoid::Orderable do
1409
1358
  end
1410
1359
  end
1411
1360
  end
1412
-
1413
1361
  end