acts_as_list_ar 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +4 -0
- data/.gitignore +3 -0
- data/LICENSE +20 -0
- data/Notes.txt +16 -0
- data/README.rdoc +64 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/acts_as_list_ar.rb +326 -0
- data/lib/acts_as_list_ar/rails2.rb +79 -0
- data/lib/acts_as_list_ar/rails3.rb +282 -0
- data/spec/list_spec.rb +112 -0
- data/spec/spec_helper.rb +8 -0
- data/test/acts_as_list_test.rb +247 -0
- data/test/list_w_string_ids_test.rb +187 -0
- data/test/sub_list_test.rb +127 -0
- data/test/test_helper.rb +83 -0
- metadata +104 -0
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,247 @@
|
|
1
|
+
# Load the plugin's test_helper (Rails 2.x needs the path)
|
2
|
+
require 'test/unit'
|
3
|
+
begin
|
4
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
5
|
+
rescue LoadError
|
6
|
+
require 'test_helper'
|
7
|
+
end
|
8
|
+
|
9
|
+
class ListTest < ActiveSupport::TestCase
|
10
|
+
def setup
|
11
|
+
setup_db
|
12
|
+
(1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 5 }
|
13
|
+
end
|
14
|
+
|
15
|
+
def teardown
|
16
|
+
teardown_db
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_reordering
|
20
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
21
|
+
|
22
|
+
ListMixin.find(2).move_lower
|
23
|
+
assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
24
|
+
|
25
|
+
ListMixin.find(2).move_higher
|
26
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
27
|
+
|
28
|
+
ListMixin.find(1).move_to_bottom
|
29
|
+
assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
30
|
+
|
31
|
+
ListMixin.find(1).move_to_top
|
32
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
33
|
+
|
34
|
+
ListMixin.find(2).move_to_bottom
|
35
|
+
assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
36
|
+
|
37
|
+
ListMixin.find(4).move_to_top
|
38
|
+
assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_move_to_bottom_with_next_to_last_item
|
42
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
43
|
+
ListMixin.find(3).move_to_bottom
|
44
|
+
assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_next_prev
|
48
|
+
assert_equal ListMixin.find(2), ListMixin.find(1).lower_item
|
49
|
+
assert_nil ListMixin.find(1).higher_item
|
50
|
+
assert_equal ListMixin.find(3), ListMixin.find(4).higher_item
|
51
|
+
assert_nil ListMixin.find(4).lower_item
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_injection
|
55
|
+
item = ListMixin.new(:parent_id => 1)
|
56
|
+
assert_equal ["parent_id = 1"], item.scope_condition
|
57
|
+
assert_equal "pos", item.position_column
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_insert
|
61
|
+
new = ListMixin.create(:parent_id => 20)
|
62
|
+
# insert as last
|
63
|
+
assert_equal 1, new.pos
|
64
|
+
assert new.first?
|
65
|
+
assert new.last?
|
66
|
+
|
67
|
+
new = ListMixin.create(:parent_id => 20)
|
68
|
+
assert_equal 2, new.pos
|
69
|
+
assert !new.first?
|
70
|
+
assert new.last?
|
71
|
+
|
72
|
+
new = ListMixin.create(:parent_id => 20)
|
73
|
+
assert_equal 3, new.pos
|
74
|
+
assert !new.first?
|
75
|
+
assert new.last?
|
76
|
+
|
77
|
+
new = ListMixin.create(:parent_id => 0)
|
78
|
+
assert_equal 1, new.pos
|
79
|
+
assert new.first?
|
80
|
+
assert new.last?
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_insert_at
|
84
|
+
# insert as last
|
85
|
+
new = ListMixin.create(:parent_id => 20)
|
86
|
+
assert_equal 1, new.pos
|
87
|
+
|
88
|
+
new = ListMixin.create(:parent_id => 20)
|
89
|
+
assert_equal 2, new.pos
|
90
|
+
|
91
|
+
new = ListMixin.create(:parent_id => 20)
|
92
|
+
assert_equal 3, new.pos
|
93
|
+
|
94
|
+
new4 = ListMixin.create(:parent_id => 20)
|
95
|
+
assert_equal 4, new4.pos
|
96
|
+
|
97
|
+
new4.insert_at(3)
|
98
|
+
assert_equal 3, new4.pos
|
99
|
+
|
100
|
+
new.reload
|
101
|
+
assert_equal 4, new.pos
|
102
|
+
|
103
|
+
new.insert_at(2)
|
104
|
+
assert_equal 2, new.pos
|
105
|
+
|
106
|
+
new4.reload
|
107
|
+
assert_equal 4, new4.pos
|
108
|
+
|
109
|
+
new5 = ListMixin.create(:parent_id => 20)
|
110
|
+
assert_equal 5, new5.pos
|
111
|
+
|
112
|
+
new5.insert_at(1)
|
113
|
+
assert_equal 1, new5.pos
|
114
|
+
|
115
|
+
new4.reload
|
116
|
+
assert_equal 5, new4.pos
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_delete_middle
|
120
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
121
|
+
|
122
|
+
ListMixin.find(2).destroy
|
123
|
+
|
124
|
+
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
125
|
+
|
126
|
+
assert_equal 1, ListMixin.find(1).pos
|
127
|
+
assert_equal 2, ListMixin.find(3).pos
|
128
|
+
assert_equal 3, ListMixin.find(4).pos
|
129
|
+
|
130
|
+
ListMixin.find(1).destroy
|
131
|
+
|
132
|
+
assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
133
|
+
|
134
|
+
assert_equal 1, ListMixin.find(3).pos
|
135
|
+
assert_equal 2, ListMixin.find(4).pos
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_with_string_based_scope
|
139
|
+
new = ListWithStringScopeMixin.create(:parent_id => 500)
|
140
|
+
assert_equal 1, new.pos
|
141
|
+
assert new.first?
|
142
|
+
assert new.last?
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_nil_scope
|
146
|
+
new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create
|
147
|
+
new2.move_higher
|
148
|
+
assert_equal [new2, new1, new3], ListMixin.find(:all, :conditions => 'parent_id IS NULL', :order => 'pos')
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_remove_from_list_should_then_fail_in_list?
|
152
|
+
assert_equal true, ListMixin.find(1).in_list?
|
153
|
+
ListMixin.find(1).remove_from_list
|
154
|
+
assert_equal false, ListMixin.find(1).in_list?
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_remove_from_list_should_set_position_to_nil
|
158
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
159
|
+
|
160
|
+
ListMixin.find(2).remove_from_list
|
161
|
+
|
162
|
+
assert_equal [2, 1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
163
|
+
|
164
|
+
assert_equal 1, ListMixin.find(1).pos
|
165
|
+
assert_equal nil, ListMixin.find(2).pos
|
166
|
+
assert_equal 2, ListMixin.find(3).pos
|
167
|
+
assert_equal 3, ListMixin.find(4).pos
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_move_to_lower_in_list
|
171
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
172
|
+
item = ListMixin.find(2)
|
173
|
+
item.move_to(3)
|
174
|
+
assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
175
|
+
item.move_to(7)
|
176
|
+
assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
177
|
+
assert_equal 4, item.pos
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_move_to_higher_in_list
|
181
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
182
|
+
item = ListMixin.find(2)
|
183
|
+
item.move_to(1)
|
184
|
+
assert_equal [2, 1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
185
|
+
item.move_to(-2)
|
186
|
+
assert_equal [2, 1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
187
|
+
assert_equal 1, item.pos
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_remove_before_destroy_does_not_shift_lower_items_twice
|
191
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
192
|
+
|
193
|
+
ListMixin.find(2).remove_from_list
|
194
|
+
ListMixin.find(2).destroy
|
195
|
+
|
196
|
+
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
197
|
+
|
198
|
+
assert_equal 1, ListMixin.find(1).pos
|
199
|
+
assert_equal 2, ListMixin.find(3).pos
|
200
|
+
assert_equal 3, ListMixin.find(4).pos
|
201
|
+
end
|
202
|
+
|
203
|
+
def test_should_not_trigger_unexpected_callbacks_on_destroy
|
204
|
+
element = ListMixin.find(2)
|
205
|
+
assert !element.before_save_triggered
|
206
|
+
element.destroy
|
207
|
+
assert !element.before_save_triggered
|
208
|
+
end
|
209
|
+
|
210
|
+
# special thanks to openhood on github
|
211
|
+
def test_delete_middle_with_holes
|
212
|
+
# first we check everything is at expected place
|
213
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
214
|
+
|
215
|
+
# then we create a hole in the list, say you're working with existing data in which you already have holes
|
216
|
+
# or your scope is very complex
|
217
|
+
ListMixin.delete(2)
|
218
|
+
|
219
|
+
# we ensure the hole is really here
|
220
|
+
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
221
|
+
assert_equal 1, ListMixin.find(1).pos
|
222
|
+
assert_equal 3, ListMixin.find(3).pos
|
223
|
+
assert_equal 4, ListMixin.find(4).pos
|
224
|
+
|
225
|
+
# can we retrieve lower item despite the hole?
|
226
|
+
assert_equal 3, ListMixin.find(1).lower_item.id
|
227
|
+
|
228
|
+
# can we move an item lower jumping more than one position?
|
229
|
+
ListMixin.find(1).move_lower
|
230
|
+
assert_equal [3, 1, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
231
|
+
assert_equal 2, ListMixin.find(3).pos
|
232
|
+
assert_equal 3, ListMixin.find(1).pos
|
233
|
+
assert_equal 4, ListMixin.find(4).pos
|
234
|
+
|
235
|
+
# create another hole
|
236
|
+
ListMixin.delete(1)
|
237
|
+
|
238
|
+
# can we retrieve higher item despite the hole?
|
239
|
+
assert_equal 3, ListMixin.find(4).higher_item.id
|
240
|
+
|
241
|
+
# can we move an item higher jumping more than one position?
|
242
|
+
ListMixin.find(4).move_higher
|
243
|
+
assert_equal [4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
244
|
+
assert_equal 2, ListMixin.find(4).pos
|
245
|
+
assert_equal 3, ListMixin.find(3).pos
|
246
|
+
end
|
247
|
+
end
|
@@ -0,0 +1,187 @@
|
|
1
|
+
# Load the plugin's test_helper (Rails 2.x needs the path)
|
2
|
+
begin
|
3
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
4
|
+
rescue LoadError
|
5
|
+
require 'test_helper'
|
6
|
+
end
|
7
|
+
require 'test/helper'
|
8
|
+
|
9
|
+
class ListWithStringIdsTest < ActiveSupport::TestCase
|
10
|
+
|
11
|
+
def setup
|
12
|
+
setup_db
|
13
|
+
(1..4).each do |counter|
|
14
|
+
m = MixinWithStrings.new(:pos => counter, :parent_id => 5)
|
15
|
+
m.id = counter.to_s
|
16
|
+
m.save!
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def teardown
|
21
|
+
teardown_db
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_reordering
|
25
|
+
assert_equal [1, 2, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
26
|
+
|
27
|
+
MixinWithStrings.find(2).move_lower
|
28
|
+
assert_equal [1, 3, 2, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
29
|
+
|
30
|
+
MixinWithStrings.find(2).move_higher
|
31
|
+
assert_equal [1, 2, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
32
|
+
|
33
|
+
MixinWithStrings.find(1).move_to_bottom
|
34
|
+
assert_equal [2, 3, 4, 1], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
35
|
+
|
36
|
+
MixinWithStrings.find(1).move_to_top
|
37
|
+
assert_equal [1, 2, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
38
|
+
|
39
|
+
MixinWithStrings.find(2).move_to_bottom
|
40
|
+
assert_equal [1, 3, 4, 2], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
41
|
+
|
42
|
+
MixinWithStrings.find(4).move_to_top
|
43
|
+
assert_equal [4, 1, 3, 2], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_move_to_bottom_with_next_to_last_item
|
47
|
+
assert_equal [1, 2, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
48
|
+
MixinWithStrings.find(3).move_to_bottom
|
49
|
+
assert_equal [1, 2, 4, 3], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_next_prev
|
53
|
+
assert_equal MixinWithStrings.find(2), MixinWithStrings.find(1).lower_item
|
54
|
+
assert_nil MixinWithStrings.find(1).higher_item
|
55
|
+
assert_equal MixinWithStrings.find(3), MixinWithStrings.find(4).higher_item
|
56
|
+
assert_nil MixinWithStrings.find(4).lower_item
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_injection
|
60
|
+
item = MixinWithStrings.new(:parent_id => 1)
|
61
|
+
assert_equal ["parent_id = 1"], item.scope_condition
|
62
|
+
assert_equal "pos", item.position_column
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_insert
|
66
|
+
new = MixinWithStrings.create(:parent_id => 20)
|
67
|
+
assert_equal 1, new.pos
|
68
|
+
assert new.first?
|
69
|
+
assert new.last?
|
70
|
+
|
71
|
+
new = MixinWithStrings.create(:parent_id => 20)
|
72
|
+
assert_equal 2, new.pos
|
73
|
+
assert !new.first?
|
74
|
+
assert new.last?
|
75
|
+
|
76
|
+
new = MixinWithStrings.create(:parent_id => 20)
|
77
|
+
assert_equal 3, new.pos
|
78
|
+
assert !new.first?
|
79
|
+
assert new.last?
|
80
|
+
|
81
|
+
new = MixinWithStrings.create(:parent_id => 0)
|
82
|
+
assert_equal 1, new.pos
|
83
|
+
assert new.first?
|
84
|
+
assert new.last?
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_insert_at
|
88
|
+
new = MixinWithStrings.create(:parent_id => 20)
|
89
|
+
assert_equal 1, new.pos
|
90
|
+
|
91
|
+
new = MixinWithStrings.create(:parent_id => 20)
|
92
|
+
assert_equal 2, new.pos
|
93
|
+
|
94
|
+
new = MixinWithStrings.create(:parent_id => 20)
|
95
|
+
assert_equal 3, new.pos
|
96
|
+
|
97
|
+
new4 = MixinWithStrings.create(:parent_id => 20)
|
98
|
+
assert_equal 4, new4.pos
|
99
|
+
|
100
|
+
new4.insert_at(3)
|
101
|
+
assert_equal 3, new4.pos
|
102
|
+
|
103
|
+
new.reload
|
104
|
+
assert_equal 4, new.pos
|
105
|
+
|
106
|
+
new.insert_at(2)
|
107
|
+
assert_equal 2, new.pos
|
108
|
+
|
109
|
+
new4.reload
|
110
|
+
assert_equal 4, new4.pos
|
111
|
+
|
112
|
+
new5 = MixinWithStrings.create(:parent_id => 20)
|
113
|
+
assert_equal 5, new5.pos
|
114
|
+
|
115
|
+
new5.insert_at(1)
|
116
|
+
assert_equal 1, new5.pos
|
117
|
+
|
118
|
+
new4.reload
|
119
|
+
assert_equal 5, new4.pos
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_delete_middle
|
123
|
+
assert_equal [1, 2, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
124
|
+
|
125
|
+
MixinWithStrings.find(2).destroy
|
126
|
+
|
127
|
+
assert_equal [1, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
128
|
+
|
129
|
+
assert_equal 1, MixinWithStrings.find(1).pos
|
130
|
+
assert_equal 2, MixinWithStrings.find(3).pos
|
131
|
+
assert_equal 3, MixinWithStrings.find(4).pos
|
132
|
+
|
133
|
+
MixinWithStrings.find(1).destroy
|
134
|
+
|
135
|
+
assert_equal [3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
136
|
+
|
137
|
+
assert_equal 1, MixinWithStrings.find(3).pos
|
138
|
+
assert_equal 2, MixinWithStrings.find(4).pos
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_with_string_based_scope
|
142
|
+
new = ListWithStringScopeMixin.create(:parent_id => 500)
|
143
|
+
assert_equal 1, new.pos
|
144
|
+
assert new.first?
|
145
|
+
assert new.last?
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_nil_scope
|
149
|
+
new1, new2, new3 = MixinWithStrings.create, MixinWithStrings.create, MixinWithStrings.create
|
150
|
+
new2.move_higher
|
151
|
+
assert_equal [new2, new1, new3], MixinWithStrings.find(:all, :conditions => 'parent_id IS NULL', :order => 'pos')
|
152
|
+
end
|
153
|
+
|
154
|
+
|
155
|
+
def test_remove_from_list_should_then_fail_in_list?
|
156
|
+
assert_equal true, MixinWithStrings.find(1).in_list?
|
157
|
+
MixinWithStrings.find(1).remove_from_list
|
158
|
+
assert_equal false, MixinWithStrings.find(1).in_list?
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_remove_from_list_should_set_position_to_nil
|
162
|
+
assert_equal [1, 2, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
163
|
+
|
164
|
+
MixinWithStrings.find(2).remove_from_list
|
165
|
+
|
166
|
+
assert_equal [2, 1, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
167
|
+
|
168
|
+
assert_equal 1, MixinWithStrings.find(1).pos
|
169
|
+
assert_equal nil, MixinWithStrings.find(2).pos
|
170
|
+
assert_equal 2, MixinWithStrings.find(3).pos
|
171
|
+
assert_equal 3, MixinWithStrings.find(4).pos
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_remove_before_destroy_does_not_shift_lower_items_twice
|
175
|
+
assert_equal [1, 2, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
176
|
+
|
177
|
+
MixinWithStrings.find(2).remove_from_list
|
178
|
+
MixinWithStrings.find(2).destroy
|
179
|
+
|
180
|
+
assert_equal [1, 3, 4], MixinWithStrings.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
181
|
+
|
182
|
+
assert_equal 1, MixinWithStrings.find(1).pos
|
183
|
+
assert_equal 2, MixinWithStrings.find(3).pos
|
184
|
+
assert_equal 3, MixinWithStrings.find(4).pos
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# Load the plugin's test_helper (Rails 2.x needs the path)
|
2
|
+
begin
|
3
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
4
|
+
rescue LoadError
|
5
|
+
require 'test_helper'
|
6
|
+
end
|
7
|
+
require 'test/helper'
|
8
|
+
|
9
|
+
class ListSubTest < ActiveSupport::TestCase
|
10
|
+
|
11
|
+
def setup
|
12
|
+
setup_db
|
13
|
+
(1..4).each { |i| ((i % 2 == 1) ? ListMixinSub1 : ListMixinSub2).create! :pos => i, :parent_id => 5000 }
|
14
|
+
end
|
15
|
+
|
16
|
+
def teardown
|
17
|
+
teardown_db
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_reordering
|
21
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
22
|
+
|
23
|
+
ListMixin.find(2).move_lower
|
24
|
+
assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
25
|
+
|
26
|
+
ListMixin.find(2).move_higher
|
27
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
28
|
+
|
29
|
+
ListMixin.find(1).move_to_bottom
|
30
|
+
assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
31
|
+
|
32
|
+
ListMixin.find(1).move_to_top
|
33
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
34
|
+
|
35
|
+
ListMixin.find(2).move_to_bottom
|
36
|
+
assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
37
|
+
|
38
|
+
ListMixin.find(4).move_to_top
|
39
|
+
assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_move_to_bottom_with_next_to_last_item
|
43
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
44
|
+
ListMixin.find(3).move_to_bottom
|
45
|
+
assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_next_prev
|
49
|
+
assert_equal ListMixin.find(2), ListMixin.find(1).lower_item
|
50
|
+
assert_nil ListMixin.find(1).higher_item
|
51
|
+
assert_equal ListMixin.find(3), ListMixin.find(4).higher_item
|
52
|
+
assert_nil ListMixin.find(4).lower_item
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_sti
|
56
|
+
new = StiMixinSub1.create
|
57
|
+
assert_equal 1, new.position
|
58
|
+
new = StiMixinSub1.create
|
59
|
+
assert_equal 2, new.position
|
60
|
+
|
61
|
+
new = StiMixinSub2.create
|
62
|
+
assert_equal 1, new.position
|
63
|
+
new = StiMixinSub2.create
|
64
|
+
assert_equal 2, new.position
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_injection
|
68
|
+
item = ListMixin.new("parent_id" => 1)
|
69
|
+
assert_equal "parent_id = 1", item.scope_condition
|
70
|
+
assert_equal "pos", item.position_column
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_insert_at
|
74
|
+
new = ListMixin.create("parent_id" => 20)
|
75
|
+
assert_equal 1, new.pos
|
76
|
+
|
77
|
+
new = ListMixinSub1.create("parent_id" => 20)
|
78
|
+
assert_equal 2, new.pos
|
79
|
+
|
80
|
+
new = ListMixinSub2.create("parent_id" => 20)
|
81
|
+
assert_equal 3, new.pos
|
82
|
+
|
83
|
+
new4 = ListMixin.create("parent_id" => 20)
|
84
|
+
assert_equal 4, new4.pos
|
85
|
+
|
86
|
+
new4.insert_at(3)
|
87
|
+
assert_equal 3, new4.pos
|
88
|
+
|
89
|
+
new.reload
|
90
|
+
assert_equal 4, new.pos
|
91
|
+
|
92
|
+
new.insert_at(2)
|
93
|
+
assert_equal 2, new.pos
|
94
|
+
|
95
|
+
new4.reload
|
96
|
+
assert_equal 4, new4.pos
|
97
|
+
|
98
|
+
new5 = ListMixinSub1.create("parent_id" => 20)
|
99
|
+
assert_equal 5, new5.pos
|
100
|
+
|
101
|
+
new5.insert_at(1)
|
102
|
+
assert_equal 1, new5.pos
|
103
|
+
|
104
|
+
new4.reload
|
105
|
+
assert_equal 5, new4.pos
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_delete_middle
|
109
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
110
|
+
|
111
|
+
ListMixin.find(2).destroy
|
112
|
+
|
113
|
+
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
114
|
+
|
115
|
+
assert_equal 1, ListMixin.find(1).pos
|
116
|
+
assert_equal 2, ListMixin.find(3).pos
|
117
|
+
assert_equal 3, ListMixin.find(4).pos
|
118
|
+
|
119
|
+
ListMixin.find(1).destroy
|
120
|
+
|
121
|
+
assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
122
|
+
|
123
|
+
assert_equal 1, ListMixin.find(3).pos
|
124
|
+
assert_equal 2, ListMixin.find(4).pos
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|