acts_as_list 0.2.0 → 0.4.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,64 +1,69 @@
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
+ assert_equal 0, new.pos
25
+ assert new.first?
26
+ assert new.last?
27
+
28
+ new = ZeroBasedMixin.create(parent_id: 1, pos: -500)
24
29
  assert_equal 0, new.pos
25
30
  assert new.first?
26
31
  assert new.last?
27
32
  end
28
33
 
29
34
  def test_reordering
30
- assert_equal [1, 2, 3, 4], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
35
+ assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
31
36
 
32
- ListMixin.find(2).move_lower
33
- assert_equal [1, 3, 2, 4], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
37
+ ListMixin.where(id: 2).first.move_lower
38
+ assert_equal [1, 3, 2, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
34
39
 
35
- ListMixin.find(2).move_higher
36
- assert_equal [1, 2, 3, 4], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
40
+ ListMixin.where(id: 2).first.move_higher
41
+ assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
37
42
 
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)
43
+ ListMixin.where(id: 1).first.move_to_bottom
44
+ assert_equal [2, 3, 4, 1], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
40
45
 
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)
46
+ ListMixin.where(id: 1).first.move_to_top
47
+ assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
43
48
 
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)
49
+ ListMixin.where(id: 2).first.move_to_bottom
50
+ assert_equal [1, 3, 4, 2], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
46
51
 
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)
52
+ ListMixin.where(id: 4).first.move_to_top
53
+ assert_equal [4, 1, 3, 2], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
49
54
  end
50
55
 
51
56
  def test_insert_at
52
- new = ZeroBasedMixin.create(:parent_id => 20)
57
+ new = ZeroBasedMixin.create(parent_id: 20)
53
58
  assert_equal 0, new.pos
54
59
 
55
- new = ZeroBasedMixin.create(:parent_id => 20)
60
+ new = ZeroBasedMixin.create(parent_id: 20)
56
61
  assert_equal 1, new.pos
57
62
 
58
- new = ZeroBasedMixin.create(:parent_id => 20)
63
+ new = ZeroBasedMixin.create(parent_id: 20)
59
64
  assert_equal 2, new.pos
60
65
 
61
- new4 = ZeroBasedMixin.create(:parent_id => 20)
66
+ new4 = ZeroBasedMixin.create(parent_id: 20)
62
67
  assert_equal 3, new4.pos
63
68
 
64
69
  new4.insert_at(2)
@@ -73,7 +78,7 @@ module Shared
73
78
  new4.reload
74
79
  assert_equal 3, new4.pos
75
80
 
76
- new5 = ListMixin.create(:parent_id => 20)
81
+ new5 = ListMixin.create(parent_id: 20)
77
82
  assert_equal 4, new5.pos
78
83
 
79
84
  new5.insert_at(1)
data/test/test_list.rb CHANGED
@@ -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,39 +47,47 @@ 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) }
76
+
77
+ def self.for_active_false_tests
78
+ unscoped.order('pos ASC').where(active: false)
79
+ end
95
80
  end
96
81
 
97
82
  class TopAdditionMixin < Mixin
98
- acts_as_list :column => "pos", :add_new_at => :top, :scope => :parent_id
83
+ acts_as_list column: "pos", add_new_at: :top, scope: :parent_id
84
+ end
85
+
86
+ class NoAdditionMixin < Mixin
87
+ acts_as_list column: "pos", add_new_at: nil, scope: :parent_id
99
88
  end
100
89
 
101
- class ActsAsListTestCase < Test::Unit::TestCase
90
+ class ActsAsListTestCase < MiniTest::Unit::TestCase
102
91
  # No default test required a this class is abstract.
103
92
  # Need for test/unit.
104
93
  undef_method :default_test if method_defined?(:default_test)
@@ -183,7 +172,7 @@ end
183
172
  class DefaultScopedTest < ActsAsListTestCase
184
173
  def setup
185
174
  setup_db
186
- (1..4).each { |counter| DefaultScopedMixin.create!({:pos => counter}) }
175
+ (1..4).each { |counter| DefaultScopedMixin.create!({pos: counter}) }
187
176
  end
188
177
 
189
178
  def test_insert
@@ -204,25 +193,25 @@ class DefaultScopedTest < ActsAsListTestCase
204
193
  end
205
194
 
206
195
  def test_reordering
207
- assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
196
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
208
197
 
209
- DefaultScopedMixin.find(2).move_lower
210
- assert_equal [1, 3, 2, 4], DefaultScopedMixin.find(:all).map(&:id)
198
+ DefaultScopedMixin.where(id: 2).first.move_lower
199
+ assert_equal [1, 3, 2, 4], DefaultScopedMixin.all.map(&:id)
211
200
 
212
- DefaultScopedMixin.find(2).move_higher
213
- assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
201
+ DefaultScopedMixin.where(id: 2).first.move_higher
202
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
214
203
 
215
- DefaultScopedMixin.find(1).move_to_bottom
216
- assert_equal [2, 3, 4, 1], DefaultScopedMixin.find(:all).map(&:id)
204
+ DefaultScopedMixin.where(id: 1).first.move_to_bottom
205
+ assert_equal [2, 3, 4, 1], DefaultScopedMixin.all.map(&:id)
217
206
 
218
- DefaultScopedMixin.find(1).move_to_top
219
- assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
207
+ DefaultScopedMixin.where(id: 1).first.move_to_top
208
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
220
209
 
221
- DefaultScopedMixin.find(2).move_to_bottom
222
- assert_equal [1, 3, 4, 2], DefaultScopedMixin.find(:all).map(&:id)
210
+ DefaultScopedMixin.where(id: 2).first.move_to_bottom
211
+ assert_equal [1, 3, 4, 2], DefaultScopedMixin.all.map(&:id)
223
212
 
224
- DefaultScopedMixin.find(4).move_to_top
225
- assert_equal [4, 1, 3, 2], DefaultScopedMixin.find(:all).map(&:id)
213
+ DefaultScopedMixin.where(id: 4).first.move_to_top
214
+ assert_equal [4, 1, 3, 2], DefaultScopedMixin.all.map(&:id)
226
215
  end
227
216
 
228
217
  def test_insert_at
@@ -261,23 +250,22 @@ class DefaultScopedTest < ActsAsListTestCase
261
250
  end
262
251
 
263
252
  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)
253
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
254
+ DefaultScopedMixin.where(id: 2).first.set_list_position(4)
255
+ assert_equal [1, 3, 4, 2], DefaultScopedMixin.all.map(&:id)
256
+ DefaultScopedMixin.where(id: 2).first.set_list_position(2)
257
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
258
+ DefaultScopedMixin.where(id: 1).first.set_list_position(4)
259
+ assert_equal [2, 3, 4, 1], DefaultScopedMixin.all.map(&:id)
260
+ DefaultScopedMixin.where(id: 1).first.set_list_position(1)
261
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
273
262
  end
274
-
275
263
  end
276
264
 
277
265
  class DefaultScopedWhereTest < ActsAsListTestCase
278
266
  def setup
279
267
  setup_db
280
- (1..4).each { |counter| DefaultScopedWhereMixin.create! :pos => counter, :active => false }
268
+ (1..4).each { |counter| DefaultScopedWhereMixin.create! pos: counter, active: false }
281
269
  end
282
270
 
283
271
  def test_insert
@@ -298,25 +286,25 @@ class DefaultScopedWhereTest < ActsAsListTestCase
298
286
  end
299
287
 
300
288
  def test_reordering
301
- assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).map(&:id)
289
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
302
290
 
303
- DefaultScopedWhereMixin.where(:active => false).find(2).move_lower
304
- assert_equal [1, 3, 2, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
291
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 2).first.move_lower
292
+ assert_equal [1, 3, 2, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
305
293
 
306
- DefaultScopedWhereMixin.where(:active => false).find(2).move_higher
307
- assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
294
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 2).first.move_higher
295
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
308
296
 
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)
297
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 1).first.move_to_bottom
298
+ assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
311
299
 
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)
300
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 1).first.move_to_top
301
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
314
302
 
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)
303
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 2).first.move_to_bottom
304
+ assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
317
305
 
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)
306
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 4).first.move_to_top
307
+ assert_equal [4, 1, 3, 2], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
320
308
  end
321
309
 
322
310
  def test_insert_at
@@ -355,15 +343,15 @@ class DefaultScopedWhereTest < ActsAsListTestCase
355
343
  end
356
344
 
357
345
  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)
346
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
347
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 2).first.set_list_position(4)
348
+ assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
349
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 2).first.set_list_position(2)
350
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
351
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 1).first.set_list_position(4)
352
+ assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
353
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 1).first.set_list_position(1)
354
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
367
355
  end
368
356
 
369
357
  end
@@ -377,13 +365,13 @@ class MultiDestroyTest < ActsAsListTestCase
377
365
  # example:
378
366
  #
379
367
  # class TodoList < ActiveRecord::Base
380
- # has_many :todo_items, :order => "position"
381
- # accepts_nested_attributes_for :todo_items, :allow_destroy => true
368
+ # has_many :todo_items, order: "position"
369
+ # accepts_nested_attributes_for :todo_items, allow_destroy: true
382
370
  # end
383
371
  #
384
372
  # class TodoItem < ActiveRecord::Base
385
373
  # belongs_to :todo_list
386
- # acts_as_list :scope => :todo_list
374
+ # acts_as_list scope: :todo_list
387
375
  # end
388
376
  #
389
377
  # Assume that there are three items.
@@ -424,7 +412,6 @@ class TopAdditionTest < ActsAsListTestCase
424
412
  end
425
413
  end
426
414
 
427
-
428
415
  class TopAdditionTestWithDefault < ActsAsListTestCase
429
416
  include Shared::TopAddition
430
417
 
@@ -434,43 +421,92 @@ class TopAdditionTestWithDefault < ActsAsListTestCase
434
421
  end
435
422
  end
436
423
 
437
- class RespectMixinProtection < ActsAsListTestCase
424
+ class NoAdditionTest < ActsAsListTestCase
425
+ include Shared::NoAddition
426
+
438
427
  def setup
439
- setup_db_with_default
428
+ setup_db
440
429
  super
441
430
  end
431
+ end
432
+
433
+ class MultipleListsTest < ActsAsListTestCase
434
+ def setup
435
+ setup_db
436
+ (1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 1}
437
+ (1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 2}
438
+ end
439
+
440
+ def test_check_scope_order
441
+ assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 1).order(:pos).map(&:id)
442
+ assert_equal [5, 6, 7, 8], ListMixin.where(:parent_id => 2).order(:pos).map(&:id)
443
+ ListMixin.find(4).update_attributes(:parent_id => 2, :pos => 2)
444
+ assert_equal [1, 2, 3], ListMixin.where(:parent_id => 1).order(:pos).map(&:id)
445
+ assert_equal [5, 4, 6, 7, 8], ListMixin.where(:parent_id => 2).order(:pos).map(&:id)
446
+ end
447
+
448
+ def test_check_scope_position
449
+ assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 1).map(&:pos)
450
+ assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 2).map(&:pos)
451
+ ListMixin.find(4).update_attributes(:parent_id => 2, :pos => 2)
452
+ assert_equal [1, 2, 3], ListMixin.where(:parent_id => 1).order(:pos).map(&:pos)
453
+ assert_equal [1, 2, 3, 4, 5], ListMixin.where(:parent_id => 2).order(:pos).map(&:pos)
454
+ end
455
+ end
442
456
 
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
457
+ class MultipleListsArrayScopeTest < ActsAsListTestCase
458
+ def setup
459
+ setup_db
460
+ (1..4).each { |counter| ArrayScopeListMixin.create! :pos => counter,:parent_id => 1, :parent_type => 'anything'}
461
+ (1..4).each { |counter| ArrayScopeListMixin.create! :pos => counter,:parent_id => 2, :parent_type => 'something'}
462
+ (1..4).each { |counter| ArrayScopeListMixin.create! :pos => counter,:parent_id => 3, :parent_type => 'anything'}
449
463
  end
450
464
 
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
465
+ def test_order_after_all_scope_properties_are_changed
466
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:id)
467
+ assert_equal [5, 6, 7, 8], ArrayScopeListMixin.where(:parent_id => 2, :parent_type => 'something').order(:pos).map(&:id)
468
+ ArrayScopeListMixin.find(2).update_attributes(:parent_id => 2, :pos => 2,:parent_type => 'something')
469
+ assert_equal [1, 3, 4], ArrayScopeListMixin.where(:parent_id => 1,:parent_type => 'anything').order(:pos).map(&:id)
470
+ assert_equal [5, 2, 6, 7, 8], ArrayScopeListMixin.where(:parent_id => 2,:parent_type => 'something').order(:pos).map(&:id)
457
471
  end
458
472
 
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
473
+ def test_position_after_all_scope_properties_are_changed
474
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').map(&:pos)
475
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 2, :parent_type => 'something').map(&:pos)
476
+ ArrayScopeListMixin.find(4).update_attributes(:parent_id => 2, :pos => 2, :parent_type => 'something')
477
+ assert_equal [1, 2, 3], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:pos)
478
+ assert_equal [1, 2, 3, 4, 5], ArrayScopeListMixin.where(:parent_id => 2, :parent_type => 'something').order(:pos).map(&:pos)
465
479
  end
466
480
 
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
481
+ def test_order_after_one_scope_property_is_changed
482
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:id)
483
+ assert_equal [9, 10, 11, 12], ArrayScopeListMixin.where(:parent_id => 3, :parent_type => 'anything').order(:pos).map(&:id)
484
+ ArrayScopeListMixin.find(2).update_attributes(:parent_id => 3, :pos => 2)
485
+ assert_equal [1, 3, 4], ArrayScopeListMixin.where(:parent_id => 1,:parent_type => 'anything').order(:pos).map(&:id)
486
+ assert_equal [9, 2, 10, 11, 12], ArrayScopeListMixin.where(:parent_id => 3,:parent_type => 'anything').order(:pos).map(&:id)
474
487
  end
475
488
 
489
+ def test_position_after_one_scope_property_is_changed
490
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').map(&:pos)
491
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 3, :parent_type => 'anything').map(&:pos)
492
+ ArrayScopeListMixin.find(4).update_attributes(:parent_id => 3, :pos => 2)
493
+ assert_equal [1, 2, 3], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:pos)
494
+ assert_equal [1, 2, 3, 4, 5], ArrayScopeListMixin.where(:parent_id => 3, :parent_type => 'anything').order(:pos).map(&:pos)
495
+ end
496
+
497
+ def test_order_after_moving_to_empty_list
498
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:id)
499
+ assert_equal [], ArrayScopeListMixin.where(:parent_id => 4, :parent_type => 'anything').order(:pos).map(&:id)
500
+ ArrayScopeListMixin.find(2).update_attributes(:parent_id => 4, :pos => 1)
501
+ assert_equal [1, 3, 4], ArrayScopeListMixin.where(:parent_id => 1,:parent_type => 'anything').order(:pos).map(&:id)
502
+ assert_equal [2], ArrayScopeListMixin.where(:parent_id => 4,:parent_type => 'anything').order(:pos).map(&:id)
503
+ end
504
+
505
+ def test_position_after_moving_to_empty_list
506
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').map(&:pos)
507
+ assert_equal [], ArrayScopeListMixin.where(:parent_id => 4, :parent_type => 'anything').map(&:pos)
508
+ ArrayScopeListMixin.find(2).update_attributes(:parent_id => 4, :pos => 1)
509
+ assert_equal [1, 2, 3], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:pos)
510
+ assert_equal [1], ArrayScopeListMixin.where(:parent_id => 4, :parent_type => 'anything').order(:pos).map(&:pos)
511
+ end
476
512
  end