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.
- checksums.yaml +7 -0
- data/.travis.yml +10 -1
- data/Gemfile +9 -0
- data/MIT-LICENSE +19 -0
- data/README.md +4 -1
- data/Rakefile +15 -9
- data/acts_as_list.gemspec +2 -3
- data/gemfiles/rails3/Gemfile +17 -0
- data/gemfiles/rails4/Gemfile +18 -0
- data/lib/acts_as_list/active_record/acts/list.rb +70 -29
- data/lib/acts_as_list/version.rb +1 -1
- data/test/helper.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 +28 -23
- data/test/test_list.rb +148 -112
- metadata +55 -98
data/test/shared_zero_based.rb
CHANGED
|
@@ -1,64 +1,69 @@
|
|
|
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
|
+
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.
|
|
35
|
+
assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
|
|
31
36
|
|
|
32
|
-
ListMixin.
|
|
33
|
-
assert_equal [1, 3, 2, 4], ZeroBasedMixin.
|
|
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.
|
|
36
|
-
assert_equal [1, 2, 3, 4], ZeroBasedMixin.
|
|
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.
|
|
39
|
-
assert_equal [2, 3, 4, 1], ZeroBasedMixin.
|
|
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.
|
|
42
|
-
assert_equal [1, 2, 3, 4], ZeroBasedMixin.
|
|
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.
|
|
45
|
-
assert_equal [1, 3, 4, 2], ZeroBasedMixin.
|
|
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.
|
|
48
|
-
assert_equal [4, 1, 3, 2], ZeroBasedMixin.
|
|
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(:
|
|
57
|
+
new = ZeroBasedMixin.create(parent_id: 20)
|
|
53
58
|
assert_equal 0, new.pos
|
|
54
59
|
|
|
55
|
-
new = ZeroBasedMixin.create(:
|
|
60
|
+
new = ZeroBasedMixin.create(parent_id: 20)
|
|
56
61
|
assert_equal 1, new.pos
|
|
57
62
|
|
|
58
|
-
new = ZeroBasedMixin.create(:
|
|
63
|
+
new = ZeroBasedMixin.create(parent_id: 20)
|
|
59
64
|
assert_equal 2, new.pos
|
|
60
65
|
|
|
61
|
-
new4 = ZeroBasedMixin.create(:
|
|
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(:
|
|
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(:
|
|
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,39 +47,47 @@ 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) }
|
|
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 :
|
|
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 <
|
|
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!({:
|
|
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.
|
|
196
|
+
assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
|
|
208
197
|
|
|
209
|
-
DefaultScopedMixin.
|
|
210
|
-
assert_equal [1, 3, 2, 4], DefaultScopedMixin.
|
|
198
|
+
DefaultScopedMixin.where(id: 2).first.move_lower
|
|
199
|
+
assert_equal [1, 3, 2, 4], DefaultScopedMixin.all.map(&:id)
|
|
211
200
|
|
|
212
|
-
DefaultScopedMixin.
|
|
213
|
-
assert_equal [1, 2, 3, 4], DefaultScopedMixin.
|
|
201
|
+
DefaultScopedMixin.where(id: 2).first.move_higher
|
|
202
|
+
assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
|
|
214
203
|
|
|
215
|
-
DefaultScopedMixin.
|
|
216
|
-
assert_equal [2, 3, 4, 1], DefaultScopedMixin.
|
|
204
|
+
DefaultScopedMixin.where(id: 1).first.move_to_bottom
|
|
205
|
+
assert_equal [2, 3, 4, 1], DefaultScopedMixin.all.map(&:id)
|
|
217
206
|
|
|
218
|
-
DefaultScopedMixin.
|
|
219
|
-
assert_equal [1, 2, 3, 4], DefaultScopedMixin.
|
|
207
|
+
DefaultScopedMixin.where(id: 1).first.move_to_top
|
|
208
|
+
assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
|
|
220
209
|
|
|
221
|
-
DefaultScopedMixin.
|
|
222
|
-
assert_equal [1, 3, 4, 2], DefaultScopedMixin.
|
|
210
|
+
DefaultScopedMixin.where(id: 2).first.move_to_bottom
|
|
211
|
+
assert_equal [1, 3, 4, 2], DefaultScopedMixin.all.map(&:id)
|
|
223
212
|
|
|
224
|
-
DefaultScopedMixin.
|
|
225
|
-
assert_equal [4, 1, 3, 2], DefaultScopedMixin.
|
|
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.
|
|
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.
|
|
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! :
|
|
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.
|
|
289
|
+
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.for_active_false_tests.map(&:id)
|
|
302
290
|
|
|
303
|
-
DefaultScopedWhereMixin.where(:
|
|
304
|
-
assert_equal [1, 3, 2, 4], DefaultScopedWhereMixin.
|
|
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(:
|
|
307
|
-
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.
|
|
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(:
|
|
310
|
-
assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.
|
|
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(:
|
|
313
|
-
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.
|
|
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(:
|
|
316
|
-
assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.
|
|
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(:
|
|
319
|
-
assert_equal [4, 1, 3, 2], DefaultScopedWhereMixin.
|
|
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.
|
|
359
|
-
DefaultScopedWhereMixin.where(:
|
|
360
|
-
assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.
|
|
361
|
-
DefaultScopedWhereMixin.where(:
|
|
362
|
-
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.
|
|
363
|
-
DefaultScopedWhereMixin.where(:
|
|
364
|
-
assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.
|
|
365
|
-
DefaultScopedWhereMixin.where(:
|
|
366
|
-
assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.
|
|
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, :
|
|
381
|
-
# accepts_nested_attributes_for :todo_items, :
|
|
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 :
|
|
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
|
|
424
|
+
class NoAdditionTest < ActsAsListTestCase
|
|
425
|
+
include Shared::NoAddition
|
|
426
|
+
|
|
438
427
|
def setup
|
|
439
|
-
|
|
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
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
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
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
assert_equal
|
|
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
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
assert_equal
|
|
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
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
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
|