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.
data/test/test_list.rb CHANGED
@@ -1,12 +1,16 @@
1
1
  # NOTE: following now done in helper.rb (better Readability)
2
- require 'helper.rb'
2
+ require 'helper'
3
3
 
4
4
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
5
+ ActiveRecord::Schema.verbose = false
5
6
 
6
- def setup_db
7
+ def setup_db(position_options = {})
8
+ # AR caches columns options like defaults etc. Clear them!
9
+ ActiveRecord::Base.connection.schema_cache.clear!
7
10
  ActiveRecord::Schema.define(:version => 1) do
8
11
  create_table :mixins do |t|
9
- t.column :pos, :integer
12
+ t.column :pos, :integer, position_options
13
+ t.column :active, :boolean, :default => true
10
14
  t.column :parent_id, :integer
11
15
  t.column :parent_type, :string
12
16
  t.column :created_at, :datetime
@@ -15,6 +19,10 @@ def setup_db
15
19
  end
16
20
  end
17
21
 
22
+ def setup_db_with_default
23
+ setup_db :default => 0
24
+ end
25
+
18
26
  # Returns true if ActiveRecord is rails3 version
19
27
  def rails_3
20
28
  defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR >= 3
@@ -27,624 +35,336 @@ def teardown_db
27
35
  end
28
36
 
29
37
  class Mixin < ActiveRecord::Base
38
+ self.table_name = 'mixins'
30
39
  end
31
40
 
32
41
  class ListMixin < Mixin
33
42
  acts_as_list :column => "pos", :scope => :parent
34
-
35
- def self.table_name() "mixins" end
36
43
  end
37
44
 
38
45
  class ListMixinSub1 < ListMixin
39
46
  end
40
47
 
41
48
  class ListMixinSub2 < ListMixin
49
+ if rails_3
50
+ validates :pos, :presence => true
51
+ else
52
+ validates_presence_of :pos
53
+ end
42
54
  end
43
55
 
44
- class ListWithStringScopeMixin < ActiveRecord::Base
56
+ class ListWithStringScopeMixin < Mixin
45
57
  acts_as_list :column => "pos", :scope => 'parent_id = #{parent_id}'
46
-
47
- def self.table_name() "mixins" end
48
58
  end
49
59
 
50
60
  class ArrayScopeListMixin < Mixin
51
61
  acts_as_list :column => "pos", :scope => [:parent_id, :parent_type]
52
-
53
- def self.table_name() "mixins" end
54
62
  end
55
63
 
56
- class ZeroBasedMixin < ActiveRecord::Base
64
+ class ZeroBasedMixin < Mixin
57
65
  acts_as_list :column => "pos", :top_of_list => 0, :scope => [:parent_id]
66
+ end
58
67
 
59
- def self.table_name() "mixins" end
68
+ class DefaultScopedMixin < Mixin
69
+ acts_as_list :column => "pos"
70
+ default_scope { order('pos ASC') }
60
71
  end
61
72
 
73
+ class DefaultScopedWhereMixin < Mixin
74
+ acts_as_list :column => "pos"
75
+ default_scope { order('pos ASC').where(:active => true) }
76
+ end
62
77
 
63
- class ZeroBasedTest < Test::Unit::TestCase
64
- def setup
65
- setup_db
66
- (1..4).each { |counter| ZeroBasedMixin.create! :pos => counter, :parent_id => 5 }
67
- end
78
+ class TopAdditionMixin < Mixin
79
+ acts_as_list :column => "pos", :add_new_at => :top, :scope => :parent_id
80
+ end
81
+
82
+ class ActsAsListTestCase < Test::Unit::TestCase
83
+ # No default test required a this class is abstract.
84
+ # Need for test/unit.
85
+ undef_method :default_test if method_defined?(:default_test)
68
86
 
69
87
  def teardown
70
88
  teardown_db
71
89
  end
90
+ end
72
91
 
73
- def test_insert
74
- new = ZeroBasedMixin.create(:parent_id => 20)
75
- assert_equal 0, new.pos
76
- assert new.first?
77
- assert new.last?
78
-
79
- new = ZeroBasedMixin.create(:parent_id => 20)
80
- assert_equal 1, new.pos
81
- assert !new.first?
82
- assert new.last?
83
-
84
- new = ZeroBasedMixin.create(:parent_id => 20)
85
- assert_equal 2, new.pos
86
- assert !new.first?
87
- assert new.last?
92
+ class ZeroBasedTest < ActsAsListTestCase
93
+ include Shared::ZeroBased
88
94
 
89
- new = ZeroBasedMixin.create(:parent_id => 0)
90
- assert_equal 0, new.pos
91
- assert new.first?
92
- assert new.last?
95
+ def setup
96
+ setup_db
97
+ super
93
98
  end
99
+ end
94
100
 
95
- def test_reordering
96
- assert_equal [1, 2, 3, 4], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
97
-
98
- ListMixin.find(2).move_lower
99
- assert_equal [1, 3, 2, 4], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
100
-
101
- ListMixin.find(2).move_higher
102
- assert_equal [1, 2, 3, 4], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
103
-
104
- ListMixin.find(1).move_to_bottom
105
- assert_equal [2, 3, 4, 1], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
106
-
107
- ListMixin.find(1).move_to_top
108
- assert_equal [1, 2, 3, 4], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
109
-
110
- ListMixin.find(2).move_to_bottom
111
- assert_equal [1, 3, 4, 2], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
101
+ class ZeroBasedTestWithDefault < ActsAsListTestCase
102
+ include Shared::ZeroBased
112
103
 
113
- ListMixin.find(4).move_to_top
114
- assert_equal [4, 1, 3, 2], ZeroBasedMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
104
+ def setup
105
+ setup_db_with_default
106
+ super
115
107
  end
108
+ end
116
109
 
117
- def test_insert_at
118
- new = ZeroBasedMixin.create(:parent_id => 20)
119
- assert_equal 0, new.pos
120
-
121
- new = ZeroBasedMixin.create(:parent_id => 20)
122
- assert_equal 1, new.pos
123
-
124
- new = ZeroBasedMixin.create(:parent_id => 20)
125
- assert_equal 2, new.pos
126
-
127
- new4 = ZeroBasedMixin.create(:parent_id => 20)
128
- assert_equal 3, new4.pos
129
-
130
- new4.insert_at(2)
131
- assert_equal 2, new4.pos
132
-
133
- new.reload
134
- assert_equal 3, new.pos
135
-
136
- new.insert_at(2)
137
- assert_equal 2, new.pos
138
-
139
- new4.reload
140
- assert_equal 3, new4.pos
110
+ class ListTest < ActsAsListTestCase
111
+ include Shared::List
141
112
 
142
- new5 = ListMixin.create(:parent_id => 20)
143
- assert_equal 4, new5.pos
113
+ def setup
114
+ setup_db
115
+ super
116
+ end
117
+ end
144
118
 
145
- new5.insert_at(1)
146
- assert_equal 1, new5.pos
119
+ class ListTestWithDefault < ActsAsListTestCase
120
+ include Shared::List
147
121
 
148
- new4.reload
149
- assert_equal 4, new4.pos
122
+ def setup
123
+ setup_db_with_default
124
+ super
150
125
  end
151
-
152
126
  end
153
127
 
154
-
155
- class ListTest < Test::Unit::TestCase
128
+ class ListSubTest < ActsAsListTestCase
129
+ include Shared::ListSub
156
130
 
157
131
  def setup
158
132
  setup_db
159
- (1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 5 }
160
- end
161
-
162
- def teardown
163
- teardown_db
133
+ super
164
134
  end
135
+ end
165
136
 
166
- def test_reordering
167
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
168
-
169
- ListMixin.find(2).move_lower
170
- assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
171
-
172
- ListMixin.find(2).move_higher
173
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
174
-
175
- ListMixin.find(1).move_to_bottom
176
- assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
137
+ class ListSubTestWithDefault < ActsAsListTestCase
138
+ include Shared::ListSub
177
139
 
178
- ListMixin.find(1).move_to_top
179
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
140
+ def setup
141
+ setup_db_with_default
142
+ super
143
+ end
144
+ end
180
145
 
181
- ListMixin.find(2).move_to_bottom
182
- assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
146
+ class ArrayScopeListTest < ActsAsListTestCase
147
+ include Shared::ArrayScopeList
183
148
 
184
- ListMixin.find(4).move_to_top
185
- assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
149
+ def setup
150
+ setup_db
151
+ super
186
152
  end
153
+ end
187
154
 
188
- def test_move_to_bottom_with_next_to_last_item
189
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
190
- ListMixin.find(3).move_to_bottom
191
- assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
192
- end
155
+ class ArrayScopeListTestWithDefault < ActsAsListTestCase
156
+ include Shared::ArrayScopeList
193
157
 
194
- def test_next_prev
195
- assert_equal ListMixin.find(2), ListMixin.find(1).lower_item
196
- assert_nil ListMixin.find(1).higher_item
197
- assert_equal ListMixin.find(3), ListMixin.find(4).higher_item
198
- assert_nil ListMixin.find(4).lower_item
158
+ def setup
159
+ setup_db_with_default
160
+ super
199
161
  end
162
+ end
200
163
 
201
- def test_injection
202
- item = ListMixin.new(:parent_id => 1)
203
- assert_equal '"mixins"."parent_id" = 1', item.scope_condition
204
- assert_equal "pos", item.position_column
164
+ class DefaultScopedTest < ActsAsListTestCase
165
+ def setup
166
+ setup_db
167
+ (1..4).each { |counter| DefaultScopedMixin.create! :pos => counter }
205
168
  end
206
169
 
207
170
  def test_insert
208
- new = ListMixin.create(:parent_id => 20)
209
- assert_equal 1, new.pos
210
- assert new.first?
211
- assert new.last?
212
-
213
- new = ListMixin.create(:parent_id => 20)
214
- assert_equal 2, new.pos
171
+ new = DefaultScopedMixin.create
172
+ assert_equal 5, new.pos
215
173
  assert !new.first?
216
174
  assert new.last?
217
175
 
218
- new = ListMixin.create(:parent_id => 20)
219
- assert_equal 3, new.pos
176
+ new = DefaultScopedMixin.create
177
+ assert_equal 6, new.pos
220
178
  assert !new.first?
221
179
  assert new.last?
222
180
 
223
- new = ListMixin.create(:parent_id => 0)
224
- assert_equal 1, new.pos
225
- assert new.first?
226
- assert new.last?
227
- end
228
-
229
- def test_insert_at
230
- new = ListMixin.create(:parent_id => 20)
231
- assert_equal 1, new.pos
232
-
233
- new = ListMixin.create(:parent_id => 20)
234
- assert_equal 2, new.pos
235
-
236
- new = ListMixin.create(:parent_id => 20)
237
- assert_equal 3, new.pos
238
-
239
- new4 = ListMixin.create(:parent_id => 20)
240
- assert_equal 4, new4.pos
241
-
242
- new4.insert_at(3)
243
- assert_equal 3, new4.pos
244
-
245
- new.reload
246
- assert_equal 4, new.pos
247
-
248
- new.insert_at(2)
249
- assert_equal 2, new.pos
250
-
251
- new4.reload
252
- assert_equal 4, new4.pos
253
-
254
- new5 = ListMixin.create(:parent_id => 20)
255
- assert_equal 5, new5.pos
256
-
257
- new5.insert_at(1)
258
- assert_equal 1, new5.pos
259
-
260
- new4.reload
261
- assert_equal 5, new4.pos
262
- end
263
-
264
- def test_delete_middle
265
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
266
-
267
- ListMixin.find(2).destroy
268
-
269
- assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
270
-
271
- assert_equal 1, ListMixin.find(1).pos
272
- assert_equal 2, ListMixin.find(3).pos
273
- assert_equal 3, ListMixin.find(4).pos
274
-
275
- ListMixin.find(1).destroy
276
-
277
- assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
278
-
279
- assert_equal 1, ListMixin.find(3).pos
280
- assert_equal 2, ListMixin.find(4).pos
281
- end
282
-
283
- def test_with_string_based_scope
284
- new = ListWithStringScopeMixin.create(:parent_id => 500)
285
- assert_equal 1, new.pos
286
- assert new.first?
287
- assert new.last?
288
- end
289
-
290
- def test_nil_scope
291
- new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create
292
- new2.move_higher
293
- assert_equal [new2, new1, new3], ListMixin.find(:all, :conditions => 'parent_id IS NULL', :order => 'pos')
294
- end
295
-
296
- def test_remove_from_list_should_then_fail_in_list?
297
- assert_equal true, ListMixin.find(1).in_list?
298
- ListMixin.find(1).remove_from_list
299
- assert_equal false, ListMixin.find(1).in_list?
300
- end
301
-
302
- def test_remove_from_list_should_set_position_to_nil
303
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
304
-
305
- ListMixin.find(2).remove_from_list
306
-
307
- assert_equal [2, 1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
308
-
309
- assert_equal 1, ListMixin.find(1).pos
310
- assert_equal nil, ListMixin.find(2).pos
311
- assert_equal 2, ListMixin.find(3).pos
312
- assert_equal 3, ListMixin.find(4).pos
313
- end
314
-
315
- def test_remove_before_destroy_does_not_shift_lower_items_twice
316
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
317
-
318
- ListMixin.find(2).remove_from_list
319
- ListMixin.find(2).destroy
320
-
321
- assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
322
-
323
- assert_equal 1, ListMixin.find(1).pos
324
- assert_equal 2, ListMixin.find(3).pos
325
- assert_equal 3, ListMixin.find(4).pos
326
- end
327
-
328
- def test_before_destroy_callbacks_do_not_update_position_to_nil_before_deleting_the_record
329
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
330
-
331
- # We need to trigger all the before_destroy callbacks without actually
332
- # destroying the record so we can see the affect the callbacks have on
333
- # the record.
334
- # NOTE: Hotfix for rails3 ActiveRecord
335
- list = ListMixin.find(2)
336
- if list.respond_to?(:run_callbacks)
337
- # Refactored to work according to Rails3 ActiveRSupport Callbacks <http://api.rubyonrails.org/classes/ActiveSupport/Callbacks.html>
338
- list.run_callbacks :destroy, :before if rails_3
339
- list.run_callbacks(:before_destroy) if !rails_3
340
- else
341
- list.send(:callback, :before_destroy)
342
- end
343
-
344
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
345
-
346
- assert_equal 1, ListMixin.find(1).pos
347
- assert_equal 2, ListMixin.find(2).pos
348
- assert_equal 2, ListMixin.find(3).pos
349
- assert_equal 3, ListMixin.find(4).pos
350
- end
351
-
352
- def test_before_create_callback_adds_to_bottom
353
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
354
-
355
- new = ListMixin.create(:parent_id => 5)
356
- assert_equal 5, new.pos
181
+ new = DefaultScopedMixin.create
182
+ assert_equal 7, new.pos
357
183
  assert !new.first?
358
184
  assert new.last?
359
-
360
- assert_equal [1, 2, 3, 4, 5], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
361
- end
362
-
363
- def test_before_create_callback_adds_to_given_position
364
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
365
-
366
- new = ListMixin.create(:pos => 1, :parent_id => 5)
367
- assert_equal 1, new.pos
368
- assert new.first?
369
- assert !new.last?
370
-
371
- assert_equal [5, 1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
372
-
373
- new = ListMixin.create(:pos => 3, :parent_id => 5)
374
- assert_equal 3, new.pos
375
- assert !new.first?
376
- assert !new.last?
377
-
378
- assert_equal [5, 1, 6, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
379
- end
380
- end
381
-
382
- class ListSubTest < Test::Unit::TestCase
383
-
384
- def setup
385
- setup_db
386
- (1..4).each { |i| ((i % 2 == 1) ? ListMixinSub1 : ListMixinSub2).create! :pos => i, :parent_id => 5000 }
387
- end
388
-
389
- def teardown
390
- teardown_db
391
185
  end
392
186
 
393
187
  def test_reordering
394
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
395
-
396
- ListMixin.find(2).move_lower
397
- assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
398
-
399
- ListMixin.find(2).move_higher
400
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
188
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
401
189
 
402
- ListMixin.find(1).move_to_bottom
403
- assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
190
+ DefaultScopedMixin.find(2).move_lower
191
+ assert_equal [1, 3, 2, 4], DefaultScopedMixin.find(:all).map(&:id)
404
192
 
405
- ListMixin.find(1).move_to_top
406
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
193
+ DefaultScopedMixin.find(2).move_higher
194
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
407
195
 
408
- ListMixin.find(2).move_to_bottom
409
- assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
196
+ DefaultScopedMixin.find(1).move_to_bottom
197
+ assert_equal [2, 3, 4, 1], DefaultScopedMixin.find(:all).map(&:id)
410
198
 
411
- ListMixin.find(4).move_to_top
412
- assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
413
- end
414
-
415
- def test_move_to_bottom_with_next_to_last_item
416
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
417
- ListMixin.find(3).move_to_bottom
418
- assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
419
- end
199
+ DefaultScopedMixin.find(1).move_to_top
200
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
420
201
 
421
- def test_next_prev
422
- assert_equal ListMixin.find(2), ListMixin.find(1).lower_item
423
- assert_nil ListMixin.find(1).higher_item
424
- assert_equal ListMixin.find(3), ListMixin.find(4).higher_item
425
- assert_nil ListMixin.find(4).lower_item
426
- end
202
+ DefaultScopedMixin.find(2).move_to_bottom
203
+ assert_equal [1, 3, 4, 2], DefaultScopedMixin.find(:all).map(&:id)
427
204
 
428
- def test_injection
429
- item = ListMixin.new("parent_id"=>1)
430
- assert_equal '"mixins"."parent_id" = 1', item.scope_condition
431
- assert_equal "pos", item.position_column
205
+ DefaultScopedMixin.find(4).move_to_top
206
+ assert_equal [4, 1, 3, 2], DefaultScopedMixin.find(:all).map(&:id)
432
207
  end
433
208
 
434
209
  def test_insert_at
435
- new = ListMixin.create("parent_id" => 20)
436
- assert_equal 1, new.pos
210
+ new = DefaultScopedMixin.create
211
+ assert_equal 5, new.pos
437
212
 
438
- new = ListMixinSub1.create("parent_id" => 20)
439
- assert_equal 2, new.pos
213
+ new = DefaultScopedMixin.create
214
+ assert_equal 6, new.pos
440
215
 
441
- new = ListMixinSub2.create("parent_id" => 20)
442
- assert_equal 3, new.pos
216
+ new = DefaultScopedMixin.create
217
+ assert_equal 7, new.pos
443
218
 
444
- new4 = ListMixin.create("parent_id" => 20)
445
- assert_equal 4, new4.pos
219
+ new4 = DefaultScopedMixin.create
220
+ assert_equal 8, new4.pos
446
221
 
447
- new4.insert_at(3)
448
- assert_equal 3, new4.pos
222
+ new4.insert_at(2)
223
+ assert_equal 2, new4.pos
449
224
 
450
225
  new.reload
451
- assert_equal 4, new.pos
226
+ assert_equal 8, new.pos
452
227
 
453
228
  new.insert_at(2)
454
229
  assert_equal 2, new.pos
455
230
 
456
231
  new4.reload
457
- assert_equal 4, new4.pos
232
+ assert_equal 3, new4.pos
458
233
 
459
- new5 = ListMixinSub1.create("parent_id" => 20)
460
- assert_equal 5, new5.pos
234
+ new5 = DefaultScopedMixin.create
235
+ assert_equal 9, new5.pos
461
236
 
462
237
  new5.insert_at(1)
463
238
  assert_equal 1, new5.pos
464
239
 
465
240
  new4.reload
466
- assert_equal 5, new4.pos
241
+ assert_equal 4, new4.pos
467
242
  end
468
243
 
469
- def test_delete_middle
470
- assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
471
-
472
- ListMixin.find(2).destroy
473
-
474
- assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
475
-
476
- assert_equal 1, ListMixin.find(1).pos
477
- assert_equal 2, ListMixin.find(3).pos
478
- assert_equal 3, ListMixin.find(4).pos
479
-
480
- ListMixin.find(1).destroy
481
-
482
- assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
483
-
484
- assert_equal 1, ListMixin.find(3).pos
485
- assert_equal 2, ListMixin.find(4).pos
244
+ def test_update_position
245
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
246
+ DefaultScopedMixin.find(2).update_attributes!(:pos => 4)
247
+ assert_equal [1, 3, 4, 2], DefaultScopedMixin.find(:all).map(&:id)
248
+ DefaultScopedMixin.find(2).update_attributes!(:pos => 2)
249
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
250
+ DefaultScopedMixin.find(1).update_attributes!(:pos => 4)
251
+ assert_equal [2, 3, 4, 1], DefaultScopedMixin.find(:all).map(&:id)
252
+ DefaultScopedMixin.find(1).update_attributes!(:pos => 1)
253
+ assert_equal [1, 2, 3, 4], DefaultScopedMixin.find(:all).map(&:id)
486
254
  end
487
255
 
488
256
  end
489
257
 
490
- class ArrayScopeListTest < Test::Unit::TestCase
491
-
258
+ class DefaultScopedWhereTest < ActsAsListTestCase
492
259
  def setup
493
260
  setup_db
494
- (1..4).each { |counter| ArrayScopeListMixin.create! :pos => counter, :parent_id => 5, :parent_type => 'ParentClass' }
261
+ (1..4).each { |counter| DefaultScopedWhereMixin.create! :pos => counter, :active => false }
495
262
  end
496
263
 
497
- def teardown
498
- teardown_db
499
- end
500
-
501
- def test_reordering
502
- assert_equal [1, 2, 3, 4], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
503
-
504
- ArrayScopeListMixin.find(2).move_lower
505
- assert_equal [1, 3, 2, 4], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
506
-
507
- ArrayScopeListMixin.find(2).move_higher
508
- assert_equal [1, 2, 3, 4], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
509
-
510
- ArrayScopeListMixin.find(1).move_to_bottom
511
- assert_equal [2, 3, 4, 1], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
512
-
513
- ArrayScopeListMixin.find(1).move_to_top
514
- assert_equal [1, 2, 3, 4], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
264
+ def test_insert
265
+ new = DefaultScopedWhereMixin.create
266
+ assert_equal 5, new.pos
267
+ assert !new.first?
268
+ assert new.last?
515
269
 
516
- ArrayScopeListMixin.find(2).move_to_bottom
517
- assert_equal [1, 3, 4, 2], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
270
+ new = DefaultScopedWhereMixin.create
271
+ assert_equal 6, new.pos
272
+ assert !new.first?
273
+ assert new.last?
518
274
 
519
- ArrayScopeListMixin.find(4).move_to_top
520
- assert_equal [4, 1, 3, 2], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
275
+ new = DefaultScopedWhereMixin.create
276
+ assert_equal 7, new.pos
277
+ assert !new.first?
278
+ assert new.last?
521
279
  end
522
280
 
523
- def test_move_to_bottom_with_next_to_last_item
524
- assert_equal [1, 2, 3, 4], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
525
- ArrayScopeListMixin.find(3).move_to_bottom
526
- assert_equal [1, 2, 4, 3], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
527
- end
281
+ def test_reordering
282
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).map(&:id)
528
283
 
529
- def test_next_prev
530
- assert_equal ArrayScopeListMixin.find(2), ArrayScopeListMixin.find(1).lower_item
531
- assert_nil ArrayScopeListMixin.find(1).higher_item
532
- assert_equal ArrayScopeListMixin.find(3), ArrayScopeListMixin.find(4).higher_item
533
- assert_nil ArrayScopeListMixin.find(4).lower_item
534
- end
284
+ DefaultScopedWhereMixin.where(:active => false).find(2).move_lower
285
+ assert_equal [1, 3, 2, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
535
286
 
536
- def test_injection
537
- item = ArrayScopeListMixin.new(:parent_id => 1, :parent_type => 'ParentClass')
538
- assert_equal '"mixins"."parent_id" = 1 AND "mixins"."parent_type" = \'ParentClass\'', item.scope_condition
539
- assert_equal "pos", item.position_column
540
- end
287
+ DefaultScopedWhereMixin.where(:active => false).find(2).move_higher
288
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
541
289
 
542
- def test_insert
543
- new = ArrayScopeListMixin.create(:parent_id => 20, :parent_type => 'ParentClass')
544
- assert_equal 1, new.pos
545
- assert new.first?
546
- assert new.last?
290
+ DefaultScopedWhereMixin.where(:active => false).find(1).move_to_bottom
291
+ assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
547
292
 
548
- new = ArrayScopeListMixin.create(:parent_id => 20, :parent_type => 'ParentClass')
549
- assert_equal 2, new.pos
550
- assert !new.first?
551
- assert new.last?
293
+ DefaultScopedWhereMixin.where(:active => false).find(1).move_to_top
294
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
552
295
 
553
- new = ArrayScopeListMixin.create(:parent_id => 20, :parent_type => 'ParentClass')
554
- assert_equal 3, new.pos
555
- assert !new.first?
556
- assert new.last?
296
+ DefaultScopedWhereMixin.where(:active => false).find(2).move_to_bottom
297
+ assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
557
298
 
558
- new = ArrayScopeListMixin.create(:parent_id => 0, :parent_type => 'ParentClass')
559
- assert_equal 1, new.pos
560
- assert new.first?
561
- assert new.last?
299
+ DefaultScopedWhereMixin.where(:active => false).find(4).move_to_top
300
+ assert_equal [4, 1, 3, 2], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
562
301
  end
563
302
 
564
303
  def test_insert_at
565
- new = ArrayScopeListMixin.create(:parent_id => 20, :parent_type => 'ParentClass')
566
- assert_equal 1, new.pos
304
+ new = DefaultScopedWhereMixin.create
305
+ assert_equal 5, new.pos
567
306
 
568
- new = ArrayScopeListMixin.create(:parent_id => 20, :parent_type => 'ParentClass')
569
- assert_equal 2, new.pos
307
+ new = DefaultScopedWhereMixin.create
308
+ assert_equal 6, new.pos
570
309
 
571
- new = ArrayScopeListMixin.create(:parent_id => 20, :parent_type => 'ParentClass')
572
- assert_equal 3, new.pos
310
+ new = DefaultScopedWhereMixin.create
311
+ assert_equal 7, new.pos
573
312
 
574
- new4 = ArrayScopeListMixin.create(:parent_id => 20, :parent_type => 'ParentClass')
575
- assert_equal 4, new4.pos
313
+ new4 = DefaultScopedWhereMixin.create
314
+ assert_equal 8, new4.pos
576
315
 
577
- new4.insert_at(3)
578
- assert_equal 3, new4.pos
316
+ new4.insert_at(2)
317
+ assert_equal 2, new4.pos
579
318
 
580
319
  new.reload
581
- assert_equal 4, new.pos
320
+ assert_equal 8, new.pos
582
321
 
583
322
  new.insert_at(2)
584
323
  assert_equal 2, new.pos
585
324
 
586
325
  new4.reload
587
- assert_equal 4, new4.pos
326
+ assert_equal 3, new4.pos
588
327
 
589
- new5 = ArrayScopeListMixin.create(:parent_id => 20, :parent_type => 'ParentClass')
590
- assert_equal 5, new5.pos
328
+ new5 = DefaultScopedWhereMixin.create
329
+ assert_equal 9, new5.pos
591
330
 
592
331
  new5.insert_at(1)
593
332
  assert_equal 1, new5.pos
594
333
 
595
334
  new4.reload
596
- assert_equal 5, new4.pos
597
- end
598
-
599
- def test_delete_middle
600
- assert_equal [1, 2, 3, 4], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
601
-
602
- ArrayScopeListMixin.find(2).destroy
603
-
604
- assert_equal [1, 3, 4], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
605
-
606
- assert_equal 1, ArrayScopeListMixin.find(1).pos
607
- assert_equal 2, ArrayScopeListMixin.find(3).pos
608
- assert_equal 3, ArrayScopeListMixin.find(4).pos
609
-
610
- ArrayScopeListMixin.find(1).destroy
611
-
612
- assert_equal [3, 4], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
613
-
614
- assert_equal 1, ArrayScopeListMixin.find(3).pos
615
- assert_equal 2, ArrayScopeListMixin.find(4).pos
335
+ assert_equal 4, new4.pos
616
336
  end
617
337
 
618
- def test_remove_from_list_should_then_fail_in_list?
619
- assert_equal true, ArrayScopeListMixin.find(1).in_list?
620
- ArrayScopeListMixin.find(1).remove_from_list
621
- assert_equal false, ArrayScopeListMixin.find(1).in_list?
338
+ def test_update_position
339
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
340
+ DefaultScopedWhereMixin.where(:active => false).find(2).update_attributes!(:pos => 4)
341
+ assert_equal [1, 3, 4, 2], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
342
+ DefaultScopedWhereMixin.where(:active => false).find(2).update_attributes!(:pos => 2)
343
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
344
+ DefaultScopedWhereMixin.where(:active => false).find(1).update_attributes!(:pos => 4)
345
+ assert_equal [2, 3, 4, 1], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
346
+ DefaultScopedWhereMixin.where(:active => false).find(1).update_attributes!(:pos => 1)
347
+ assert_equal [1, 2, 3, 4], DefaultScopedWhereMixin.where(:active => false).find(:all).map(&:id)
622
348
  end
623
349
 
624
- def test_remove_from_list_should_set_position_to_nil
625
- assert_equal [1, 2, 3, 4], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
350
+ end
626
351
 
627
- ArrayScopeListMixin.find(2).remove_from_list
352
+ #class TopAdditionMixin < Mixin
628
353
 
629
- assert_equal [2, 1, 3, 4], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
354
+ class TopAdditionTest < ActsAsListTestCase
355
+ include Shared::TopAddition
630
356
 
631
- assert_equal 1, ArrayScopeListMixin.find(1).pos
632
- assert_equal nil, ArrayScopeListMixin.find(2).pos
633
- assert_equal 2, ArrayScopeListMixin.find(3).pos
634
- assert_equal 3, ArrayScopeListMixin.find(4).pos
357
+ def setup
358
+ setup_db
359
+ super
635
360
  end
361
+ end
636
362
 
637
- def test_remove_before_destroy_does_not_shift_lower_items_twice
638
- assert_equal [1, 2, 3, 4], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
639
-
640
- ArrayScopeListMixin.find(2).remove_from_list
641
- ArrayScopeListMixin.find(2).destroy
642
-
643
- assert_equal [1, 3, 4], ArrayScopeListMixin.find(:all, :conditions => "parent_id = 5 AND parent_type = 'ParentClass'", :order => 'pos').map(&:id)
363
+ class TopAdditionTestWithDefault < ActsAsListTestCase
364
+ include Shared::TopAddition
644
365
 
645
- assert_equal 1, ArrayScopeListMixin.find(1).pos
646
- assert_equal 2, ArrayScopeListMixin.find(3).pos
647
- assert_equal 3, ArrayScopeListMixin.find(4).pos
366
+ def setup
367
+ setup_db_with_default
368
+ super
648
369
  end
649
-
650
370
  end