acts_as_list 0.1.4 → 0.1.8

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