suhovius-acts_as_list 0.7.2.s
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gemtest +0 -0
- data/.gitignore +9 -0
- data/.travis.yml +11 -0
- data/Appraisals +11 -0
- data/Gemfile +20 -0
- data/MIT-LICENSE +19 -0
- data/README.md +119 -0
- data/Rakefile +34 -0
- data/acts_as_list.gemspec +29 -0
- data/gemfiles/rails_3_2.gemfile +21 -0
- data/gemfiles/rails_3_2.gemfile.lock +258 -0
- data/gemfiles/rails_4_1.gemfile +21 -0
- data/gemfiles/rails_4_1.gemfile.lock +262 -0
- data/gemfiles/rails_4_2.gemfile +21 -0
- data/gemfiles/rails_4_2.gemfile.lock +53 -0
- data/init.rb +2 -0
- data/lib/acts_as_list.rb +15 -0
- data/lib/acts_as_list/active_record/acts/list.rb +523 -0
- data/lib/acts_as_list/version.rb +7 -0
- data/test/helper.rb +14 -0
- data/test/shared.rb +9 -0
- data/test/shared_array_scope_list.rb +160 -0
- data/test/shared_list.rb +250 -0
- data/test/shared_list_sub.rb +127 -0
- data/test/shared_no_addition.rb +25 -0
- data/test/shared_top_addition.rb +87 -0
- data/test/shared_zero_based.rb +91 -0
- data/test/test_list.rb +570 -0
- metadata +118 -0
data/test/helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require "active_record"
|
11
|
+
require "minitest/autorun"
|
12
|
+
require "#{File.dirname(__FILE__)}/../init"
|
13
|
+
|
14
|
+
require "shared"
|
data/test/shared.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# Common shared behaviour.
|
2
|
+
module Shared
|
3
|
+
autoload :List, 'shared_list'
|
4
|
+
autoload :ListSub, 'shared_list_sub'
|
5
|
+
autoload :ZeroBased, 'shared_zero_based'
|
6
|
+
autoload :ArrayScopeList, 'shared_array_scope_list'
|
7
|
+
autoload :TopAddition, 'shared_top_addition'
|
8
|
+
autoload :NoAddition, 'shared_no_addition'
|
9
|
+
end
|
@@ -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.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
10
|
+
|
11
|
+
ArrayScopeListMixin.where(id: 2).first.move_lower
|
12
|
+
assert_equal [1, 3, 2, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
13
|
+
|
14
|
+
ArrayScopeListMixin.where(id: 2).first.move_higher
|
15
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
16
|
+
|
17
|
+
ArrayScopeListMixin.where(id: 1).first.move_to_bottom
|
18
|
+
assert_equal [2, 3, 4, 1], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
19
|
+
|
20
|
+
ArrayScopeListMixin.where(id: 1).first.move_to_top
|
21
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
22
|
+
|
23
|
+
ArrayScopeListMixin.where(id: 2).first.move_to_bottom
|
24
|
+
assert_equal [1, 3, 4, 2], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
25
|
+
|
26
|
+
ArrayScopeListMixin.where(id: 4).first.move_to_top
|
27
|
+
assert_equal [4, 1, 3, 2], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
28
|
+
|
29
|
+
ArrayScopeListMixin.where(id: 4).first.insert_at(4)
|
30
|
+
assert_equal [1, 3, 2, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
31
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, 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.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
36
|
+
ArrayScopeListMixin.where(id: 3).first.move_to_bottom
|
37
|
+
assert_equal [1, 2, 4, 3], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_next_prev
|
41
|
+
assert_equal ArrayScopeListMixin.where(id: 2).first, ArrayScopeListMixin.where(id: 1).first.lower_item
|
42
|
+
assert_nil ArrayScopeListMixin.where(id: 1).first.higher_item
|
43
|
+
assert_equal ArrayScopeListMixin.where(id: 3).first, ArrayScopeListMixin.where(id: 4).first.higher_item
|
44
|
+
assert_nil ArrayScopeListMixin.where(id: 4).first.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.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
111
|
+
|
112
|
+
ArrayScopeListMixin.where(id: 2).first.destroy
|
113
|
+
|
114
|
+
assert_equal [1, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
115
|
+
|
116
|
+
assert_equal 1, ArrayScopeListMixin.where(id: 1).first.pos
|
117
|
+
assert_equal 2, ArrayScopeListMixin.where(id: 3).first.pos
|
118
|
+
assert_equal 3, ArrayScopeListMixin.where(id: 4).first.pos
|
119
|
+
|
120
|
+
ArrayScopeListMixin.where(id: 1).first.destroy
|
121
|
+
|
122
|
+
assert_equal [3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
123
|
+
|
124
|
+
assert_equal 1, ArrayScopeListMixin.where(id: 3).first.pos
|
125
|
+
assert_equal 2, ArrayScopeListMixin.where(id: 4).first.pos
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_remove_from_list_should_then_fail_in_list?
|
129
|
+
assert_equal true, ArrayScopeListMixin.where(id: 1).first.in_list?
|
130
|
+
ArrayScopeListMixin.where(id: 1).first.remove_from_list
|
131
|
+
assert_equal false, ArrayScopeListMixin.where(id: 1).first.in_list?
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_remove_from_list_should_set_position_to_nil
|
135
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
136
|
+
|
137
|
+
ArrayScopeListMixin.where(id: 2).first.remove_from_list
|
138
|
+
|
139
|
+
assert_equal [2, 1, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
140
|
+
|
141
|
+
assert_equal 1, ArrayScopeListMixin.where(id: 1).first.pos
|
142
|
+
assert_equal nil, ArrayScopeListMixin.where(id: 2).first.pos
|
143
|
+
assert_equal 2, ArrayScopeListMixin.where(id: 3).first.pos
|
144
|
+
assert_equal 3, ArrayScopeListMixin.where(id: 4).first.pos
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_remove_before_destroy_does_not_shift_lower_items_twice
|
148
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
149
|
+
|
150
|
+
ArrayScopeListMixin.where(id: 2).first.remove_from_list
|
151
|
+
ArrayScopeListMixin.where(id: 2).first.destroy
|
152
|
+
|
153
|
+
assert_equal [1, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: 'ParentClass').order('pos').map(&:id)
|
154
|
+
|
155
|
+
assert_equal 1, ArrayScopeListMixin.where(id: 1).first.pos
|
156
|
+
assert_equal 2, ArrayScopeListMixin.where(id: 3).first.pos
|
157
|
+
assert_equal 3, ArrayScopeListMixin.where(id: 4).first.pos
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
data/test/shared_list.rb
ADDED
@@ -0,0 +1,250 @@
|
|
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.where(parent_id: 5).order('pos').map(&:id)
|
13
|
+
|
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
|
+
|
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
|
+
|
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
|
+
|
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
|
+
|
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
|
+
|
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
|
+
end
|
32
|
+
|
33
|
+
def test_move_to_bottom_with_next_to_last_item
|
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
|
+
end
|
38
|
+
|
39
|
+
def test_next_prev
|
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
|
+
end
|
45
|
+
|
46
|
+
def test_injection
|
47
|
+
item = ListMixin.new(parent_id: 1)
|
48
|
+
assert_equal({ 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.where(parent_id: 5).order('pos').map(&:id)
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
ListMixin.where(id: 2).first.destroy
|
115
|
+
|
116
|
+
assert_equal [1, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
117
|
+
|
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
|
121
|
+
|
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
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_with_string_based_scope
|
131
|
+
new = ListWithStringScopeMixin.create(parent_id: 500)
|
132
|
+
assert_equal 1, new.pos
|
133
|
+
assert new.first?
|
134
|
+
assert new.last?
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_nil_scope
|
138
|
+
new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create
|
139
|
+
new2.move_higher
|
140
|
+
assert_equal [new2, new1, new3].map(&:id), ListMixin.where(parent_id: nil).order('pos').map(&:id)
|
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(parent_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, 3, 4, 2], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_remove_from_list_should_then_fail_in_list?
|
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?
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_remove_from_list_should_set_position_to_nil
|
168
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
169
|
+
|
170
|
+
ListMixin.where(id: 2).first.remove_from_list
|
171
|
+
|
172
|
+
assert_equal [2, 1, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
173
|
+
|
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
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_remove_before_destroy_does_not_shift_lower_items_twice
|
181
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
182
|
+
|
183
|
+
ListMixin.where(id: 2).first.remove_from_list
|
184
|
+
ListMixin.where(id: 2).first.destroy
|
185
|
+
|
186
|
+
assert_equal [1, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
187
|
+
|
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
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_before_destroy_callbacks_do_not_update_position_to_nil_before_deleting_the_record
|
194
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
195
|
+
|
196
|
+
# We need to trigger all the before_destroy callbacks without actually
|
197
|
+
# destroying the record so we can see the affect the callbacks have on
|
198
|
+
# the record.
|
199
|
+
# NOTE: Hotfix for rails3 ActiveRecord
|
200
|
+
list = ListMixin.where(id: 2).first
|
201
|
+
if list.respond_to?(:run_callbacks)
|
202
|
+
# Refactored to work according to Rails3 ActiveRSupport Callbacks <http://api.rubyonrails.org/classes/ActiveSupport/Callbacks.html>
|
203
|
+
list.run_callbacks(:destroy) if rails_3
|
204
|
+
list.run_callbacks(:before_destroy) if !rails_3
|
205
|
+
else
|
206
|
+
list.send(:callback, :before_destroy)
|
207
|
+
end
|
208
|
+
|
209
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
210
|
+
|
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
|
215
|
+
end
|
216
|
+
|
217
|
+
def test_before_create_callback_adds_to_bottom
|
218
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
219
|
+
|
220
|
+
new = ListMixin.create(parent_id: 5)
|
221
|
+
assert_equal 5, new.pos
|
222
|
+
assert !new.first?
|
223
|
+
assert new.last?
|
224
|
+
|
225
|
+
assert_equal [1, 2, 3, 4, 5], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
226
|
+
end
|
227
|
+
|
228
|
+
def test_before_create_callback_adds_to_given_position
|
229
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
230
|
+
|
231
|
+
new = ListMixin.new(parent_id: 5)
|
232
|
+
new.pos = 1
|
233
|
+
new.save!
|
234
|
+
assert_equal 1, new.pos
|
235
|
+
assert new.first?
|
236
|
+
assert !new.last?
|
237
|
+
|
238
|
+
assert_equal [5, 1, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
239
|
+
|
240
|
+
new = ListMixin.new(parent_id: 5)
|
241
|
+
new.pos = 3
|
242
|
+
new.save!
|
243
|
+
assert_equal 3, new.pos
|
244
|
+
assert !new.first?
|
245
|
+
assert !new.last?
|
246
|
+
|
247
|
+
assert_equal [5, 1, 6, 2, 3, 4], ListMixin.where(parent_id: 5).order('pos').map(&:id)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
@@ -0,0 +1,127 @@
|
|
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.where(parent_id: 5000).order('pos').map(&:id)
|
13
|
+
|
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
|
+
|
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
|
+
|
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
|
+
|
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
|
+
|
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
|
+
|
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
|
+
end
|
32
|
+
|
33
|
+
def test_move_to_bottom_with_next_to_last_item
|
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
|
+
end
|
38
|
+
|
39
|
+
def test_next_prev
|
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
|
+
end
|
45
|
+
|
46
|
+
def test_next_prev_groups
|
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
|
+
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({ 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.where(parent_id: 5000).order('pos').map(&:id)
|
105
|
+
|
106
|
+
ListMixin.where(id: 2).first.destroy
|
107
|
+
|
108
|
+
assert_equal [1, 3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
|
109
|
+
|
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
|
+
|
114
|
+
ListMixin.where(id: 1).first.destroy
|
115
|
+
|
116
|
+
assert_equal [3, 4], ListMixin.where(parent_id: 5000).order('pos').map(&:id)
|
117
|
+
|
118
|
+
assert_equal 1, ListMixin.where(id: 3).first.pos
|
119
|
+
assert_equal 2, ListMixin.where(id: 4).first.pos
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_acts_as_list_class
|
123
|
+
assert_equal TheBaseClass, TheBaseSubclass.new.acts_as_list_class
|
124
|
+
assert_equal TheAbstractSubclass, TheAbstractSubclass.new.acts_as_list_class
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|