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