acts_as_list 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,64 +1,64 @@
1
1
  module Shared
2
2
  module ZeroBased
3
3
  def setup
4
- (1..4).each { |counter| ZeroBasedMixin.create! :pos => counter, :parent_id => 5 }
4
+ (1..4).each { |counter| ZeroBasedMixin.create! pos: counter, parent_id: 5 }
5
5
  end
6
6
 
7
7
  def test_insert
8
- new = ZeroBasedMixin.create(:parent_id => 20)
8
+ new = ZeroBasedMixin.create(parent_id: 20)
9
9
  assert_equal 0, new.pos
10
10
  assert new.first?
11
11
  assert new.last?
12
12
 
13
- new = ZeroBasedMixin.create(:parent_id => 20)
13
+ new = ZeroBasedMixin.create(parent_id: 20)
14
14
  assert_equal 1, new.pos
15
15
  assert !new.first?
16
16
  assert new.last?
17
17
 
18
- new = ZeroBasedMixin.create(:parent_id => 20)
18
+ new = ZeroBasedMixin.create(parent_id: 20)
19
19
  assert_equal 2, new.pos
20
20
  assert !new.first?
21
21
  assert new.last?
22
22
 
23
- new = ZeroBasedMixin.create(:parent_id => 0)
23
+ new = ZeroBasedMixin.create(parent_id: 0)
24
24
  assert_equal 0, new.pos
25
25
  assert new.first?
26
26
  assert new.last?
27
27
  end
28
28
 
29
29
  def test_reordering
30
- assert_equal [1, 2, 3, 4], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
30
+ assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
31
31
 
32
- ListMixin.find(2).move_lower
33
- assert_equal [1, 3, 2, 4], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
32
+ ListMixin.where(id: 2).first.move_lower
33
+ assert_equal [1, 3, 2, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
34
34
 
35
- ListMixin.find(2).move_higher
36
- assert_equal [1, 2, 3, 4], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
35
+ ListMixin.where(id: 2).first.move_higher
36
+ assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
37
37
 
38
- ListMixin.find(1).move_to_bottom
39
- assert_equal [2, 3, 4, 1], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
38
+ ListMixin.where(id: 1).first.move_to_bottom
39
+ assert_equal [2, 3, 4, 1], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
40
40
 
41
- ListMixin.find(1).move_to_top
42
- assert_equal [1, 2, 3, 4], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
41
+ ListMixin.where(id: 1).first.move_to_top
42
+ assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
43
43
 
44
- ListMixin.find(2).move_to_bottom
45
- assert_equal [1, 3, 4, 2], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
44
+ ListMixin.where(id: 2).first.move_to_bottom
45
+ assert_equal [1, 3, 4, 2], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
46
46
 
47
- ListMixin.find(4).move_to_top
48
- assert_equal [4, 1, 3, 2], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
47
+ ListMixin.where(id: 4).first.move_to_top
48
+ assert_equal [4, 1, 3, 2], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
49
49
  end
50
50
 
51
51
  def test_insert_at
52
- new = ZeroBasedMixin.create(:parent_id => 20)
52
+ new = ZeroBasedMixin.create(parent_id: 20)
53
53
  assert_equal 0, new.pos
54
54
 
55
- new = ZeroBasedMixin.create(:parent_id => 20)
55
+ new = ZeroBasedMixin.create(parent_id: 20)
56
56
  assert_equal 1, new.pos
57
57
 
58
- new = ZeroBasedMixin.create(:parent_id => 20)
58
+ new = ZeroBasedMixin.create(parent_id: 20)
59
59
  assert_equal 2, new.pos
60
60
 
61
- new4 = ZeroBasedMixin.create(:parent_id => 20)
61
+ new4 = ZeroBasedMixin.create(parent_id: 20)
62
62
  assert_equal 3, new4.pos
63
63
 
64
64
  new4.insert_at(2)
@@ -73,7 +73,7 @@ module Shared
73
73
  new4.reload
74
74
  assert_equal 3, new4.pos
75
75
 
76
- new5 = ListMixin.create(:parent_id => 20)
76
+ new5 = ListMixin.create(parent_id: 20)
77
77
  assert_equal 4, new5.pos
78
78
 
79
79
  new5.insert_at(1)
@@ -1,16 +1,16 @@
1
1
  # NOTE: following now done in helper.rb (better Readability)
2
2
  require 'helper'
3
3
 
4
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
4
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
5
5
  ActiveRecord::Schema.verbose = false
6
6
 
7
7
  def setup_db(position_options = {})
8
8
  # AR caches columns options like defaults etc. Clear them!
9
9
  ActiveRecord::Base.connection.schema_cache.clear!
10
- ActiveRecord::Schema.define(:version => 1) do
10
+ ActiveRecord::Schema.define(version: 1) do
11
11
  create_table :mixins do |t|
12
12
  t.column :pos, :integer, position_options
13
- t.column :active, :boolean, :default => true
13
+ t.column :active, :boolean, default: true
14
14
  t.column :parent_id, :integer
15
15
  t.column :parent_type, :string
16
16
  t.column :created_at, :datetime
@@ -20,10 +20,10 @@ def setup_db(position_options = {})
20
20
  end
21
21
 
22
22
  def setup_db_with_default
23
- setup_db :default => 0
23
+ setup_db default: 0
24
24
  end
25
25
 
26
- # Returns true if ActiveRecord is rails3 version
26
+ # Returns true if ActiveRecord is rails3,4 version
27
27
  def rails_3
28
28
  defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR >= 3
29
29
  end
@@ -36,29 +36,10 @@ end
36
36
 
37
37
  class Mixin < ActiveRecord::Base
38
38
  self.table_name = 'mixins'
39
- attr_accessible :active, :parent_id, :parent_type
40
39
  end
41
40
 
42
- class ProtectedMixin < ActiveRecord::Base
43
- self.table_name = 'mixins'
44
- attr_protected :active
45
- end
46
-
47
- class ProtectedListMixin < ProtectedMixin
48
- acts_as_list :column => "pos"
49
- end
50
-
51
- class UnProtectedMixin < ActiveRecord::Base
52
- self.table_name = 'mixins'
53
- end
54
-
55
- class UnProtectedListMixin < UnProtectedMixin
56
- acts_as_list :column => "pos"
57
- end
58
-
59
-
60
41
  class ListMixin < Mixin
61
- acts_as_list :column => "pos", :scope => :parent
42
+ acts_as_list column: "pos", scope: :parent
62
43
  end
63
44
 
64
45
  class ListMixinSub1 < ListMixin
@@ -66,36 +47,40 @@ end
66
47
 
67
48
  class ListMixinSub2 < ListMixin
68
49
  if rails_3
69
- validates :pos, :presence => true
50
+ validates :pos, presence: true
70
51
  else
71
52
  validates_presence_of :pos
72
53
  end
73
54
  end
74
55
 
75
56
  class ListWithStringScopeMixin < Mixin
76
- acts_as_list :column => "pos", :scope => 'parent_id = #{parent_id}'
57
+ acts_as_list column: "pos", scope: 'parent_id = #{parent_id}'
77
58
  end
78
59
 
79
60
  class ArrayScopeListMixin < Mixin
80
- acts_as_list :column => "pos", :scope => [:parent_id, :parent_type]
61
+ acts_as_list column: "pos", scope: [:parent_id, :parent_type]
81
62
  end
82
63
 
83
64
  class ZeroBasedMixin < Mixin
84
- acts_as_list :column => "pos", :top_of_list => 0, :scope => [:parent_id]
65
+ acts_as_list column: "pos", top_of_list: 0, scope: [:parent_id]
85
66
  end
86
67
 
87
68
  class DefaultScopedMixin < Mixin
88
- acts_as_list :column => "pos"
69
+ acts_as_list column: "pos"
89
70
  default_scope { order('pos ASC') }
90
71
  end
91
72
 
92
73
  class DefaultScopedWhereMixin < Mixin
93
- acts_as_list :column => "pos"
94
- default_scope { order('pos ASC').where(:active => true) }
74
+ acts_as_list column: "pos"
75
+ default_scope { order('pos ASC').where(active: true) }
95
76
  end
96
77
 
97
78
  class TopAdditionMixin < Mixin
98
- acts_as_list :column => "pos", :add_new_at => :top, :scope => :parent_id
79
+ acts_as_list column: "pos", add_new_at: :top, scope: :parent_id
80
+ end
81
+
82
+ class NoAdditionMixin < Mixin
83
+ acts_as_list column: "pos", add_new_at: nil, scope: :parent_id
99
84
  end
100
85
 
101
86
  class ActsAsListTestCase < Test::Unit::TestCase
@@ -183,7 +168,7 @@ end
183
168
  class DefaultScopedTest < ActsAsListTestCase
184
169
  def setup
185
170
  setup_db
186
- (1..4).each { |counter| DefaultScopedMixin.create!({:pos => counter}) }
171
+ (1..4).each { |counter| DefaultScopedMixin.create!({pos: counter}) }
187
172
  end
188
173
 
189
174
  def test_insert
@@ -204,25 +189,25 @@ class DefaultScopedTest < ActsAsListTestCase
204
189
  end
205
190
 
206
191
  def test_reordering
207
- assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
192
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
208
193
 
209
- DefaultScopedMixin.find(2).move_lower
210
- assert_equal [1, 3, 2, 4], DefaultScopedMixin.find(:all).map(&:id)
194
+ DefaultScopedMixin.where(id: 2).first.move_lower
195
+ assert_equal [1, 3, 2, 4], DefaultScopedMixin.all.map(&:id)
211
196
 
212
- DefaultScopedMixin.find(2).move_higher
213
- assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
197
+ DefaultScopedMixin.where(id: 2).first.move_higher
198
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
214
199
 
215
- DefaultScopedMixin.find(1).move_to_bottom
216
- assert_equal [2, 3, 4, 1], DefaultScopedMixin.find(:all).map(&:id)
200
+ DefaultScopedMixin.where(id: 1).first.move_to_bottom
201
+ assert_equal [2, 3, 4, 1], DefaultScopedMixin.all.map(&:id)
217
202
 
218
- DefaultScopedMixin.find(1).move_to_top
219
- assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
203
+ DefaultScopedMixin.where(id: 1).first.move_to_top
204
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
220
205
 
221
- DefaultScopedMixin.find(2).move_to_bottom
222
- assert_equal [1, 3, 4, 2], DefaultScopedMixin.find(:all).map(&:id)
206
+ DefaultScopedMixin.where(id: 2).first.move_to_bottom
207
+ assert_equal [1, 3, 4, 2], DefaultScopedMixin.all.map(&:id)
223
208
 
224
- DefaultScopedMixin.find(4).move_to_top
225
- assert_equal [4, 1, 3, 2], DefaultScopedMixin.find(:all).map(&:id)
209
+ DefaultScopedMixin.where(id: 4).first.move_to_top
210
+ assert_equal [4, 1, 3, 2], DefaultScopedMixin.all.map(&:id)
226
211
  end
227
212
 
228
213
  def test_insert_at
@@ -261,23 +246,22 @@ class DefaultScopedTest < ActsAsListTestCase
261
246
  end
262
247
 
263
248
  def test_update_position
264
- assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
265
- DefaultScopedMixin.find(2).set_list_position(4)
266
- assert_equal [1, 3, 4, 2], DefaultScopedMixin.find(:all).map(&:id)
267
- DefaultScopedMixin.find(2).set_list_position(2)
268
- assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
269
- DefaultScopedMixin.find(1).set_list_position(4)
270
- assert_equal [2, 3, 4, 1], DefaultScopedMixin.find(:all).map(&:id)
271
- DefaultScopedMixin.find(1).set_list_position(1)
272
- assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
249
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
250
+ DefaultScopedMixin.where(id: 2).first.set_list_position(4)
251
+ assert_equal [1, 3, 4, 2], DefaultScopedMixin.all.map(&:id)
252
+ DefaultScopedMixin.where(id: 2).first.set_list_position(2)
253
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
254
+ DefaultScopedMixin.where(id: 1).first.set_list_position(4)
255
+ assert_equal [2, 3, 4, 1], DefaultScopedMixin.all.map(&:id)
256
+ DefaultScopedMixin.where(id: 1).first.set_list_position(1)
257
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
273
258
  end
274
-
275
259
  end
276
260
 
277
261
  class DefaultScopedWhereTest < ActsAsListTestCase
278
262
  def setup
279
263
  setup_db
280
- (1..4).each { |counter| DefaultScopedWhereMixin.create! :pos => counter, :active => false }
264
+ (1..4).each { |counter| DefaultScopedWhereMixin.create! pos: counter, active: false }
281
265
  end
282
266
 
283
267
  def test_insert
@@ -298,25 +282,25 @@ class DefaultScopedWhereTest < ActsAsListTestCase
298
282
  end
299
283
 
300
284
  def test_reordering
301
- assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).map(&:id)
285
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(active: false).map(&:id)
302
286
 
303
- DefaultScopedWhereMixin.where(:active => false).find(2).move_lower
304
- assert_equal [1, 3, 2, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
287
+ DefaultScopedWhereMixin.where(active: false).where(id: 2).first.move_lower
288
+ assert_equal [1, 3, 2, 4], DefaultScopedWhereMixin.where(active: false).map(&:id)
305
289
 
306
- DefaultScopedWhereMixin.where(:active => false).find(2).move_higher
307
- assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
290
+ DefaultScopedWhereMixin.where(active: false).where(id: 2).first.move_higher
291
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(active: false).map(&:id)
308
292
 
309
- DefaultScopedWhereMixin.where(:active => false).find(1).move_to_bottom
310
- assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
293
+ DefaultScopedWhereMixin.where(active: false).where(id: 1).first.move_to_bottom
294
+ assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.where(active: false).map(&:id)
311
295
 
312
- DefaultScopedWhereMixin.where(:active => false).find(1).move_to_top
313
- assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
296
+ DefaultScopedWhereMixin.where(active: false).where(id: 1).first.move_to_top
297
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(active: false).map(&:id)
314
298
 
315
- DefaultScopedWhereMixin.where(:active => false).find(2).move_to_bottom
316
- assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
299
+ DefaultScopedWhereMixin.where(active: false).where(id: 2).first.move_to_bottom
300
+ assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.where(active: false).map(&:id)
317
301
 
318
- DefaultScopedWhereMixin.where(:active => false).find(4).move_to_top
319
- assert_equal [4, 1, 3, 2], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
302
+ DefaultScopedWhereMixin.where(active: false).where(id: 4).first.move_to_top
303
+ assert_equal [4, 1, 3, 2], DefaultScopedWhereMixin.where(active: false).map(&:id)
320
304
  end
321
305
 
322
306
  def test_insert_at
@@ -355,15 +339,15 @@ class DefaultScopedWhereTest < ActsAsListTestCase
355
339
  end
356
340
 
357
341
  def test_update_position
358
- assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
359
- DefaultScopedWhereMixin.where(:active => false).find(2).set_list_position(4)
360
- assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
361
- DefaultScopedWhereMixin.where(:active => false).find(2).set_list_position(2)
362
- assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
363
- DefaultScopedWhereMixin.where(:active => false).find(1).set_list_position(4)
364
- assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
365
- DefaultScopedWhereMixin.where(:active => false).find(1).set_list_position(1)
366
- assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
342
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(active: false).map(&:id)
343
+ DefaultScopedWhereMixin.where(active: false).where(id: 2).first.set_list_position(4)
344
+ assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.where(active: false).map(&:id)
345
+ DefaultScopedWhereMixin.where(active: false).where(id: 2).first.set_list_position(2)
346
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(active: false).map(&:id)
347
+ DefaultScopedWhereMixin.where(active: false).where(id: 1).first.set_list_position(4)
348
+ assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.where(active: false).map(&:id)
349
+ DefaultScopedWhereMixin.where(active: false).where(id: 1).first.set_list_position(1)
350
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(active: false).map(&:id)
367
351
  end
368
352
 
369
353
  end
@@ -377,13 +361,13 @@ class MultiDestroyTest < ActsAsListTestCase
377
361
  # example:
378
362
  #
379
363
  # class TodoList < ActiveRecord::Base
380
- # has_many :todo_items, :order => "position"
381
- # accepts_nested_attributes_for :todo_items, :allow_destroy => true
364
+ # has_many :todo_items, order: "position"
365
+ # accepts_nested_attributes_for :todo_items, allow_destroy: true
382
366
  # end
383
367
  #
384
368
  # class TodoItem < ActiveRecord::Base
385
369
  # belongs_to :todo_list
386
- # acts_as_list :scope => :todo_list
370
+ # acts_as_list scope: :todo_list
387
371
  # end
388
372
  #
389
373
  # Assume that there are three items.
@@ -424,7 +408,6 @@ class TopAdditionTest < ActsAsListTestCase
424
408
  end
425
409
  end
426
410
 
427
-
428
411
  class TopAdditionTestWithDefault < ActsAsListTestCase
429
412
  include Shared::TopAddition
430
413
 
@@ -434,43 +417,92 @@ class TopAdditionTestWithDefault < ActsAsListTestCase
434
417
  end
435
418
  end
436
419
 
437
- class RespectMixinProtection < ActsAsListTestCase
420
+ class NoAdditionTest < ActsAsListTestCase
421
+ include Shared::NoAddition
422
+
438
423
  def setup
439
- setup_db_with_default
424
+ setup_db
440
425
  super
441
426
  end
427
+ end
442
428
 
443
- # if an attribute is set attr_protected
444
- # it should be unchanged by update_attributes
445
- def test_unmodified_protection
446
- a = ProtectedMixin.new
447
- a.update_attributes({:active => false})
448
- assert_equal true, a.active
429
+ class MultipleListsTest < ActsAsListTestCase
430
+ def setup
431
+ setup_db
432
+ (1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 1}
433
+ (1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 2}
449
434
  end
450
435
 
451
- # even after the acts_as_list mixin is joined
452
- # that protection should continue to exist
453
- def test_still_protected
454
- b = ProtectedListMixin.new
455
- b.update_attributes({:active => false})
456
- assert_equal true, b.active
436
+ def test_check_scope_order
437
+ assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 1).order(:pos).map(&:id)
438
+ assert_equal [5, 6, 7, 8], ListMixin.where(:parent_id => 2).order(:pos).map(&:id)
439
+ ListMixin.find(4).update_attributes(:parent_id => 2, :pos => 2)
440
+ assert_equal [1, 2, 3], ListMixin.where(:parent_id => 1).order(:pos).map(&:id)
441
+ assert_equal [5, 4, 6, 7, 8], ListMixin.where(:parent_id => 2).order(:pos).map(&:id)
457
442
  end
458
443
 
459
- # similarly, if a class lacks mass_assignment protection
460
- # it should be able to be changed
461
- def test_unprotected
462
- a = UnProtectedMixin.new
463
- a.update_attributes({:active => false})
464
- assert_equal false, a.active
444
+ def test_check_scope_position
445
+ assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 1).map(&:pos)
446
+ assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 2).map(&:pos)
447
+ ListMixin.find(4).update_attributes(:parent_id => 2, :pos => 2)
448
+ assert_equal [1, 2, 3], ListMixin.where(:parent_id => 1).order(:pos).map(&:pos)
449
+ assert_equal [1, 2, 3, 4, 5], ListMixin.where(:parent_id => 2).order(:pos).map(&:pos)
465
450
  end
451
+ end
466
452
 
467
- # and it should continue to be mutable by mass_assignment
468
- # even after the acts_as_list plugin has been joined
469
- def test_still_unprotected_mixin
470
- b = UnProtectedListMixin.new
471
- b.assign_attributes({:active => false})
472
- # p UnProtectedListMixin.accessible_attributes.length
473
- assert_equal false, b.active
453
+ class MultipleListsArrayScopeTest < ActsAsListTestCase
454
+ def setup
455
+ setup_db
456
+ (1..4).each { |counter| ArrayScopeListMixin.create! :pos => counter,:parent_id => 1, :parent_type => 'anything'}
457
+ (1..4).each { |counter| ArrayScopeListMixin.create! :pos => counter,:parent_id => 2, :parent_type => 'something'}
458
+ (1..4).each { |counter| ArrayScopeListMixin.create! :pos => counter,:parent_id => 3, :parent_type => 'anything'}
459
+ end
460
+
461
+ def test_order_after_all_scope_properties_are_changed
462
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:id)
463
+ assert_equal [5, 6, 7, 8], ArrayScopeListMixin.where(:parent_id => 2, :parent_type => 'something').order(:pos).map(&:id)
464
+ ArrayScopeListMixin.find(2).update_attributes(:parent_id => 2, :pos => 2,:parent_type => 'something')
465
+ assert_equal [1, 3, 4], ArrayScopeListMixin.where(:parent_id => 1,:parent_type => 'anything').order(:pos).map(&:id)
466
+ assert_equal [5, 2, 6, 7, 8], ArrayScopeListMixin.where(:parent_id => 2,:parent_type => 'something').order(:pos).map(&:id)
474
467
  end
475
468
 
469
+ def test_position_after_all_scope_properties_are_changed
470
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').map(&:pos)
471
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 2, :parent_type => 'something').map(&:pos)
472
+ ArrayScopeListMixin.find(4).update_attributes(:parent_id => 2, :pos => 2, :parent_type => 'something')
473
+ assert_equal [1, 2, 3], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:pos)
474
+ assert_equal [1, 2, 3, 4, 5], ArrayScopeListMixin.where(:parent_id => 2, :parent_type => 'something').order(:pos).map(&:pos)
475
+ end
476
+
477
+ def test_order_after_one_scope_property_is_changed
478
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:id)
479
+ assert_equal [9, 10, 11, 12], ArrayScopeListMixin.where(:parent_id => 3, :parent_type => 'anything').order(:pos).map(&:id)
480
+ ArrayScopeListMixin.find(2).update_attributes(:parent_id => 3, :pos => 2)
481
+ assert_equal [1, 3, 4], ArrayScopeListMixin.where(:parent_id => 1,:parent_type => 'anything').order(:pos).map(&:id)
482
+ assert_equal [9, 2, 10, 11, 12], ArrayScopeListMixin.where(:parent_id => 3,:parent_type => 'anything').order(:pos).map(&:id)
483
+ end
484
+
485
+ def test_position_after_one_scope_property_is_changed
486
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').map(&:pos)
487
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 3, :parent_type => 'anything').map(&:pos)
488
+ ArrayScopeListMixin.find(4).update_attributes(:parent_id => 3, :pos => 2)
489
+ assert_equal [1, 2, 3], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:pos)
490
+ assert_equal [1, 2, 3, 4, 5], ArrayScopeListMixin.where(:parent_id => 3, :parent_type => 'anything').order(:pos).map(&:pos)
491
+ end
492
+
493
+ def test_order_after_moving_to_empty_list
494
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:id)
495
+ assert_equal [], ArrayScopeListMixin.where(:parent_id => 4, :parent_type => 'anything').order(:pos).map(&:id)
496
+ ArrayScopeListMixin.find(2).update_attributes(:parent_id => 4, :pos => 1)
497
+ assert_equal [1, 3, 4], ArrayScopeListMixin.where(:parent_id => 1,:parent_type => 'anything').order(:pos).map(&:id)
498
+ assert_equal [2], ArrayScopeListMixin.where(:parent_id => 4,:parent_type => 'anything').order(:pos).map(&:id)
499
+ end
500
+
501
+ def test_position_after_moving_to_empty_list
502
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').map(&:pos)
503
+ assert_equal [], ArrayScopeListMixin.where(:parent_id => 4, :parent_type => 'anything').map(&:pos)
504
+ ArrayScopeListMixin.find(2).update_attributes(:parent_id => 4, :pos => 1)
505
+ assert_equal [1, 2, 3], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:pos)
506
+ assert_equal [1], ArrayScopeListMixin.where(:parent_id => 4, :parent_type => 'anything').order(:pos).map(&:pos)
507
+ end
476
508
  end