mongoid_orderable 6.0.4 → 6.0.5

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.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -3
  3. data/lib/mongoid/orderable/configs/field_config.rb +1 -1
  4. data/lib/mongoid/orderable/generators/base.rb +1 -1
  5. data/lib/mongoid/orderable/generators/listable.rb +1 -1
  6. data/lib/mongoid/orderable/generators/lock_collection.rb +1 -1
  7. data/lib/mongoid/orderable/generators/movable.rb +1 -1
  8. data/lib/mongoid/orderable/generators/position.rb +3 -3
  9. data/lib/mongoid/orderable/generators/scope.rb +1 -1
  10. data/lib/mongoid/orderable/handlers/base.rb +32 -6
  11. data/lib/mongoid/orderable/handlers/document.rb +9 -1
  12. data/lib/mongoid/orderable/handlers/document_transactional.rb +3 -6
  13. data/lib/mongoid/orderable/handlers/transaction.rb +6 -2
  14. data/lib/mongoid/orderable/installer.rb +1 -1
  15. data/lib/mongoid/orderable/mixins/callbacks.rb +3 -1
  16. data/lib/mongoid/orderable/mixins/movable.rb +7 -3
  17. data/lib/mongoid/orderable/version.rb +1 -1
  18. data/lib/mongoid_orderable.rb +1 -2
  19. metadata +5 -92
  20. data/spec/integration/cascadeable_spec.rb +0 -12
  21. data/spec/integration/concurrency_spec.rb +0 -232
  22. data/spec/integration/conditional_spec.rb +0 -36
  23. data/spec/integration/customized_spec.rb +0 -31
  24. data/spec/integration/embedded_spec.rb +0 -63
  25. data/spec/integration/foreign_key_spec.rb +0 -33
  26. data/spec/integration/inherited_spec.rb +0 -54
  27. data/spec/integration/multiple_fields_spec.rb +0 -554
  28. data/spec/integration/multiple_scoped_spec.rb +0 -63
  29. data/spec/integration/no_indexed_spec.rb +0 -23
  30. data/spec/integration/scoped_spec.rb +0 -151
  31. data/spec/integration/simple_spec.rb +0 -184
  32. data/spec/integration/string_scoped_spec.rb +0 -28
  33. data/spec/integration/zero_based_spec.rb +0 -161
  34. data/spec/spec_helper.rb +0 -42
  35. data/spec/support/models.rb +0 -134
@@ -1,232 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'concurrency' do
4
- enable_transactions!
5
-
6
- describe 'simple create' do
7
-
8
- it 'should correctly insert at the top' do
9
- 20.times.map do
10
- Thread.new do
11
- SimpleOrderable.create!(move_to: :top)
12
- end
13
- end.each(&:join)
14
-
15
- expect(SimpleOrderable.pluck(:position).sort).to eq((1..20).to_a)
16
- end
17
-
18
- it 'should correctly insert at the bottom' do
19
- 20.times.map do
20
- Thread.new do
21
- SimpleOrderable.create!
22
- end
23
- end.each(&:join)
24
-
25
- expect(SimpleOrderable.pluck(:position).sort).to eq((1..20).to_a)
26
- end
27
-
28
- it 'should correctly insert at a random position' do
29
- 20.times.map do
30
- Thread.new do
31
- SimpleOrderable.create!(move_to: (1..10).to_a.sample)
32
- end
33
- end.each(&:join)
34
-
35
- expect(SimpleOrderable.pluck(:position).sort).to eq((1..20).to_a)
36
- end
37
- end
38
-
39
- describe 'simple update' do
40
- before :each do
41
- 5.times { SimpleOrderable.create! }
42
- end
43
-
44
- it 'should correctly move items to top' do
45
- 20.times.map do
46
- Thread.new do
47
- record = SimpleOrderable.all.sample
48
- record.update_attributes move_to: :top
49
- end
50
- end.each(&:join)
51
-
52
- expect(SimpleOrderable.pluck(:position).sort).to eq([1, 2, 3, 4, 5])
53
- end
54
-
55
- it 'should correctly move items to bottom' do
56
- 20.times.map do
57
- Thread.new do
58
- record = SimpleOrderable.all.sample
59
- record.update_attributes move_to: :bottom
60
- end
61
- end.each(&:join)
62
-
63
- expect(SimpleOrderable.pluck(:position).sort).to eq([1, 2, 3, 4, 5])
64
- end
65
-
66
- it 'should correctly move items higher' do
67
- 20.times.map do
68
- Thread.new do
69
- record = SimpleOrderable.all.sample
70
- record.update_attributes move_to: :higher
71
- end
72
- end.each(&:join)
73
-
74
- expect(SimpleOrderable.pluck(:position).sort).to eq([1, 2, 3, 4, 5])
75
- end
76
-
77
- it 'should correctly move items lower' do
78
- 20.times.map do
79
- Thread.new do
80
- record = SimpleOrderable.all.sample
81
- record.update_attributes move_to: :lower
82
- end
83
- end.each(&:join)
84
-
85
- expect(SimpleOrderable.pluck(:position).sort).to eq([1, 2, 3, 4, 5])
86
- end
87
-
88
- it 'should correctly insert at the top' do
89
- 20.times.map do
90
- Thread.new do
91
- SimpleOrderable.create!(move_to: :top)
92
- end
93
- end.each(&:join)
94
-
95
- expect(SimpleOrderable.pluck(:position).sort).to eq((1..25).to_a)
96
- end
97
-
98
- it 'should correctly insert at the bottom' do
99
- 20.times.map do
100
- Thread.new do
101
- SimpleOrderable.create!
102
- end
103
- end.each(&:join)
104
-
105
- expect(SimpleOrderable.pluck(:position).sort).to eq((1..25).to_a)
106
- end
107
-
108
- it 'should correctly insert at a random position' do
109
- 20.times.map do
110
- Thread.new do
111
- SimpleOrderable.create!(move_to: (1..10).to_a.sample)
112
- end
113
- end.each(&:join)
114
-
115
- expect(SimpleOrderable.pluck(:position).sort).to eq((1..25).to_a)
116
- end
117
-
118
- it 'should correctly move items to a random position' do
119
- 20.times.map do
120
- Thread.new do
121
- record = SimpleOrderable.all.sample
122
- record.update_attributes move_to: (1..5).to_a.sample
123
- end
124
- end.each(&:join)
125
-
126
- expect(SimpleOrderable.pluck(:position).sort).to eq([1, 2, 3, 4, 5])
127
- end
128
- end
129
-
130
- describe 'scoped update' do
131
-
132
- before :each do
133
- 2.times { ScopedOrderable.create! group_id: 1 }
134
- 3.times { ScopedOrderable.create! group_id: 2 }
135
- end
136
-
137
- it 'should correctly move items to top' do
138
- 20.times.map do
139
- Thread.new do
140
- record = ScopedOrderable.all.sample
141
- record.update_attributes move_to: :top
142
- end
143
- end.each(&:join)
144
-
145
- expect(ScopedOrderable.pluck(:position).sort).to eq([1, 1, 2, 2, 3])
146
- end
147
-
148
- it 'should correctly move items to bottom' do
149
- 20.times.map do
150
- Thread.new do
151
- record = ScopedOrderable.all.sample
152
- record.update_attributes move_to: :bottom
153
- end
154
- end.each(&:join)
155
-
156
- expect(ScopedOrderable.pluck(:position).sort).to eq([1, 1, 2, 2, 3])
157
- end
158
-
159
- it 'should correctly move items higher' do
160
- 20.times.map do
161
- Thread.new do
162
- record = ScopedOrderable.all.sample
163
- record.update_attributes move_to: :higher
164
- end
165
- end.each(&:join)
166
-
167
- expect(ScopedOrderable.pluck(:position).sort).to eq([1, 1, 2, 2, 3])
168
- end
169
-
170
- it 'should correctly move items lower' do
171
- 20.times.map do
172
- Thread.new do
173
- record = ScopedOrderable.all.sample
174
- record.update_attributes move_to: :lower
175
- end
176
- end.each(&:join)
177
-
178
- expect(ScopedOrderable.pluck(:position).sort).to eq([1, 1, 2, 2, 3])
179
- end
180
-
181
- it 'should correctly move items to a random position' do
182
- 20.times.map do
183
- Thread.new do
184
- record = ScopedOrderable.all.sample
185
- record.update_attributes move_to: (1..5).to_a.sample
186
- end
187
- end.each(&:join)
188
-
189
- expect(ScopedOrderable.pluck(:position).sort).to eq([1, 1, 2, 2, 3])
190
- end
191
-
192
- # This spec fails randomly
193
- it 'should correctly move items to a random scope', retry: 5 do
194
- 20.times.map do
195
- Thread.new do
196
- record = ScopedOrderable.all.sample
197
- group_id = ([1, 2, 3] - [record.group_id]).sample
198
- record.update_attributes group_id: group_id
199
- end
200
- end.each(&:join)
201
-
202
- result = ScopedOrderable.all.to_a.each_with_object({}) do |obj, hash|
203
- hash[obj.group_id] ||= []
204
- hash[obj.group_id] << obj.position
205
- end
206
-
207
- result.values.each do |ary|
208
- expect(ary.sort).to eq((1..(ary.size)).to_a)
209
- end
210
- end
211
-
212
- it 'should correctly move items to a random position and scope' do
213
- 20.times.map do
214
- Thread.new do
215
- record = ScopedOrderable.all.sample
216
- group_id = ([1, 2, 3] - [record.group_id]).sample
217
- position = (1..5).to_a.sample
218
- record.update_attributes group_id: group_id, move_to: position
219
- end
220
- end.each(&:join)
221
-
222
- result = ScopedOrderable.all.to_a.each_with_object({}) do |obj, hash|
223
- hash[obj.group_id] ||= []
224
- hash[obj.group_id] << obj.position
225
- end
226
-
227
- result.values.each do |ary|
228
- expect(ary.sort).to eq((1..(ary.size)).to_a)
229
- end
230
- end
231
- end
232
- end
@@ -1,36 +0,0 @@
1
- require_relative '../spec_helper'
2
-
3
- describe ConditionalOrderable do
4
-
5
- shared_examples_for 'conditional_orderable' do
6
-
7
- before :each do
8
- ConditionalOrderable.create!(cond_a: false, cond_b: nil)
9
- ConditionalOrderable.create!(cond_a: false, cond_b: 1)
10
- ConditionalOrderable.create!(cond_a: true, cond_b: 2)
11
- ConditionalOrderable.create!(cond_a: false, cond_b: 3)
12
- ConditionalOrderable.create!(cond_a: true, cond_b: 4)
13
- ConditionalOrderable.create!(cond_a: true, cond_b: 5)
14
- end
15
-
16
- it 'should have proper position field' do
17
- orderables = ConditionalOrderable.all.sort_by {|x| x.cond_b || 0 }
18
-
19
- expect(orderables.map(&:pos_a)).to eq [nil, nil, 1, nil, 2, 3]
20
- expect(orderables.map(&:pos_b)).to eq [nil, 1, 2, 3, 4, nil]
21
- expect(orderables.map(&:pos_c)).to eq [1, 2, 3, 4, 5, 6]
22
- end
23
- end
24
-
25
- context 'with transactions' do
26
- enable_transactions!
27
-
28
- it_behaves_like 'conditional_orderable'
29
- end
30
-
31
- context 'without transactions' do
32
- disable_transactions!
33
-
34
- it_behaves_like 'conditional_orderable'
35
- end
36
- end
@@ -1,31 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe CustomizedOrderable do
4
-
5
- shared_examples_for 'customized_orderable' do
6
-
7
- it 'does not have default position field' do
8
- expect(CustomizedOrderable.fields).not_to have_key('position')
9
- end
10
-
11
- it 'should have custom pos field' do
12
- expect(CustomizedOrderable.fields).to have_key('pos')
13
- end
14
-
15
- it 'should have an alias my_position which points to pos field on Mongoid 3+' do
16
- expect(CustomizedOrderable.database_field_name('my_position')).to eq('pos')
17
- end
18
- end
19
-
20
- context 'with transactions' do
21
- enable_transactions!
22
-
23
- it_behaves_like 'customized_orderable'
24
- end
25
-
26
- context 'without transactions' do
27
- disable_transactions!
28
-
29
- it_behaves_like 'customized_orderable'
30
- end
31
- end
@@ -1,63 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe EmbeddedOrderable do
4
-
5
- shared_examples_for 'embedded_orderable' do
6
-
7
- before :each do
8
- eo = EmbedsOrderable.create!
9
- 2.times { eo.embedded_orderables.create! }
10
- eo = EmbedsOrderable.create!
11
- 3.times { eo.embedded_orderables.create! }
12
- end
13
-
14
- def positions
15
- EmbedsOrderable.order_by(position: 1).all.map { |eo| eo.embedded_orderables.map(&:position).sort }
16
- end
17
-
18
- it 'sets proper position while creation' do
19
- expect(positions).to eq([[1, 2], [1, 2, 3]])
20
- end
21
-
22
- it 'move_to! moves an item returned by a query to position' do
23
- parent = EmbedsOrderable.first
24
- child1 = parent.embedded_orderables.where(position: 1).first
25
- child2 = parent.embedded_orderables.where(position: 2).first
26
- child1.move_to!(2)
27
- expect(child1.reload.position).to eq(2)
28
- expect(child2.reload.position).to eq(1)
29
- end
30
-
31
- it 'move_to moves an item returned by a query to position when saving the parent' do
32
- parent = EmbedsOrderable.first
33
- child1 = parent.embedded_orderables.where(position: 1).first
34
- child2 = parent.embedded_orderables.where(position: 2).first
35
- child1.move_to(2)
36
- parent.save!
37
- expect(child1.reload.position).to eq(2)
38
- expect(child2.reload.position).to eq(1)
39
- end
40
-
41
- it 'move_to= moves an item returned by a query to position when saving the parent' do
42
- parent = EmbedsOrderable.first
43
- child1 = parent.embedded_orderables.where(position: 1).first
44
- child2 = parent.embedded_orderables.where(position: 2).first
45
- child1.move_to = 2
46
- parent.save!
47
- expect(child1.reload.position).to eq(2)
48
- expect(child2.reload.position).to eq(1)
49
- end
50
- end
51
-
52
- context 'with transactions' do
53
- enable_transactions!
54
-
55
- it_behaves_like 'embedded_orderable'
56
- end
57
-
58
- context 'without transactions' do
59
- disable_transactions!
60
-
61
- it_behaves_like 'embedded_orderable'
62
- end
63
- end
@@ -1,33 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ForeignKeyDiffersOrderable do
4
-
5
- shared_examples_for 'foreign_key_orderable' do
6
-
7
- it 'uses the foreign key of the relationship as scope' do
8
- orderable1, orderable2, orderable3 = nil
9
- parent_scope1 = ForeignKeyDiffersOrderable.create
10
- parent_scope2 = ForeignKeyDiffersOrderable.create
11
- expect do
12
- orderable1 = ForeignKeyDiffersOrderable.create!(different_scope: parent_scope1)
13
- orderable2 = ForeignKeyDiffersOrderable.create!(different_scope: parent_scope1)
14
- orderable3 = ForeignKeyDiffersOrderable.create!(different_scope: parent_scope2)
15
- end.to_not raise_error
16
- expect(orderable1.position).to eq 1
17
- expect(orderable2.position).to eq 2
18
- expect(orderable3.position).to eq 1
19
- end
20
- end
21
-
22
- context 'with transactions' do
23
- enable_transactions!
24
-
25
- it_behaves_like 'foreign_key_orderable'
26
- end
27
-
28
- context 'without transactions' do
29
- disable_transactions!
30
-
31
- it_behaves_like 'foreign_key_orderable'
32
- end
33
- end
@@ -1,54 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe InheritedOrderable do
4
-
5
- shared_examples_for 'inherited_orderable' do
6
-
7
- it 'should set proper position' do
8
- fruit1 = Apple.create
9
- fruit2 = Orange.create
10
- expect(fruit1.position).to eq(1)
11
- expect(fruit2.position).to eq(2)
12
- end
13
-
14
- describe 'movement' do
15
- before :each do
16
- 5.times { Apple.create! }
17
- end
18
-
19
- it 'with symbol position' do
20
- first_apple = Apple.asc(:_id).first
21
- top_pos = first_apple.position
22
- bottom_pos = Apple.asc(:_id).last.position
23
- expect { first_apple.move_to!(:bottom) }.to change(first_apple, :position).from(top_pos).to bottom_pos
24
- end
25
-
26
- it 'with point position' do
27
- first_apple = Apple.asc(:_id).first
28
- top_pos = first_apple.position
29
- bottom_pos = Apple.asc(:_id).last.position
30
- expect { first_apple.move_to!(bottom_pos) }.to change(first_apple, :position).from(top_pos).to bottom_pos
31
- end
32
- end
33
-
34
- describe 'add orderable configs in inherited class' do
35
- it 'does not affect the orderable configs of parent class and sibling class' do
36
- expect(InheritedOrderable.orderable_configs).not_to eq Apple.orderable_configs
37
- expect(Orange.orderable_configs).not_to eq Apple.orderable_configs
38
- expect(InheritedOrderable.orderable_configs).to eq Orange.orderable_configs
39
- end
40
- end
41
- end
42
-
43
- context 'with transactions' do
44
- enable_transactions!
45
-
46
- it_behaves_like 'inherited_orderable'
47
- end
48
-
49
- context 'without transactions' do
50
- disable_transactions!
51
-
52
- it_behaves_like 'inherited_orderable'
53
- end
54
- end