acts_as_list 1.2.1 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,188 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Shared
4
- module ListSub
5
- def setup
6
- (1..4).each do |i|
7
- node = ((i % 2 == 1) ? ListMixinSub1 : ListMixinSub2).new parent_id: 5000
8
- node.pos = i
9
- node.save!
10
- end
11
- end
12
-
13
- def test_reordering
14
- assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
15
-
16
- ListMixin.where(id: 2).first.move_lower
17
- assert_equal [1, 3, 2, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
18
-
19
- ListMixin.where(id: 2).first.move_higher
20
- assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
21
-
22
- ListMixin.where(id: 1).first.move_to_bottom
23
- assert_equal [2, 3, 4, 1], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
24
-
25
- ListMixin.where(id: 1).first.move_to_top
26
- assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
27
-
28
- ListMixin.where(id: 2).first.move_to_bottom
29
- assert_equal [1, 3, 4, 2], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
30
-
31
- ListMixin.where(id: 4).first.move_to_top
32
- assert_equal [4, 1, 3, 2], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
33
- end
34
-
35
- def test_move_to_bottom_with_next_to_last_item
36
- assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
37
- ListMixin.where(id: 3).first.move_to_bottom
38
- assert_equal [1, 2, 4, 3], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
39
- end
40
-
41
- def test_next_prev
42
- assert_equal ListMixin.where(id: 2).first, ListMixin.where(id: 1).first.lower_item
43
- assert_nil ListMixin.where(id: 1).first.higher_item
44
- assert_equal ListMixin.where(id: 3).first, ListMixin.where(id: 4).first.higher_item
45
- assert_nil ListMixin.where(id: 4).first.lower_item
46
- end
47
-
48
- def test_next_prev_not_regular_sequence
49
- ListMixin.all.each do |item|
50
- item.update pos: item.pos * 5
51
- end
52
-
53
- assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
54
- assert_equal [5, 10, 15, 20], ListMixin.where(parent_id: 5000).order('id').map(&:pos)
55
-
56
- ListMixin.where(id: 2).first.move_lower
57
- assert_equal [1, 3, 2, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
58
- assert_equal [5, 15, 10, 20], ListMixin.where(parent_id: 5000).order('id').map(&:pos)
59
-
60
-
61
- ListMixin.where(id: 2).first.move_higher
62
- assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
63
- assert_equal [5, 10, 15, 20], ListMixin.where(parent_id: 5000).order('id').map(&:pos)
64
-
65
- ListMixin.where(id: 1).first.move_to_bottom
66
- assert_equal [2, 3, 4, 1], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
67
-
68
- ListMixin.where(id: 1).first.move_to_top
69
- assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
70
-
71
- ListMixin.where(id: 2).first.move_to_bottom
72
- assert_equal [1, 3, 4, 2], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
73
-
74
- ListMixin.where(id: 4).first.move_to_top
75
- assert_equal [4, 1, 3, 2], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
76
- end
77
-
78
- def test_next_prev_groups
79
- li1 = ListMixin.where(id: 1).first
80
- li2 = ListMixin.where(id: 2).first
81
- li3 = ListMixin.where(id: 3).first
82
- li4 = ListMixin.where(id: 4).first
83
- assert_equal [li2, li3, li4], li1.lower_items
84
- assert_equal [li4], li3.lower_items
85
- assert_equal [li2, li3], li1.lower_items(2)
86
- assert_equal [], li4.lower_items
87
-
88
- assert_equal [li2, li1], li3.higher_items
89
- assert_equal [li1], li2.higher_items
90
- assert_equal [li3, li2], li4.higher_items(2)
91
- assert_equal [], li1.higher_items
92
- end
93
-
94
- def test_next_prev_groups_with_same_position
95
- li1 = ListMixin.where(id: 1).first
96
- li2 = ListMixin.where(id: 2).first
97
- li3 = ListMixin.where(id: 3).first
98
- li4 = ListMixin.where(id: 4).first
99
-
100
- li3.update_column(:pos, 2) # Make the same position as li2
101
-
102
- assert_equal [1, 2, 2, 4], ListMixin.order(:pos).pluck(:pos)
103
-
104
- assert_equal [li3, li4], li2.lower_items
105
- assert_equal [li2, li4], li3.lower_items
106
-
107
- assert_equal [li3, li1], li2.higher_items
108
- assert_equal [li2, li1], li3.higher_items
109
- end
110
-
111
- def test_injection
112
- item = ListMixin.new("parent_id"=>1)
113
- assert_equal({ parent_id: 1 }, item.scope_condition)
114
- assert_equal "pos", item.position_column
115
- end
116
-
117
- def test_insert_at
118
- new = ListMixin.create("parent_id" => 20)
119
- assert_equal 1, new.pos
120
-
121
- new = ListMixinSub1.create("parent_id" => 20)
122
- assert_equal 2, new.pos
123
-
124
- new = ListMixinSub1.create("parent_id" => 20)
125
- assert_equal 3, new.pos
126
-
127
- new_noup = ListMixinSub1.acts_as_list_no_update { ListMixinSub1.create("parent_id" => 20) }
128
- assert_equal_or_nil $default_position, new_noup.pos
129
-
130
- new4 = ListMixin.create("parent_id" => 20)
131
- assert_equal 4, new4.pos
132
-
133
- new4.insert_at(3)
134
- assert_equal 3, new4.pos
135
-
136
- new.reload
137
- assert_equal 4, new.pos
138
-
139
- new.insert_at(2)
140
- assert_equal 2, new.pos
141
-
142
- new4.reload
143
- assert_equal 4, new4.pos
144
-
145
- new5 = ListMixinSub1.create("parent_id" => 20)
146
- assert_equal 5, new5.pos
147
-
148
- new5.insert_at(1)
149
- assert_equal 1, new5.pos
150
-
151
- new4.reload
152
- assert_equal 5, new4.pos
153
-
154
- new_noup.reload
155
- assert_equal_or_nil $default_position, new_noup.pos
156
- end
157
-
158
- def test_delete_middle
159
- assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
160
-
161
- ListMixin.where(id: 2).first.destroy
162
-
163
- assert_equal [1, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
164
-
165
- assert_equal 1, ListMixin.where(id: 1).first.pos
166
- assert_equal 2, ListMixin.where(id: 3).first.pos
167
- assert_equal 3, ListMixin.where(id: 4).first.pos
168
-
169
- ListMixin.where(id: 1).first.destroy
170
-
171
- assert_equal [3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
172
-
173
- assert_equal 1, ListMixin.where(id: 3).first.pos
174
- assert_equal 2, ListMixin.where(id: 4).first.pos
175
-
176
- ListMixin.acts_as_list_no_update { ListMixin.where(id: 3).first.destroy }
177
-
178
- assert_equal [4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
179
-
180
- assert_equal 2, ListMixin.where(id: 4).first.pos
181
- end
182
-
183
- def test_acts_as_list_class
184
- assert_equal TheBaseClass, TheBaseSubclass.new.acts_as_list_class
185
- assert_equal TheAbstractSubclass, TheAbstractSubclass.new.acts_as_list_class
186
- end
187
- end
188
- end
@@ -1,73 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Shared
4
- module NoAddition
5
- def setup
6
- (1..4).each { |counter| NoAdditionMixin.create! pos: counter, parent_id: 5 }
7
- end
8
-
9
- def test_insert
10
- new = NoAdditionMixin.create(parent_id: 20)
11
- assert_nil new.pos
12
- assert !new.in_list?
13
-
14
- new = NoAdditionMixin.create(parent_id: 20)
15
- assert_nil new.pos
16
- end
17
-
18
- def test_update_does_not_add_to_list
19
- new = NoAdditionMixin.create(parent_id: 20)
20
- new.update_attribute(:updated_at, Time.now) # force some change
21
- new.reload
22
-
23
- assert !new.in_list?
24
- end
25
-
26
- def test_update_scope_does_not_add_to_list
27
- new = NoAdditionMixin.create
28
-
29
- new.update_attribute(:parent_id, 20)
30
- new.reload
31
- assert !new.in_list?
32
-
33
- new.update_attribute(:parent_id, 5)
34
- new.reload
35
- assert !new.in_list?
36
- end
37
-
38
- def test_collision_avoidance_with_explicit_position
39
- first = NoAdditionMixin.create(parent_id: 20, pos: 1)
40
- second = NoAdditionMixin.create(parent_id: 20, pos: 1)
41
- third = NoAdditionMixin.create(parent_id: 30, pos: 1)
42
-
43
- first.reload
44
- second.reload
45
- third.reload
46
-
47
- assert_equal 2, first.pos
48
- assert_equal 1, second.pos
49
- assert_equal 1, third.pos
50
-
51
- first.update(pos: 1)
52
-
53
- first.reload
54
- second.reload
55
-
56
- assert_equal 1, first.pos
57
- assert_equal 2, second.pos
58
-
59
- first.update(parent_id: 30)
60
-
61
- first.reload
62
- second.reload
63
- third.reload
64
-
65
- assert_equal 1, first.pos
66
- assert_equal 30, first.parent_id
67
- assert_equal 1, second.pos
68
- assert_equal 20, second.parent_id
69
- assert_equal 2, third.pos
70
- assert_equal 30, third.parent_id
71
- end
72
- end
73
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Shared
4
- module Quoting
5
-
6
- def setup
7
- 3.times { |counter| QuotedList.create! order: counter }
8
- end
9
-
10
- def test_create
11
- assert_equal QuotedList.in_list.size, 3
12
- end
13
-
14
- # This test execute raw queries involving table name
15
- def test_moving
16
- item = QuotedList.first
17
- item.higher_items
18
- item.lower_items
19
- item.send :bottom_item # Part of private api
20
- end
21
-
22
- end
23
- end
@@ -1,110 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Shared
4
- module TopAddition
5
- def setup
6
- (1..4).each { |counter| TopAdditionMixin.create! pos: counter, parent_id: 5 }
7
- end
8
-
9
- def test_setup_state
10
- # If we explicitly define a position (as above) then that position is what gets applied
11
- assert_equal [1, 2, 3, 4], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
12
- end
13
-
14
- def test_reordering
15
- TopAdditionMixin.where(id: 2).first.move_lower
16
- assert_equal [1, 3, 2, 4], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
17
-
18
- TopAdditionMixin.where(id: 2).first.move_higher
19
- assert_equal [1, 2, 3, 4], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
20
-
21
- TopAdditionMixin.where(id: 1).first.move_to_bottom
22
- assert_equal [2, 3, 4, 1], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
23
-
24
- TopAdditionMixin.where(id: 1).first.move_to_top
25
- assert_equal [1, 2, 3, 4], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
26
-
27
- TopAdditionMixin.where(id: 2).first.move_to_bottom
28
- assert_equal [1, 3, 4, 2], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
29
-
30
- TopAdditionMixin.where(id: 4).first.move_to_top
31
- assert_equal [4, 1, 3, 2], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
32
- end
33
-
34
- def test_injection
35
- item = TopAdditionMixin.new(parent_id: 1)
36
- assert_equal({ parent_id: 1 }, item.scope_condition)
37
- assert_equal "pos", item.position_column
38
- end
39
-
40
- def test_insert
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.acts_as_list_no_update { TopAdditionMixin.create(parent_id: 20) }
52
- assert_equal_or_nil $default_position, new.pos
53
- assert_equal $default_position.is_a?(Integer), new.first?
54
- assert !new.last?
55
-
56
- new = TopAdditionMixin.create(parent_id: 20)
57
- assert_equal 1, new.pos
58
- assert_equal $default_position.nil?, new.first?
59
- assert !new.last?
60
-
61
- new = TopAdditionMixin.create(parent_id: 0)
62
- assert_equal 1, new.pos
63
- assert new.first?
64
- assert new.last?
65
- end
66
-
67
- def test_insert_at
68
- new = TopAdditionMixin.create(parent_id: 20)
69
- assert_equal 1, new.pos
70
-
71
- new = TopAdditionMixin.create(parent_id: 20)
72
- assert_equal 1, new.pos
73
-
74
- new = TopAdditionMixin.create(parent_id: 20)
75
- assert_equal 1, new.pos
76
-
77
- new = TopAdditionMixin.acts_as_list_no_update { TopAdditionMixin.create(parent_id: 20) }
78
- assert_equal_or_nil $default_position, new.pos
79
-
80
- new4 = TopAdditionMixin.create(parent_id: 20)
81
- assert_equal 1, new4.pos
82
-
83
- new4.insert_at(3)
84
- assert_equal 3, new4.pos
85
- end
86
-
87
- def test_supplied_position
88
- new = TopAdditionMixin.create(parent_id: 20, pos: 3)
89
- assert_equal 3, new.pos
90
- end
91
-
92
- def test_delete_middle
93
- TopAdditionMixin.where(id: 2).first.destroy
94
-
95
- assert_equal [1, 3, 4], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
96
-
97
- assert_equal 1, TopAdditionMixin.where(id: 1).first.pos
98
- assert_equal 2, TopAdditionMixin.where(id: 3).first.pos
99
- assert_equal 3, TopAdditionMixin.where(id: 4).first.pos
100
-
101
- TopAdditionMixin.acts_as_list_no_update { TopAdditionMixin.where(id: 3).first.destroy }
102
-
103
- assert_equal [1, 4], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
104
-
105
- assert_equal 1, TopAdditionMixin.where(id: 1).first.pos
106
- assert_equal 3, TopAdditionMixin.where(id: 4).first.pos
107
- end
108
-
109
- end
110
- end
@@ -1,104 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Shared
4
- module ZeroBased
5
- def setup
6
- (1..4).each { |counter| ZeroBasedMixin.create! pos: counter, parent_id: 5 }
7
- end
8
-
9
- def test_insert
10
- new = ZeroBasedMixin.create(parent_id: 20)
11
- assert_equal 0, new.pos
12
- assert new.first?
13
- assert new.last?
14
-
15
- new = ZeroBasedMixin.create(parent_id: 20)
16
- assert_equal 1, new.pos
17
- assert !new.first?
18
- assert new.last?
19
-
20
- new = ZeroBasedMixin.acts_as_list_no_update { ZeroBasedMixin.create(parent_id: 20) }
21
- assert_equal_or_nil $default_position, new.pos
22
- assert !new.first?
23
- assert !new.last?
24
-
25
- new = ZeroBasedMixin.create(parent_id: 20)
26
- assert_equal 2, new.pos
27
- assert !new.first?
28
- assert new.last?
29
-
30
- new = ZeroBasedMixin.create(parent_id: 0)
31
- assert_equal 0, new.pos
32
- assert new.first?
33
- assert new.last?
34
-
35
- new = ZeroBasedMixin.create(parent_id: 1, pos: -500)
36
- assert_equal 0, new.pos
37
- assert new.first?
38
- assert new.last?
39
- end
40
-
41
- def test_reordering
42
- assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
43
-
44
- ListMixin.where(id: 2).first.move_lower
45
- assert_equal [1, 3, 2, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
46
-
47
- ListMixin.where(id: 2).first.move_higher
48
- assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
49
-
50
- ListMixin.where(id: 1).first.move_to_bottom
51
- assert_equal [2, 3, 4, 1], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
52
-
53
- ListMixin.where(id: 1).first.move_to_top
54
- assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
55
-
56
- ListMixin.where(id: 2).first.move_to_bottom
57
- assert_equal [1, 3, 4, 2], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
58
-
59
- ListMixin.where(id: 4).first.move_to_top
60
- assert_equal [4, 1, 3, 2], ZeroBasedMixin.where(parent_id: 5).order('pos').map(&:id)
61
- end
62
-
63
- def test_insert_at
64
- new = ZeroBasedMixin.create(parent_id: 20)
65
- assert_equal 0, new.pos
66
-
67
- new = ZeroBasedMixin.create(parent_id: 20)
68
- assert_equal 1, new.pos
69
-
70
- new = ZeroBasedMixin.create(parent_id: 20)
71
- assert_equal 2, new.pos
72
-
73
- new_noup = ZeroBasedMixin.acts_as_list_no_update { ZeroBasedMixin.create(parent_id: 20) }
74
- assert_equal_or_nil $default_position, new_noup.pos
75
-
76
- new4 = ZeroBasedMixin.create(parent_id: 20)
77
- assert_equal 3, new4.pos
78
-
79
- new4.insert_at(2)
80
- assert_equal 2, new4.pos
81
-
82
- new.reload
83
- assert_equal 3, new.pos
84
-
85
- new.insert_at(2)
86
- assert_equal 2, new.pos
87
-
88
- new4.reload
89
- assert_equal 3, new4.pos
90
-
91
- new5 = ListMixin.create(parent_id: 20)
92
- assert_equal 4, new5.pos
93
-
94
- new5.insert_at(1)
95
- assert_equal 1, new5.pos
96
-
97
- new4.reload
98
- assert_equal 4, new4.pos
99
-
100
- new_noup.reload
101
- assert_equal_or_nil $default_position, new_noup.pos
102
- end
103
- end
104
- end
@@ -1,20 +0,0 @@
1
- sqlite:
2
- adapter: sqlite3
3
- database: file::memory:?cache=shared
4
-
5
- mysql:
6
- adapter: mysql2
7
- database: runner
8
- pool: 5
9
- timeout: 5000
10
- username: root
11
- password: root
12
-
13
- postgresql:
14
- adapter: postgresql
15
- database: runner
16
- pool: 5
17
- timeout: 5000
18
- username: runner
19
- password:
20
- min_messages: ERROR
@@ -1,18 +0,0 @@
1
- sqlite:
2
- adapter: sqlite3
3
- database: file::memory:?cache=shared
4
-
5
- mysql:
6
- adapter: mysql2
7
- host: 127.0.0.1
8
- username: root
9
- password:
10
- database: acts_as_list
11
-
12
- postgresql:
13
- adapter: postgresql
14
- host: localhost
15
- username: postgres
16
- password: postgres
17
- database: acts_as_list
18
- min_messages: ERROR
@@ -1,33 +0,0 @@
1
- require 'helper'
2
-
3
- class Animal < ActiveRecord::Base
4
- acts_as_list
5
- default_scope -> { select(:name) }
6
- end
7
-
8
- class DefaultScopeWithSelectTest < Minitest::Test
9
- def setup
10
- ActiveRecord::Base.connection.create_table :animals do |t|
11
- t.column :position, :integer
12
- t.column :name, :string
13
- end
14
-
15
- ActiveRecord::Base.connection.schema_cache.clear!
16
- Animal.reset_column_information
17
- super
18
- end
19
-
20
- def teardown
21
- teardown_db
22
- super
23
- end
24
-
25
- def test_default_scope_with_select
26
- animal1 = Animal.create name: 'Fox'
27
- animal2 = Animal.create name: 'Panda'
28
- animal3 = Animal.create name: 'Wildebeast'
29
- assert_equal 1, animal1.position
30
- assert_equal 2, animal2.position
31
- assert_equal 3, animal3.position
32
- end
33
- end
@@ -1,61 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'helper'
4
-
5
- class Section < ActiveRecord::Base
6
- has_many :items
7
- acts_as_list
8
-
9
- scope :visible, -> { where(visible: true) }
10
- end
11
-
12
- class Item < ActiveRecord::Base
13
- belongs_to :section
14
- acts_as_list scope: :section
15
-
16
- scope :visible, -> { where(visible: true).joins(:section).merge(Section.visible) }
17
- end
18
-
19
- class JoinedTestCase < Minitest::Test
20
- def setup
21
- ActiveRecord::Base.connection.create_table :sections do |t|
22
- t.column :position, :integer
23
- t.column :visible, :boolean, default: true
24
- end
25
-
26
- ActiveRecord::Base.connection.create_table :items do |t|
27
- t.column :position, :integer
28
- t.column :section_id, :integer
29
- t.column :visible, :boolean, default: true
30
- end
31
-
32
- ActiveRecord::Base.connection.schema_cache.clear!
33
- [Section, Item].each(&:reset_column_information)
34
- super
35
- end
36
-
37
- def teardown
38
- teardown_db
39
- super
40
- end
41
- end
42
-
43
- # joining the relation returned by `#higher_items` or `#lower_items` to another table
44
- # previously could result in ambiguous column names in the query
45
- class TestHigherLowerItems < JoinedTestCase
46
- def test_higher_items
47
- section = Section.create
48
- item1 = Item.create section: section
49
- item2 = Item.create section: section
50
- item3 = Item.create section: section
51
- assert_equal item3.higher_items.visible, [item2, item1]
52
- end
53
-
54
- def test_lower_items
55
- section = Section.create
56
- item1 = Item.create section: section
57
- item2 = Item.create section: section
58
- item3 = Item.create section: section
59
- assert_equal item1.lower_items.visible, [item2, item3]
60
- end
61
- end