acts_as_list 0.2.0 → 0.3.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 +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_list.rb
CHANGED
@@ -2,86 +2,86 @@ module Shared
|
|
2
2
|
module List
|
3
3
|
def setup
|
4
4
|
(1..4).each do |counter|
|
5
|
-
node = ListMixin.new :
|
5
|
+
node = ListMixin.new parent_id: 5
|
6
6
|
node.pos = counter
|
7
7
|
node.save!
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_reordering
|
12
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
12
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
13
13
|
|
14
|
-
ListMixin.
|
15
|
-
assert_equal [1, 3, 2, 4], ListMixin.
|
14
|
+
ListMixin.where(id: 2).first.move_lower
|
15
|
+
assert_equal [1, 3, 2, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
16
16
|
|
17
|
-
ListMixin.
|
18
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
17
|
+
ListMixin.where(id: 2).first.move_higher
|
18
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
19
19
|
|
20
|
-
ListMixin.
|
21
|
-
assert_equal [2, 3, 4, 1], ListMixin.
|
20
|
+
ListMixin.where(id: 1).first.move_to_bottom
|
21
|
+
assert_equal [2, 3, 4, 1], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
22
22
|
|
23
|
-
ListMixin.
|
24
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
23
|
+
ListMixin.where(id: 1).first.move_to_top
|
24
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
25
25
|
|
26
|
-
ListMixin.
|
27
|
-
assert_equal [1, 3, 4, 2], ListMixin.
|
26
|
+
ListMixin.where(id: 2).first.move_to_bottom
|
27
|
+
assert_equal [1, 3, 4, 2], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
28
28
|
|
29
|
-
ListMixin.
|
30
|
-
assert_equal [4, 1, 3, 2], ListMixin.
|
29
|
+
ListMixin.where(id: 4).first.move_to_top
|
30
|
+
assert_equal [4, 1, 3, 2], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
31
31
|
end
|
32
32
|
|
33
33
|
def test_move_to_bottom_with_next_to_last_item
|
34
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
35
|
-
ListMixin.
|
36
|
-
assert_equal [1, 2, 4, 3], ListMixin.
|
34
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
35
|
+
ListMixin.where(id: 3).first.move_to_bottom
|
36
|
+
assert_equal [1, 2, 4, 3], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
37
37
|
end
|
38
38
|
|
39
39
|
def test_next_prev
|
40
|
-
assert_equal ListMixin.
|
41
|
-
assert_nil ListMixin.
|
42
|
-
assert_equal ListMixin.
|
43
|
-
assert_nil ListMixin.
|
40
|
+
assert_equal ListMixin.where(id: 2).first, ListMixin.where(id: 1).first.lower_item
|
41
|
+
assert_nil ListMixin.where(id: 1).first.higher_item
|
42
|
+
assert_equal ListMixin.where(id: 3).first, ListMixin.where(id: 4).first.higher_item
|
43
|
+
assert_nil ListMixin.where(id: 4).first.lower_item
|
44
44
|
end
|
45
45
|
|
46
46
|
def test_injection
|
47
|
-
item = ListMixin.new(:
|
47
|
+
item = ListMixin.new(parent_id: 1)
|
48
48
|
assert_equal '"mixins"."parent_id" = 1', item.scope_condition
|
49
49
|
assert_equal "pos", item.position_column
|
50
50
|
end
|
51
51
|
|
52
52
|
def test_insert
|
53
|
-
new = ListMixin.create(:
|
53
|
+
new = ListMixin.create(parent_id: 20)
|
54
54
|
assert_equal 1, new.pos
|
55
55
|
assert new.first?
|
56
56
|
assert new.last?
|
57
57
|
|
58
|
-
new = ListMixin.create(:
|
58
|
+
new = ListMixin.create(parent_id: 20)
|
59
59
|
assert_equal 2, new.pos
|
60
60
|
assert !new.first?
|
61
61
|
assert new.last?
|
62
62
|
|
63
|
-
new = ListMixin.create(:
|
63
|
+
new = ListMixin.create(parent_id: 20)
|
64
64
|
assert_equal 3, new.pos
|
65
65
|
assert !new.first?
|
66
66
|
assert new.last?
|
67
67
|
|
68
|
-
new = ListMixin.create(:
|
68
|
+
new = ListMixin.create(parent_id: 0)
|
69
69
|
assert_equal 1, new.pos
|
70
70
|
assert new.first?
|
71
71
|
assert new.last?
|
72
72
|
end
|
73
73
|
|
74
74
|
def test_insert_at
|
75
|
-
new = ListMixin.create(:
|
75
|
+
new = ListMixin.create(parent_id: 20)
|
76
76
|
assert_equal 1, new.pos
|
77
77
|
|
78
|
-
new = ListMixin.create(:
|
78
|
+
new = ListMixin.create(parent_id: 20)
|
79
79
|
assert_equal 2, new.pos
|
80
80
|
|
81
|
-
new = ListMixin.create(:
|
81
|
+
new = ListMixin.create(parent_id: 20)
|
82
82
|
assert_equal 3, new.pos
|
83
83
|
|
84
|
-
new4 = ListMixin.create(:
|
84
|
+
new4 = ListMixin.create(parent_id: 20)
|
85
85
|
assert_equal 4, new4.pos
|
86
86
|
|
87
87
|
new4.insert_at(3)
|
@@ -96,7 +96,7 @@ module Shared
|
|
96
96
|
new4.reload
|
97
97
|
assert_equal 4, new4.pos
|
98
98
|
|
99
|
-
new5 = ListMixin.create(:
|
99
|
+
new5 = ListMixin.create(parent_id: 20)
|
100
100
|
assert_equal 5, new5.pos
|
101
101
|
|
102
102
|
new5.insert_at(1)
|
@@ -107,26 +107,28 @@ module Shared
|
|
107
107
|
end
|
108
108
|
|
109
109
|
def test_delete_middle
|
110
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
110
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
111
111
|
|
112
|
-
ListMixin.find(2).destroy
|
113
112
|
|
114
|
-
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
115
113
|
|
116
|
-
|
117
|
-
assert_equal 2, ListMixin.find(3).pos
|
118
|
-
assert_equal 3, ListMixin.find(4).pos
|
114
|
+
ListMixin.where(id: 2).first.destroy
|
119
115
|
|
120
|
-
ListMixin.
|
116
|
+
assert_equal [1, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
121
117
|
|
122
|
-
assert_equal
|
118
|
+
assert_equal 1, ListMixin.where(id: 1).first.pos
|
119
|
+
assert_equal 2, ListMixin.where(id: 3).first.pos
|
120
|
+
assert_equal 3, ListMixin.where(id: 4).first.pos
|
123
121
|
|
124
|
-
|
125
|
-
|
122
|
+
ListMixin.where(id: 1).first.destroy
|
123
|
+
|
124
|
+
assert_equal [3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
125
|
+
|
126
|
+
assert_equal 1, ListMixin.where(id: 3).first.pos
|
127
|
+
assert_equal 2, ListMixin.where(id: 4).first.pos
|
126
128
|
end
|
127
129
|
|
128
130
|
def test_with_string_based_scope
|
129
|
-
new = ListWithStringScopeMixin.create(:
|
131
|
+
new = ListWithStringScopeMixin.create(parent_id: 500)
|
130
132
|
assert_equal 1, new.pos
|
131
133
|
assert new.first?
|
132
134
|
assert new.last?
|
@@ -135,96 +137,114 @@ module Shared
|
|
135
137
|
def test_nil_scope
|
136
138
|
new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create
|
137
139
|
new2.move_higher
|
138
|
-
assert_equal [new2, new1, new3], ListMixin.
|
140
|
+
assert_equal [new2, new1, new3], ListMixin.where(parent_id: nil).order('pos')
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_update_position_when_scope_changes
|
144
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
145
|
+
parent = ListMixin.create(id: 6)
|
146
|
+
|
147
|
+
ListMixin.where(id: 2).first.move_within_scope(6)
|
148
|
+
|
149
|
+
assert_equal 2, ListMixin.where(id: 2).first.pos
|
150
|
+
|
151
|
+
assert_equal [1, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
152
|
+
|
153
|
+
assert_equal 1, ListMixin.where(id: 1).first.pos
|
154
|
+
assert_equal 2, ListMixin.where(id: 3).first.pos
|
155
|
+
assert_equal 3, ListMixin.where(id: 4).first.pos
|
156
|
+
|
157
|
+
ListMixin.where(id: 2).first.move_within_scope(5)
|
158
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
139
159
|
end
|
140
160
|
|
141
161
|
def test_remove_from_list_should_then_fail_in_list?
|
142
|
-
assert_equal true, ListMixin.
|
143
|
-
ListMixin.
|
144
|
-
assert_equal false, ListMixin.
|
162
|
+
assert_equal true, ListMixin.where(id: 1).first.in_list?
|
163
|
+
ListMixin.where(id: 1).first.remove_from_list
|
164
|
+
assert_equal false, ListMixin.where(id: 1).first.in_list?
|
145
165
|
end
|
146
166
|
|
147
167
|
def test_remove_from_list_should_set_position_to_nil
|
148
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
168
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
149
169
|
|
150
|
-
ListMixin.
|
170
|
+
ListMixin.where(id: 2).first.remove_from_list
|
151
171
|
|
152
|
-
assert_equal [2, 1, 3, 4], ListMixin.
|
172
|
+
assert_equal [2, 1, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
153
173
|
|
154
|
-
assert_equal 1, ListMixin.
|
155
|
-
assert_equal nil, ListMixin.
|
156
|
-
assert_equal 2, ListMixin.
|
157
|
-
assert_equal 3, ListMixin.
|
174
|
+
assert_equal 1, ListMixin.where(id: 1).first.pos
|
175
|
+
assert_equal nil, ListMixin.where(id: 2).first.pos
|
176
|
+
assert_equal 2, ListMixin.where(id: 3).first.pos
|
177
|
+
assert_equal 3, ListMixin.where(id: 4).first.pos
|
158
178
|
end
|
159
179
|
|
160
180
|
def test_remove_before_destroy_does_not_shift_lower_items_twice
|
161
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
181
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
162
182
|
|
163
|
-
ListMixin.
|
164
|
-
ListMixin.
|
183
|
+
ListMixin.where(id: 2).first.remove_from_list
|
184
|
+
ListMixin.where(id: 2).first.destroy
|
165
185
|
|
166
|
-
assert_equal [1, 3, 4], ListMixin.
|
186
|
+
assert_equal [1, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
167
187
|
|
168
|
-
assert_equal 1, ListMixin.
|
169
|
-
assert_equal 2, ListMixin.
|
170
|
-
assert_equal 3, ListMixin.
|
188
|
+
assert_equal 1, ListMixin.where(id: 1).first.pos
|
189
|
+
assert_equal 2, ListMixin.where(id: 3).first.pos
|
190
|
+
assert_equal 3, ListMixin.where(id: 4).first.pos
|
171
191
|
end
|
172
192
|
|
173
193
|
def test_before_destroy_callbacks_do_not_update_position_to_nil_before_deleting_the_record
|
174
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
194
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
175
195
|
|
176
196
|
# We need to trigger all the before_destroy callbacks without actually
|
177
197
|
# destroying the record so we can see the affect the callbacks have on
|
178
198
|
# the record.
|
179
199
|
# NOTE: Hotfix for rails3 ActiveRecord
|
180
|
-
list = ListMixin.
|
200
|
+
list = ListMixin.where(id: 2).first
|
181
201
|
if list.respond_to?(:run_callbacks)
|
182
202
|
# Refactored to work according to Rails3 ActiveRSupport Callbacks <http://api.rubyonrails.org/classes/ActiveSupport/Callbacks.html>
|
183
|
-
list.run_callbacks
|
203
|
+
list.run_callbacks(:destroy) if rails_3
|
184
204
|
list.run_callbacks(:before_destroy) if !rails_3
|
185
205
|
else
|
186
206
|
list.send(:callback, :before_destroy)
|
187
207
|
end
|
188
208
|
|
189
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
209
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
190
210
|
|
191
|
-
assert_equal 1, ListMixin.
|
192
|
-
assert_equal 2, ListMixin.
|
193
|
-
assert_equal 2, ListMixin.
|
194
|
-
assert_equal 3, ListMixin.
|
211
|
+
assert_equal 1, ListMixin.where(id: 1).first.pos
|
212
|
+
assert_equal 2, ListMixin.where(id: 2).first.pos
|
213
|
+
assert_equal 2, ListMixin.where(id: 3).first.pos
|
214
|
+
assert_equal 3, ListMixin.where(id: 4).first.pos
|
195
215
|
end
|
196
216
|
|
197
217
|
def test_before_create_callback_adds_to_bottom
|
198
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
218
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
199
219
|
|
200
|
-
new = ListMixin.create(:
|
220
|
+
new = ListMixin.create(parent_id: 5)
|
201
221
|
assert_equal 5, new.pos
|
202
222
|
assert !new.first?
|
203
223
|
assert new.last?
|
204
224
|
|
205
|
-
assert_equal [1, 2, 3, 4, 5], ListMixin.
|
225
|
+
assert_equal [1, 2, 3, 4, 5], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
206
226
|
end
|
207
227
|
|
208
228
|
def test_before_create_callback_adds_to_given_position
|
209
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
229
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
210
230
|
|
211
|
-
new = ListMixin.new(:
|
231
|
+
new = ListMixin.new(parent_id: 5)
|
212
232
|
new.pos = 1
|
213
233
|
new.save!
|
214
234
|
assert_equal 1, new.pos
|
215
235
|
assert new.first?
|
216
236
|
assert !new.last?
|
217
237
|
|
218
|
-
assert_equal [5, 1, 2, 3, 4], ListMixin.
|
238
|
+
assert_equal [5, 1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
219
239
|
|
220
|
-
new = ListMixin.new(:
|
240
|
+
new = ListMixin.new(parent_id: 5)
|
221
241
|
new.pos = 3
|
222
242
|
new.save!
|
223
243
|
assert_equal 3, new.pos
|
224
244
|
assert !new.first?
|
225
245
|
assert !new.last?
|
226
246
|
|
227
|
-
assert_equal [5, 1, 6, 2, 3, 4], ListMixin.
|
247
|
+
assert_equal [5, 1, 6, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
228
248
|
end
|
229
249
|
end
|
230
250
|
end
|
data/test/shared_list_sub.rb
CHANGED
@@ -2,52 +2,52 @@ module Shared
|
|
2
2
|
module ListSub
|
3
3
|
def setup
|
4
4
|
(1..4).each do |i|
|
5
|
-
node = ((i % 2 == 1) ? ListMixinSub1 : ListMixinSub2).new :
|
5
|
+
node = ((i % 2 == 1) ? ListMixinSub1 : ListMixinSub2).new parent_id: 5000
|
6
6
|
node.pos = i
|
7
7
|
node.save!
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_reordering
|
12
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
12
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
|
13
13
|
|
14
|
-
ListMixin.
|
15
|
-
assert_equal [1, 3, 2, 4], ListMixin.
|
14
|
+
ListMixin.where(id: 2).first.move_lower
|
15
|
+
assert_equal [1, 3, 2, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
|
16
16
|
|
17
|
-
ListMixin.
|
18
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
17
|
+
ListMixin.where(id: 2).first.move_higher
|
18
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
|
19
19
|
|
20
|
-
ListMixin.
|
21
|
-
assert_equal [2, 3, 4, 1], ListMixin.
|
20
|
+
ListMixin.where(id: 1).first.move_to_bottom
|
21
|
+
assert_equal [2, 3, 4, 1], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
|
22
22
|
|
23
|
-
ListMixin.
|
24
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
23
|
+
ListMixin.where(id: 1).first.move_to_top
|
24
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
|
25
25
|
|
26
|
-
ListMixin.
|
27
|
-
assert_equal [1, 3, 4, 2], ListMixin.
|
26
|
+
ListMixin.where(id: 2).first.move_to_bottom
|
27
|
+
assert_equal [1, 3, 4, 2], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
|
28
28
|
|
29
|
-
ListMixin.
|
30
|
-
assert_equal [4, 1, 3, 2], ListMixin.
|
29
|
+
ListMixin.where(id: 4).first.move_to_top
|
30
|
+
assert_equal [4, 1, 3, 2], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
|
31
31
|
end
|
32
32
|
|
33
33
|
def test_move_to_bottom_with_next_to_last_item
|
34
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
35
|
-
ListMixin.
|
36
|
-
assert_equal [1, 2, 4, 3], ListMixin.
|
34
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
|
35
|
+
ListMixin.where(id: 3).first.move_to_bottom
|
36
|
+
assert_equal [1, 2, 4, 3], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
|
37
37
|
end
|
38
38
|
|
39
39
|
def test_next_prev
|
40
|
-
assert_equal ListMixin.
|
41
|
-
assert_nil ListMixin.
|
42
|
-
assert_equal ListMixin.
|
43
|
-
assert_nil ListMixin.
|
40
|
+
assert_equal ListMixin.where(id: 2).first, ListMixin.where(id: 1).first.lower_item
|
41
|
+
assert_nil ListMixin.where(id: 1).first.higher_item
|
42
|
+
assert_equal ListMixin.where(id: 3).first, ListMixin.where(id: 4).first.higher_item
|
43
|
+
assert_nil ListMixin.where(id: 4).first.lower_item
|
44
44
|
end
|
45
45
|
|
46
46
|
def test_next_prev_groups
|
47
|
-
li1 = ListMixin.
|
48
|
-
li2 = ListMixin.
|
49
|
-
li3 = ListMixin.
|
50
|
-
li4 = ListMixin.
|
47
|
+
li1 = ListMixin.where(id: 1).first
|
48
|
+
li2 = ListMixin.where(id: 2).first
|
49
|
+
li3 = ListMixin.where(id: 3).first
|
50
|
+
li4 = ListMixin.where(id: 4).first
|
51
51
|
assert_equal [li2, li3, li4], li1.lower_items
|
52
52
|
assert_equal [li4], li3.lower_items
|
53
53
|
assert_equal [li2, li3], li1.lower_items(2)
|
@@ -101,22 +101,22 @@ module Shared
|
|
101
101
|
end
|
102
102
|
|
103
103
|
def test_delete_middle
|
104
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
104
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
|
105
105
|
|
106
|
-
ListMixin.
|
106
|
+
ListMixin.where(id: 2).first.destroy
|
107
107
|
|
108
|
-
assert_equal [1, 3, 4], ListMixin.
|
108
|
+
assert_equal [1, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
|
109
109
|
|
110
|
-
assert_equal 1, ListMixin.
|
111
|
-
assert_equal 2, ListMixin.
|
112
|
-
assert_equal 3, ListMixin.
|
110
|
+
assert_equal 1, ListMixin.where(id: 1).first.pos
|
111
|
+
assert_equal 2, ListMixin.where(id: 3).first.pos
|
112
|
+
assert_equal 3, ListMixin.where(id: 4).first.pos
|
113
113
|
|
114
|
-
ListMixin.
|
114
|
+
ListMixin.where(id: 1).first.destroy
|
115
115
|
|
116
|
-
assert_equal [3, 4], ListMixin.
|
116
|
+
assert_equal [3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
|
117
117
|
|
118
|
-
assert_equal 1, ListMixin.
|
119
|
-
assert_equal 2, ListMixin.
|
118
|
+
assert_equal 1, ListMixin.where(id: 3).first.pos
|
119
|
+
assert_equal 2, ListMixin.where(id: 4).first.pos
|
120
120
|
end
|
121
121
|
end
|
122
122
|
end
|
@@ -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
|
data/test/shared_top_addition.rb
CHANGED
@@ -1,70 +1,70 @@
|
|
1
1
|
module Shared
|
2
2
|
module TopAddition
|
3
3
|
def setup
|
4
|
-
(1..4).each { |counter| TopAdditionMixin.create! :
|
4
|
+
(1..4).each { |counter| TopAdditionMixin.create! pos: counter, parent_id: 5 }
|
5
5
|
end
|
6
6
|
|
7
7
|
def test_reordering
|
8
|
-
assert_equal [4, 3, 2, 1], TopAdditionMixin.
|
8
|
+
assert_equal [4, 3, 2, 1], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
|
9
9
|
|
10
|
-
TopAdditionMixin.
|
11
|
-
assert_equal [4, 3, 1, 2], TopAdditionMixin.
|
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
12
|
|
13
|
-
TopAdditionMixin.
|
14
|
-
assert_equal [4, 3, 2, 1], TopAdditionMixin.
|
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
15
|
|
16
|
-
TopAdditionMixin.
|
17
|
-
assert_equal [4, 3, 2, 1], TopAdditionMixin.
|
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
18
|
|
19
|
-
TopAdditionMixin.
|
20
|
-
assert_equal [1, 4, 3, 2], TopAdditionMixin.
|
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
21
|
|
22
|
-
TopAdditionMixin.
|
23
|
-
assert_equal [1, 4, 3, 2], TopAdditionMixin.
|
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
24
|
|
25
|
-
TopAdditionMixin.
|
26
|
-
assert_equal [4, 1, 3, 2], TopAdditionMixin.
|
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
27
|
end
|
28
28
|
|
29
29
|
def test_injection
|
30
|
-
item = TopAdditionMixin.new(:
|
30
|
+
item = TopAdditionMixin.new(parent_id: 1)
|
31
31
|
assert_equal '"mixins"."parent_id" = 1', item.scope_condition
|
32
32
|
assert_equal "pos", item.position_column
|
33
33
|
end
|
34
34
|
|
35
35
|
def test_insert
|
36
|
-
new = TopAdditionMixin.create(:
|
36
|
+
new = TopAdditionMixin.create(parent_id: 20)
|
37
37
|
assert_equal 1, new.pos
|
38
38
|
assert new.first?
|
39
39
|
assert new.last?
|
40
40
|
|
41
|
-
new = TopAdditionMixin.create(:
|
41
|
+
new = TopAdditionMixin.create(parent_id: 20)
|
42
42
|
assert_equal 1, new.pos
|
43
43
|
assert new.first?
|
44
44
|
assert !new.last?
|
45
45
|
|
46
|
-
new = TopAdditionMixin.create(:
|
46
|
+
new = TopAdditionMixin.create(parent_id: 20)
|
47
47
|
assert_equal 1, new.pos
|
48
48
|
assert new.first?
|
49
49
|
assert !new.last?
|
50
50
|
|
51
|
-
new = TopAdditionMixin.create(:
|
51
|
+
new = TopAdditionMixin.create(parent_id: 0)
|
52
52
|
assert_equal 1, new.pos
|
53
53
|
assert new.first?
|
54
54
|
assert new.last?
|
55
55
|
end
|
56
56
|
|
57
57
|
def test_insert_at
|
58
|
-
new = TopAdditionMixin.create(:
|
58
|
+
new = TopAdditionMixin.create(parent_id: 20)
|
59
59
|
assert_equal 1, new.pos
|
60
60
|
|
61
|
-
new = TopAdditionMixin.create(:
|
61
|
+
new = TopAdditionMixin.create(parent_id: 20)
|
62
62
|
assert_equal 1, new.pos
|
63
63
|
|
64
|
-
new = TopAdditionMixin.create(:
|
64
|
+
new = TopAdditionMixin.create(parent_id: 20)
|
65
65
|
assert_equal 1, new.pos
|
66
66
|
|
67
|
-
new4 = TopAdditionMixin.create(:
|
67
|
+
new4 = TopAdditionMixin.create(parent_id: 20)
|
68
68
|
assert_equal 1, new4.pos
|
69
69
|
|
70
70
|
new4.insert_at(3)
|
@@ -72,15 +72,15 @@ module Shared
|
|
72
72
|
end
|
73
73
|
|
74
74
|
def test_delete_middle
|
75
|
-
assert_equal [4, 3, 2, 1], TopAdditionMixin.
|
75
|
+
assert_equal [4, 3, 2, 1], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
|
76
76
|
|
77
|
-
TopAdditionMixin.
|
77
|
+
TopAdditionMixin.where(id: 2).first.destroy
|
78
78
|
|
79
|
-
assert_equal [4, 3, 1], TopAdditionMixin.
|
79
|
+
assert_equal [4, 3, 1], TopAdditionMixin.where(parent_id: 5).order('pos').map(&:id)
|
80
80
|
|
81
|
-
assert_equal 3, TopAdditionMixin.
|
82
|
-
assert_equal 2, TopAdditionMixin.
|
83
|
-
assert_equal 1, TopAdditionMixin.
|
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
84
|
end
|
85
85
|
|
86
86
|
end
|