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.
- checksums.yaml +15 -0
- data/.travis.yml +9 -1
- data/gemfiles/rails3/Gemfile +8 -0
- data/gemfiles/rails4/Gemfile +8 -0
- data/lib/acts_as_list/active_record/acts/list.rb +60 -27
- data/lib/acts_as_list/version.rb +1 -1
- data/test/shared.rb +1 -0
- data/test/shared_array_scope_list.rb +62 -62
- data/test/shared_list.rb +96 -76
- data/test/shared_list_sub.rb +35 -35
- data/test/shared_no_addition.rb +25 -0
- data/test/shared_top_addition.rb +29 -29
- data/test/shared_zero_based.rb +23 -23
- data/test/test_list.rb +143 -111
- metadata +77 -94
data/test/shared_zero_based.rb
CHANGED
@@ -1,64 +1,64 @@
|
|
1
1
|
module Shared
|
2
2
|
module ZeroBased
|
3
3
|
def setup
|
4
|
-
(1..4).each { |counter| ZeroBasedMixin.create! :
|
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(:
|
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(:
|
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(:
|
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(:
|
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.
|
30
|
+
assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
|
31
31
|
|
32
|
-
ListMixin.
|
33
|
-
assert_equal [1, 3, 2, 4], ZeroBasedMixin.
|
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.
|
36
|
-
assert_equal [1, 2, 3, 4], ZeroBasedMixin.
|
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.
|
39
|
-
assert_equal [2, 3, 4, 1], ZeroBasedMixin.
|
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.
|
42
|
-
assert_equal [1, 2, 3, 4], ZeroBasedMixin.
|
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.
|
45
|
-
assert_equal [1, 3, 4, 2], ZeroBasedMixin.
|
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.
|
48
|
-
assert_equal [4, 1, 3, 2], ZeroBasedMixin.
|
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(:
|
52
|
+
new = ZeroBasedMixin.create(parent_id: 20)
|
53
53
|
assert_equal 0, new.pos
|
54
54
|
|
55
|
-
new = ZeroBasedMixin.create(:
|
55
|
+
new = ZeroBasedMixin.create(parent_id: 20)
|
56
56
|
assert_equal 1, new.pos
|
57
57
|
|
58
|
-
new = ZeroBasedMixin.create(:
|
58
|
+
new = ZeroBasedMixin.create(parent_id: 20)
|
59
59
|
assert_equal 2, new.pos
|
60
60
|
|
61
|
-
new4 = ZeroBasedMixin.create(:
|
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(:
|
76
|
+
new5 = ListMixin.create(parent_id: 20)
|
77
77
|
assert_equal 4, new5.pos
|
78
78
|
|
79
79
|
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(:
|
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(:
|
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, :
|
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 :
|
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 :
|
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, :
|
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 :
|
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 :
|
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 :
|
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 :
|
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 :
|
94
|
-
default_scope { order('pos ASC').where(:
|
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 :
|
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!({:
|
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.
|
192
|
+
assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
|
208
193
|
|
209
|
-
DefaultScopedMixin.
|
210
|
-
assert_equal [1, 3, 2, 4], DefaultScopedMixin.
|
194
|
+
DefaultScopedMixin.where(id: 2).first.move_lower
|
195
|
+
assert_equal [1, 3, 2, 4], DefaultScopedMixin.all.map(&:id)
|
211
196
|
|
212
|
-
DefaultScopedMixin.
|
213
|
-
assert_equal [1, 2, 3, 4], DefaultScopedMixin.
|
197
|
+
DefaultScopedMixin.where(id: 2).first.move_higher
|
198
|
+
assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
|
214
199
|
|
215
|
-
DefaultScopedMixin.
|
216
|
-
assert_equal [2, 3, 4, 1], DefaultScopedMixin.
|
200
|
+
DefaultScopedMixin.where(id: 1).first.move_to_bottom
|
201
|
+
assert_equal [2, 3, 4, 1], DefaultScopedMixin.all.map(&:id)
|
217
202
|
|
218
|
-
DefaultScopedMixin.
|
219
|
-
assert_equal [1, 2, 3, 4], DefaultScopedMixin.
|
203
|
+
DefaultScopedMixin.where(id: 1).first.move_to_top
|
204
|
+
assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
|
220
205
|
|
221
|
-
DefaultScopedMixin.
|
222
|
-
assert_equal [1, 3, 4, 2], DefaultScopedMixin.
|
206
|
+
DefaultScopedMixin.where(id: 2).first.move_to_bottom
|
207
|
+
assert_equal [1, 3, 4, 2], DefaultScopedMixin.all.map(&:id)
|
223
208
|
|
224
|
-
DefaultScopedMixin.
|
225
|
-
assert_equal [4, 1, 3, 2], DefaultScopedMixin.
|
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.
|
265
|
-
DefaultScopedMixin.
|
266
|
-
assert_equal [1, 3, 4, 2], DefaultScopedMixin.
|
267
|
-
DefaultScopedMixin.
|
268
|
-
assert_equal [1, 2, 3, 4], DefaultScopedMixin.
|
269
|
-
DefaultScopedMixin.
|
270
|
-
assert_equal [2, 3, 4, 1], DefaultScopedMixin.
|
271
|
-
DefaultScopedMixin.
|
272
|
-
assert_equal [1, 2, 3, 4], DefaultScopedMixin.
|
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! :
|
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(:
|
285
|
+
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(active: false).map(&:id)
|
302
286
|
|
303
|
-
DefaultScopedWhereMixin.where(:
|
304
|
-
assert_equal [1, 3, 2, 4], DefaultScopedWhereMixin.where(:
|
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(:
|
307
|
-
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:
|
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(:
|
310
|
-
assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.where(:
|
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(:
|
313
|
-
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:
|
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(:
|
316
|
-
assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.where(:
|
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(:
|
319
|
-
assert_equal [4, 1, 3, 2], DefaultScopedWhereMixin.where(:
|
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(:
|
359
|
-
DefaultScopedWhereMixin.where(:
|
360
|
-
assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.where(:
|
361
|
-
DefaultScopedWhereMixin.where(:
|
362
|
-
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:
|
363
|
-
DefaultScopedWhereMixin.where(:
|
364
|
-
assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.where(:
|
365
|
-
DefaultScopedWhereMixin.where(:
|
366
|
-
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:
|
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, :
|
381
|
-
# accepts_nested_attributes_for :todo_items, :
|
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 :
|
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
|
420
|
+
class NoAdditionTest < ActsAsListTestCase
|
421
|
+
include Shared::NoAddition
|
422
|
+
|
438
423
|
def setup
|
439
|
-
|
424
|
+
setup_db
|
440
425
|
super
|
441
426
|
end
|
427
|
+
end
|
442
428
|
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
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
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
assert_equal
|
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
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
assert_equal
|
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
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
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
|