suhovius-acts_as_list 0.7.2.s

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ module Shared
2
+ module NoAddition
3
+ def setup
4
+ (1..4).each { |counter| NoAdditionMixin.create! pos: counter, parent_id: 5 }
5
+ end
6
+
7
+ def test_insert
8
+ new = NoAdditionMixin.create(parent_id: 20)
9
+ assert_equal nil, new.pos
10
+ assert !new.in_list?
11
+
12
+ new = NoAdditionMixin.create(parent_id: 20)
13
+ assert_equal nil, new.pos
14
+ end
15
+
16
+ def test_update_does_not_add_to_list
17
+ new = NoAdditionMixin.create(parent_id: 20)
18
+ new.update_attribute(:updated_at, Time.now) # force some change
19
+ new.reload
20
+
21
+ assert !new.in_list?
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,87 @@
1
+ module Shared
2
+ module TopAddition
3
+ def setup
4
+ (1..4).each { |counter| TopAdditionMixin.create! pos: counter, parent_id: 5 }
5
+ end
6
+
7
+ def test_reordering
8
+ assert_equal [4, 3, 2, 1], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
9
+
10
+ TopAdditionMixin.where(id: 2).first.move_lower
11
+ assert_equal [4, 3, 1, 2], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
12
+
13
+ TopAdditionMixin.where(id: 2).first.move_higher
14
+ assert_equal [4, 3, 2, 1], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
15
+
16
+ TopAdditionMixin.where(id: 1).first.move_to_bottom
17
+ assert_equal [4, 3, 2, 1], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
18
+
19
+ TopAdditionMixin.where(id: 1).first.move_to_top
20
+ assert_equal [1, 4, 3, 2], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
21
+
22
+ TopAdditionMixin.where(id: 2).first.move_to_bottom
23
+ assert_equal [1, 4, 3, 2], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
24
+
25
+ TopAdditionMixin.where(id: 4).first.move_to_top
26
+ assert_equal [4, 1, 3, 2], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
27
+ end
28
+
29
+ def test_injection
30
+ item = TopAdditionMixin.new(parent_id: 1)
31
+ assert_equal({ parent_id: 1 }, item.scope_condition)
32
+ assert_equal "pos", item.position_column
33
+ end
34
+
35
+ def test_insert
36
+ new = TopAdditionMixin.create(parent_id: 20)
37
+ assert_equal 1, new.pos
38
+ assert new.first?
39
+ assert new.last?
40
+
41
+ new = TopAdditionMixin.create(parent_id: 20)
42
+ assert_equal 1, new.pos
43
+ assert new.first?
44
+ assert !new.last?
45
+
46
+ new = TopAdditionMixin.create(parent_id: 20)
47
+ assert_equal 1, new.pos
48
+ assert new.first?
49
+ assert !new.last?
50
+
51
+ new = TopAdditionMixin.create(parent_id: 0)
52
+ assert_equal 1, new.pos
53
+ assert new.first?
54
+ assert new.last?
55
+ end
56
+
57
+ def test_insert_at
58
+ new = TopAdditionMixin.create(parent_id: 20)
59
+ assert_equal 1, new.pos
60
+
61
+ new = TopAdditionMixin.create(parent_id: 20)
62
+ assert_equal 1, new.pos
63
+
64
+ new = TopAdditionMixin.create(parent_id: 20)
65
+ assert_equal 1, new.pos
66
+
67
+ new4 = TopAdditionMixin.create(parent_id: 20)
68
+ assert_equal 1, new4.pos
69
+
70
+ new4.insert_at(3)
71
+ assert_equal 3, new4.pos
72
+ end
73
+
74
+ def test_delete_middle
75
+ assert_equal [4, 3, 2, 1], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
76
+
77
+ TopAdditionMixin.where(id: 2).first.destroy
78
+
79
+ assert_equal [4, 3, 1], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
80
+
81
+ assert_equal 3, TopAdditionMixin.where(id: 1).first.pos
82
+ assert_equal 2, TopAdditionMixin.where(id: 3).first.pos
83
+ assert_equal 1, TopAdditionMixin.where(id: 4).first.pos
84
+ end
85
+
86
+ end
87
+ end
@@ -0,0 +1,91 @@
1
+ module Shared
2
+ module ZeroBased
3
+ def setup
4
+ (1..4).each { |counter| ZeroBasedMixin.create! pos: counter, parent_id: 5 }
5
+ end
6
+
7
+ def test_insert
8
+ new = ZeroBasedMixin.create(parent_id: 20)
9
+ assert_equal 0, new.pos
10
+ assert new.first?
11
+ assert new.last?
12
+
13
+ new = ZeroBasedMixin.create(parent_id: 20)
14
+ assert_equal 1, new.pos
15
+ assert !new.first?
16
+ assert new.last?
17
+
18
+ new = ZeroBasedMixin.create(parent_id: 20)
19
+ assert_equal 2, new.pos
20
+ assert !new.first?
21
+ assert new.last?
22
+
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)
29
+ assert_equal 0, new.pos
30
+ assert new.first?
31
+ assert new.last?
32
+ end
33
+
34
+ def test_reordering
35
+ assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
36
+
37
+ ListMixin.where(id: 2).first.move_lower
38
+ assert_equal [1, 3, 2, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
39
+
40
+ ListMixin.where(id: 2).first.move_higher
41
+ assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
42
+
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)
45
+
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)
48
+
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)
51
+
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)
54
+ end
55
+
56
+ def test_insert_at
57
+ new = ZeroBasedMixin.create(parent_id: 20)
58
+ assert_equal 0, new.pos
59
+
60
+ new = ZeroBasedMixin.create(parent_id: 20)
61
+ assert_equal 1, new.pos
62
+
63
+ new = ZeroBasedMixin.create(parent_id: 20)
64
+ assert_equal 2, new.pos
65
+
66
+ new4 = ZeroBasedMixin.create(parent_id: 20)
67
+ assert_equal 3, new4.pos
68
+
69
+ new4.insert_at(2)
70
+ assert_equal 2, new4.pos
71
+
72
+ new.reload
73
+ assert_equal 3, new.pos
74
+
75
+ new.insert_at(2)
76
+ assert_equal 2, new.pos
77
+
78
+ new4.reload
79
+ assert_equal 3, new4.pos
80
+
81
+ new5 = ListMixin.create(parent_id: 20)
82
+ assert_equal 4, new5.pos
83
+
84
+ new5.insert_at(1)
85
+ assert_equal 1, new5.pos
86
+
87
+ new4.reload
88
+ assert_equal 4, new4.pos
89
+ end
90
+ end
91
+ end
data/test/test_list.rb ADDED
@@ -0,0 +1,570 @@
1
+ # NOTE: following now done in helper.rb (better Readability)
2
+ require 'helper'
3
+
4
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
5
+ ActiveRecord::Schema.verbose = false
6
+
7
+ def setup_db(position_options = {})
8
+ # AR caches columns options like defaults etc. Clear them!
9
+ ActiveRecord::Base.connection.create_table :mixins do |t|
10
+ t.column :pos, :integer, position_options
11
+ t.column :active, :boolean, default: true
12
+ t.column :parent_id, :integer
13
+ t.column :parent_type, :string
14
+ t.column :created_at, :datetime
15
+ t.column :updated_at, :datetime
16
+ t.column :state, :integer
17
+ end
18
+
19
+ mixins = [ Mixin, ListMixin, ListMixinSub1, ListMixinSub2, ListWithStringScopeMixin,
20
+ ArrayScopeListMixin, ZeroBasedMixin, DefaultScopedMixin,
21
+ DefaultScopedWhereMixin, TopAdditionMixin, NoAdditionMixin ]
22
+
23
+ mixins << EnumArrayScopeListMixin if rails_4
24
+
25
+ ActiveRecord::Base.connection.schema_cache.clear!
26
+ mixins.each do |klass|
27
+ klass.reset_column_information
28
+ end
29
+ end
30
+
31
+ def setup_db_with_default
32
+ setup_db default: 0
33
+ end
34
+
35
+ # Returns true if ActiveRecord is rails3,4 version
36
+ def rails_3
37
+ defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR >= 3
38
+ end
39
+
40
+ def rails_4
41
+ defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR >= 4
42
+ end
43
+
44
+ def teardown_db
45
+ ActiveRecord::Base.connection.tables.each do |table|
46
+ ActiveRecord::Base.connection.drop_table(table)
47
+ end
48
+ end
49
+
50
+ class Mixin < ActiveRecord::Base
51
+ self.table_name = 'mixins'
52
+ end
53
+
54
+ class ListMixin < Mixin
55
+ acts_as_list column: "pos", scope: :parent
56
+ end
57
+
58
+ class ListMixinSub1 < ListMixin
59
+ end
60
+
61
+ class ListMixinSub2 < ListMixin
62
+ if rails_3
63
+ validates :pos, presence: true
64
+ else
65
+ validates_presence_of :pos
66
+ end
67
+ end
68
+
69
+ class ListWithStringScopeMixin < Mixin
70
+ acts_as_list column: "pos", scope: 'parent_id = #{parent_id}'
71
+ end
72
+
73
+ class ArrayScopeListMixin < Mixin
74
+ acts_as_list column: "pos", scope: [:parent_id, :parent_type]
75
+ end
76
+
77
+ if rails_4
78
+ class EnumArrayScopeListMixin < Mixin
79
+ STATE_VALUES = %w(active archived)
80
+ enum state: STATE_VALUES
81
+
82
+ acts_as_list column: "pos", scope: [:parent_id, :state]
83
+ end
84
+ end
85
+
86
+ class ZeroBasedMixin < Mixin
87
+ acts_as_list column: "pos", top_of_list: 0, scope: [:parent_id]
88
+ end
89
+
90
+ class DefaultScopedMixin < Mixin
91
+ acts_as_list column: "pos"
92
+ default_scope { order('pos ASC') }
93
+ end
94
+
95
+ class DefaultScopedWhereMixin < Mixin
96
+ acts_as_list column: "pos"
97
+ default_scope { order('pos ASC').where(active: true) }
98
+
99
+ def self.for_active_false_tests
100
+ unscoped.order('pos ASC').where(active: false)
101
+ end
102
+ end
103
+
104
+ class TopAdditionMixin < Mixin
105
+ acts_as_list column: "pos", add_new_at: :top, scope: :parent_id
106
+ end
107
+
108
+ class NoAdditionMixin < Mixin
109
+ acts_as_list column: "pos", add_new_at: nil, scope: :parent_id
110
+ end
111
+
112
+ class TheAbstractClass < ActiveRecord::Base
113
+ self.abstract_class = true
114
+ self.table_name = 'mixins'
115
+ end
116
+
117
+ class TheAbstractSubclass < TheAbstractClass
118
+ acts_as_list column: "pos", scope: :parent
119
+ end
120
+
121
+ class TheBaseClass < ActiveRecord::Base
122
+ self.table_name = 'mixins'
123
+ acts_as_list column: "pos", scope: :parent
124
+ end
125
+
126
+ class TheBaseSubclass < TheBaseClass
127
+ end
128
+
129
+ class ActsAsListTestCase < Minitest::Test
130
+ # No default test required as this class is abstract.
131
+ # Need for test/unit.
132
+ undef_method :default_test if method_defined?(:default_test)
133
+
134
+ def teardown
135
+ teardown_db
136
+ end
137
+ end
138
+
139
+ class ZeroBasedTest < ActsAsListTestCase
140
+ include Shared::ZeroBased
141
+
142
+ def setup
143
+ setup_db
144
+ super
145
+ end
146
+ end
147
+
148
+ class ZeroBasedTestWithDefault < ActsAsListTestCase
149
+ include Shared::ZeroBased
150
+
151
+ def setup
152
+ setup_db_with_default
153
+ super
154
+ end
155
+ end
156
+
157
+ class ListTest < ActsAsListTestCase
158
+ include Shared::List
159
+
160
+ def setup
161
+ setup_db
162
+ super
163
+ end
164
+ end
165
+
166
+ class ListTestWithDefault < ActsAsListTestCase
167
+ include Shared::List
168
+
169
+ def setup
170
+ setup_db_with_default
171
+ super
172
+ end
173
+ end
174
+
175
+ class ListSubTest < ActsAsListTestCase
176
+ include Shared::ListSub
177
+
178
+ def setup
179
+ setup_db
180
+ super
181
+ end
182
+ end
183
+
184
+ class ListSubTestWithDefault < ActsAsListTestCase
185
+ include Shared::ListSub
186
+
187
+ def setup
188
+ setup_db_with_default
189
+ super
190
+ end
191
+ end
192
+
193
+ class ArrayScopeListTest < ActsAsListTestCase
194
+ include Shared::ArrayScopeList
195
+
196
+ def setup
197
+ setup_db
198
+ super
199
+ end
200
+ end
201
+
202
+ class ArrayScopeListTestWithDefault < ActsAsListTestCase
203
+ include Shared::ArrayScopeList
204
+
205
+ def setup
206
+ setup_db_with_default
207
+ super
208
+ end
209
+ end
210
+
211
+ class DefaultScopedTest < ActsAsListTestCase
212
+ def setup
213
+ setup_db
214
+ (1..4).each { |counter| DefaultScopedMixin.create!({pos: counter}) }
215
+ end
216
+
217
+ def test_insert
218
+ new = DefaultScopedMixin.create
219
+ assert_equal 5, new.pos
220
+ assert !new.first?
221
+ assert new.last?
222
+
223
+ new = DefaultScopedMixin.create
224
+ assert_equal 6, new.pos
225
+ assert !new.first?
226
+ assert new.last?
227
+
228
+ new = DefaultScopedMixin.create
229
+ assert_equal 7, new.pos
230
+ assert !new.first?
231
+ assert new.last?
232
+ end
233
+
234
+ def test_reordering
235
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
236
+
237
+ DefaultScopedMixin.where(id: 2).first.move_lower
238
+ assert_equal [1, 3, 2, 4], DefaultScopedMixin.all.map(&:id)
239
+
240
+ DefaultScopedMixin.where(id: 2).first.move_higher
241
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
242
+
243
+ DefaultScopedMixin.where(id: 1).first.move_to_bottom
244
+ assert_equal [2, 3, 4, 1], DefaultScopedMixin.all.map(&:id)
245
+
246
+ DefaultScopedMixin.where(id: 1).first.move_to_top
247
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
248
+
249
+ DefaultScopedMixin.where(id: 2).first.move_to_bottom
250
+ assert_equal [1, 3, 4, 2], DefaultScopedMixin.all.map(&:id)
251
+
252
+ DefaultScopedMixin.where(id: 4).first.move_to_top
253
+ assert_equal [4, 1, 3, 2], DefaultScopedMixin.all.map(&:id)
254
+ end
255
+
256
+ def test_insert_at
257
+ new = DefaultScopedMixin.create
258
+ assert_equal 5, new.pos
259
+
260
+ new = DefaultScopedMixin.create
261
+ assert_equal 6, new.pos
262
+
263
+ new = DefaultScopedMixin.create
264
+ assert_equal 7, new.pos
265
+
266
+ new4 = DefaultScopedMixin.create
267
+ assert_equal 8, new4.pos
268
+
269
+ new4.insert_at(2)
270
+ assert_equal 2, new4.pos
271
+
272
+ new.reload
273
+ assert_equal 8, new.pos
274
+
275
+ new.insert_at(2)
276
+ assert_equal 2, new.pos
277
+
278
+ new4.reload
279
+ assert_equal 3, new4.pos
280
+
281
+ new5 = DefaultScopedMixin.create
282
+ assert_equal 9, new5.pos
283
+
284
+ new5.insert_at(1)
285
+ assert_equal 1, new5.pos
286
+
287
+ new4.reload
288
+ assert_equal 4, new4.pos
289
+ end
290
+
291
+ def test_update_position
292
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
293
+ DefaultScopedMixin.where(id: 2).first.set_list_position(4)
294
+ assert_equal [1, 3, 4, 2], DefaultScopedMixin.all.map(&:id)
295
+ DefaultScopedMixin.where(id: 2).first.set_list_position(2)
296
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
297
+ DefaultScopedMixin.where(id: 1).first.set_list_position(4)
298
+ assert_equal [2, 3, 4, 1], DefaultScopedMixin.all.map(&:id)
299
+ DefaultScopedMixin.where(id: 1).first.set_list_position(1)
300
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
301
+ end
302
+ end
303
+
304
+ class DefaultScopedWhereTest < ActsAsListTestCase
305
+ def setup
306
+ setup_db
307
+ (1..4).each { |counter| DefaultScopedWhereMixin.create! pos: counter, active: false }
308
+ end
309
+
310
+ def test_insert
311
+ new = DefaultScopedWhereMixin.create
312
+ assert_equal 5, new.pos
313
+ assert !new.first?
314
+ assert new.last?
315
+
316
+ new = DefaultScopedWhereMixin.create
317
+ assert_equal 6, new.pos
318
+ assert !new.first?
319
+ assert new.last?
320
+
321
+ new = DefaultScopedWhereMixin.create
322
+ assert_equal 7, new.pos
323
+ assert !new.first?
324
+ assert new.last?
325
+ end
326
+
327
+ def test_reordering
328
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
329
+
330
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 2).first.move_lower
331
+ assert_equal [1, 3, 2, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
332
+
333
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 2).first.move_higher
334
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
335
+
336
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 1).first.move_to_bottom
337
+ assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
338
+
339
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 1).first.move_to_top
340
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
341
+
342
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 2).first.move_to_bottom
343
+ assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
344
+
345
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 4).first.move_to_top
346
+ assert_equal [4, 1, 3, 2], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
347
+ end
348
+
349
+ def test_insert_at
350
+ new = DefaultScopedWhereMixin.create
351
+ assert_equal 5, new.pos
352
+
353
+ new = DefaultScopedWhereMixin.create
354
+ assert_equal 6, new.pos
355
+
356
+ new = DefaultScopedWhereMixin.create
357
+ assert_equal 7, new.pos
358
+
359
+ new4 = DefaultScopedWhereMixin.create
360
+ assert_equal 8, new4.pos
361
+
362
+ new4.insert_at(2)
363
+ assert_equal 2, new4.pos
364
+
365
+ new.reload
366
+ assert_equal 8, new.pos
367
+
368
+ new.insert_at(2)
369
+ assert_equal 2, new.pos
370
+
371
+ new4.reload
372
+ assert_equal 3, new4.pos
373
+
374
+ new5 = DefaultScopedWhereMixin.create
375
+ assert_equal 9, new5.pos
376
+
377
+ new5.insert_at(1)
378
+ assert_equal 1, new5.pos
379
+
380
+ new4.reload
381
+ assert_equal 4, new4.pos
382
+ end
383
+
384
+ def test_update_position
385
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
386
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 2).first.set_list_position(4)
387
+ assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
388
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 2).first.set_list_position(2)
389
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
390
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 1).first.set_list_position(4)
391
+ assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
392
+ DefaultScopedWhereMixin.for_active_false_tests.where(id: 1).first.set_list_position(1)
393
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
394
+ end
395
+
396
+ end
397
+
398
+ class MultiDestroyTest < ActsAsListTestCase
399
+
400
+ def setup
401
+ setup_db
402
+ end
403
+
404
+ # example:
405
+ #
406
+ # class TodoList < ActiveRecord::Base
407
+ # has_many :todo_items, order: "position"
408
+ # accepts_nested_attributes_for :todo_items, allow_destroy: true
409
+ # end
410
+ #
411
+ # class TodoItem < ActiveRecord::Base
412
+ # belongs_to :todo_list
413
+ # acts_as_list scope: :todo_list
414
+ # end
415
+ #
416
+ # Assume that there are three items.
417
+ # The user mark two items as deleted, click save button, form will be post:
418
+ #
419
+ # todo_list.todo_items_attributes = [
420
+ # {id: 1, _destroy: true},
421
+ # {id: 2, _destroy: true}
422
+ # ]
423
+ #
424
+ # Save toto_list, the position of item #3 should eql 1.
425
+ #
426
+ def test_destroy
427
+ new1 = DefaultScopedMixin.create
428
+ assert_equal 1, new1.pos
429
+
430
+ new2 = DefaultScopedMixin.create
431
+ assert_equal 2, new2.pos
432
+
433
+ new3 = DefaultScopedMixin.create
434
+ assert_equal 3, new3.pos
435
+
436
+ new1.destroy
437
+ new2.destroy
438
+ new3.reload
439
+ assert_equal 1, new3.pos
440
+ end
441
+ end
442
+
443
+ #class TopAdditionMixin < Mixin
444
+
445
+ class TopAdditionTest < ActsAsListTestCase
446
+ include Shared::TopAddition
447
+
448
+ def setup
449
+ setup_db
450
+ super
451
+ end
452
+ end
453
+
454
+ class TopAdditionTestWithDefault < ActsAsListTestCase
455
+ include Shared::TopAddition
456
+
457
+ def setup
458
+ setup_db_with_default
459
+ super
460
+ end
461
+ end
462
+
463
+ class NoAdditionTest < ActsAsListTestCase
464
+ include Shared::NoAddition
465
+
466
+ def setup
467
+ setup_db
468
+ super
469
+ end
470
+ end
471
+
472
+ class MultipleListsTest < ActsAsListTestCase
473
+ def setup
474
+ setup_db
475
+ (1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 1}
476
+ (1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 2}
477
+ end
478
+
479
+ def test_check_scope_order
480
+ assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 1).order(:pos).map(&:id)
481
+ assert_equal [5, 6, 7, 8], ListMixin.where(:parent_id => 2).order(:pos).map(&:id)
482
+ ListMixin.find(4).update_attributes(:parent_id => 2, :pos => 2)
483
+ assert_equal [1, 2, 3], ListMixin.where(:parent_id => 1).order(:pos).map(&:id)
484
+ assert_equal [5, 4, 6, 7, 8], ListMixin.where(:parent_id => 2).order(:pos).map(&:id)
485
+ end
486
+
487
+ def test_check_scope_position
488
+ assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 1).map(&:pos)
489
+ assert_equal [1, 2, 3, 4], ListMixin.where(:parent_id => 2).map(&:pos)
490
+ ListMixin.find(4).update_attributes(:parent_id => 2, :pos => 2)
491
+ assert_equal [1, 2, 3], ListMixin.where(:parent_id => 1).order(:pos).map(&:pos)
492
+ assert_equal [1, 2, 3, 4, 5], ListMixin.where(:parent_id => 2).order(:pos).map(&:pos)
493
+ end
494
+ end
495
+
496
+ if rails_4
497
+ class EnumArrayScopeListMixinTest < ActsAsListTestCase
498
+ def setup
499
+ setup_db
500
+ EnumArrayScopeListMixin.create! :parent_id => 1, :state => EnumArrayScopeListMixin.states['active']
501
+ EnumArrayScopeListMixin.create! :parent_id => 1, :state => EnumArrayScopeListMixin.states['archived']
502
+ EnumArrayScopeListMixin.create! :parent_id => 2, :state => EnumArrayScopeListMixin.states["active"]
503
+ EnumArrayScopeListMixin.create! :parent_id => 2, :state => EnumArrayScopeListMixin.states["archived"]
504
+ end
505
+
506
+ def test_positions
507
+ assert_equal [1], EnumArrayScopeListMixin.where(:parent_id => 1, :state => EnumArrayScopeListMixin.states['active']).map(&:pos)
508
+ assert_equal [1], EnumArrayScopeListMixin.where(:parent_id => 1, :state => EnumArrayScopeListMixin.states['archived']).map(&:pos)
509
+ assert_equal [1], EnumArrayScopeListMixin.where(:parent_id => 2, :state => EnumArrayScopeListMixin.states['active']).map(&:pos)
510
+ assert_equal [1], EnumArrayScopeListMixin.where(:parent_id => 2, :state => EnumArrayScopeListMixin.states['archived']).map(&:pos)
511
+ end
512
+ end
513
+ end
514
+
515
+ class MultipleListsArrayScopeTest < ActsAsListTestCase
516
+ def setup
517
+ setup_db
518
+ (1..4).each { |counter| ArrayScopeListMixin.create! :pos => counter,:parent_id => 1, :parent_type => 'anything'}
519
+ (1..4).each { |counter| ArrayScopeListMixin.create! :pos => counter,:parent_id => 2, :parent_type => 'something'}
520
+ (1..4).each { |counter| ArrayScopeListMixin.create! :pos => counter,:parent_id => 3, :parent_type => 'anything'}
521
+ end
522
+
523
+ def test_order_after_all_scope_properties_are_changed
524
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:id)
525
+ assert_equal [5, 6, 7, 8], ArrayScopeListMixin.where(:parent_id => 2, :parent_type => 'something').order(:pos).map(&:id)
526
+ ArrayScopeListMixin.find(2).update_attributes(:parent_id => 2, :pos => 2,:parent_type => 'something')
527
+ assert_equal [1, 3, 4], ArrayScopeListMixin.where(:parent_id => 1,:parent_type => 'anything').order(:pos).map(&:id)
528
+ assert_equal [5, 2, 6, 7, 8], ArrayScopeListMixin.where(:parent_id => 2,:parent_type => 'something').order(:pos).map(&:id)
529
+ end
530
+
531
+ def test_position_after_all_scope_properties_are_changed
532
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').map(&:pos)
533
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 2, :parent_type => 'something').map(&:pos)
534
+ ArrayScopeListMixin.find(4).update_attributes(:parent_id => 2, :pos => 2, :parent_type => 'something')
535
+ assert_equal [1, 2, 3], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:pos)
536
+ assert_equal [1, 2, 3, 4, 5], ArrayScopeListMixin.where(:parent_id => 2, :parent_type => 'something').order(:pos).map(&:pos)
537
+ end
538
+
539
+ def test_order_after_one_scope_property_is_changed
540
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:id)
541
+ assert_equal [9, 10, 11, 12], ArrayScopeListMixin.where(:parent_id => 3, :parent_type => 'anything').order(:pos).map(&:id)
542
+ ArrayScopeListMixin.find(2).update_attributes(:parent_id => 3, :pos => 2)
543
+ assert_equal [1, 3, 4], ArrayScopeListMixin.where(:parent_id => 1,:parent_type => 'anything').order(:pos).map(&:id)
544
+ assert_equal [9, 2, 10, 11, 12], ArrayScopeListMixin.where(:parent_id => 3,:parent_type => 'anything').order(:pos).map(&:id)
545
+ end
546
+
547
+ def test_position_after_one_scope_property_is_changed
548
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').map(&:pos)
549
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 3, :parent_type => 'anything').map(&:pos)
550
+ ArrayScopeListMixin.find(4).update_attributes(:parent_id => 3, :pos => 2)
551
+ assert_equal [1, 2, 3], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:pos)
552
+ assert_equal [1, 2, 3, 4, 5], ArrayScopeListMixin.where(:parent_id => 3, :parent_type => 'anything').order(:pos).map(&:pos)
553
+ end
554
+
555
+ def test_order_after_moving_to_empty_list
556
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:id)
557
+ assert_equal [], ArrayScopeListMixin.where(:parent_id => 4, :parent_type => 'anything').order(:pos).map(&:id)
558
+ ArrayScopeListMixin.find(2).update_attributes(:parent_id => 4, :pos => 1)
559
+ assert_equal [1, 3, 4], ArrayScopeListMixin.where(:parent_id => 1,:parent_type => 'anything').order(:pos).map(&:id)
560
+ assert_equal [2], ArrayScopeListMixin.where(:parent_id => 4,:parent_type => 'anything').order(:pos).map(&:id)
561
+ end
562
+
563
+ def test_position_after_moving_to_empty_list
564
+ assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').map(&:pos)
565
+ assert_equal [], ArrayScopeListMixin.where(:parent_id => 4, :parent_type => 'anything').map(&:pos)
566
+ ArrayScopeListMixin.find(2).update_attributes(:parent_id => 4, :pos => 1)
567
+ assert_equal [1, 2, 3], ArrayScopeListMixin.where(:parent_id => 1, :parent_type => 'anything').order(:pos).map(&:pos)
568
+ assert_equal [1], ArrayScopeListMixin.where(:parent_id => 4, :parent_type => 'anything').order(:pos).map(&:pos)
569
+ end
570
+ end