acts_as_silent_list 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.rdoc +4 -0
- data/Gemfile +12 -1
- data/MIT-LICENSE +20 -0
- data/Rakefile +22 -22
- data/acts_as_silent_list.gemspec +10 -13
- data/init.rb +2 -0
- data/lib/acts_as_silent_list.rb +23 -1
- data/lib/acts_as_silent_list/active_record/acts/silent_list.rb +27 -32
- data/lib/acts_as_silent_list/version.rb +1 -1
- data/test/{test_list.rb → acts_as_silent_list_test.rb} +164 -139
- data/test/test_helper.rb +3 -0
- metadata +90 -114
- data/test/helper.rb +0 -12
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4aaab2e363a459ad3c00182e51967c28e0880aee
|
4
|
+
data.tar.gz: 2546f7bdf9f53ef7ab4e6b877f0c70bfd3c8882f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bfd2f6c4d8429839e58f0169dbd8706265ec88c59710f9cee42a862e9a0a80fd6d173a7f37d71191ff96d638eb358902d6f124257914d83d0004dec024baec3f
|
7
|
+
data.tar.gz: b0ab4990e99488e0a3e83e78e716532053ea9ff9a0a066601d0ea51c2a51e9a878e019393db8d404deccf8a199c4f6d8b0a832bafe2157a30e5db5a198ca296a
|
data/CHANGELOG.rdoc
CHANGED
data/Gemfile
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
-
source
|
1
|
+
source "https://rubygems.org"
|
2
2
|
|
3
|
+
# Declare your gem's dependencies in acts_as_silent_list.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
3
6
|
gemspec
|
7
|
+
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
+
# your gem to rubygems.org.
|
12
|
+
|
13
|
+
# To use debugger
|
14
|
+
# gem 'debugger'
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 Finn GmbH
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -1,31 +1,31 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
6
|
|
7
|
-
|
8
|
-
desc 'Default: run acts_as_silent_list unit tests.'
|
9
|
-
task :default => :test
|
7
|
+
require 'rdoc/task'
|
10
8
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'ActsAsSilentList'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
16
15
|
end
|
17
16
|
|
18
17
|
|
18
|
+
Bundler::GemHelper.install_tasks
|
19
19
|
|
20
|
-
|
21
|
-
require 'rdoc/task'
|
22
|
-
RDoc::Task.new do |rdoc|
|
23
|
-
require "acts_as_silent_list/version"
|
24
|
-
version = ActiveRecord::Acts::SilentList::VERSION
|
20
|
+
require 'rake/testtask'
|
25
21
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
22
|
+
desc 'Test the acts_as_silent_list plugin.'
|
23
|
+
Rake::TestTask.new(:test) do |t|
|
24
|
+
t.libs << 'lib'
|
25
|
+
t.libs << 'test'
|
26
|
+
t.pattern = 'test/**/*_test.rb'
|
27
|
+
t.verbose = true
|
30
28
|
end
|
31
29
|
|
30
|
+
desc 'Default: run acts_as_silent_list unit tests.'
|
31
|
+
task default: :test
|
data/acts_as_silent_list.gemspec
CHANGED
@@ -1,29 +1,26 @@
|
|
1
|
-
|
2
|
-
$:.push File.expand_path('../lib', __FILE__)
|
3
|
-
require 'acts_as_silent_list/version'
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
4
2
|
|
5
|
-
|
3
|
+
# Maintain your gem's version:
|
4
|
+
require "acts_as_silent_list/version"
|
6
5
|
|
7
|
-
|
8
|
-
|
6
|
+
# Describe your gem and declare its dependencies:
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "acts_as_silent_list"
|
9
9
|
s.version = ActiveRecord::Acts::SilentList::VERSION
|
10
|
-
s.
|
11
|
-
s.
|
12
|
-
s.
|
13
|
-
s.homepage = 'http://github.com/swanandp/acts_as_silent_list'
|
10
|
+
s.authors = ['David Heinemeier Hansson', 'Swanand Pagnis', 'Quinn Chaffee', 'Finn GmbH']
|
11
|
+
s.email = ["info@finn.de"]
|
12
|
+
s.homepage = "http://www.finn.de/"
|
14
13
|
s.summary = %q{A gem allowing a active_record model to be ordered, without triggering active record callbacks.}
|
15
14
|
s.description = %q{This "acts_as" extension is a clone of the well known acts_as_list, only it avoids triggering active record callbacks.}
|
16
15
|
|
17
|
-
|
18
16
|
# Load Paths...
|
19
17
|
s.files = `git ls-files`.split("\n")
|
20
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
20
|
s.require_paths = ['lib']
|
23
21
|
|
24
|
-
|
25
22
|
# Dependencies (installed via 'bundle install')...
|
26
|
-
s.add_development_dependency("bundler", ["
|
23
|
+
s.add_development_dependency("bundler", [">= 1.0.0"])
|
27
24
|
s.add_development_dependency("activerecord", [">= 1.15.4.7794"])
|
28
25
|
s.add_development_dependency("rake")
|
29
26
|
s.add_development_dependency("rdoc")
|
data/init.rb
CHANGED
data/lib/acts_as_silent_list.rb
CHANGED
@@ -1,2 +1,24 @@
|
|
1
1
|
require 'acts_as_silent_list/active_record/acts/silent_list'
|
2
|
-
|
2
|
+
|
3
|
+
module ActsAsSilentList
|
4
|
+
if defined? Rails::Railtie
|
5
|
+
require 'rails'
|
6
|
+
class Railtie < Rails::Railtie
|
7
|
+
initializer 'acts_as_silent_list.insert_into_active_record' do
|
8
|
+
ActiveSupport.on_load :active_record do
|
9
|
+
ActsAsSilentList::Railtie.insert
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Railtie
|
16
|
+
def self.insert
|
17
|
+
if defined?(ActiveRecord)
|
18
|
+
ActiveRecord::Base.send(:include, ActiveRecord::Acts::SilentList)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
ActsAsSilentList::Railtie.insert
|
@@ -194,28 +194,28 @@ module ActiveRecord
|
|
194
194
|
# Return the next higher item in the list.
|
195
195
|
def higher_item
|
196
196
|
return nil unless in_list?
|
197
|
-
acts_as_silent_list_class
|
198
|
-
|
199
|
-
|
197
|
+
acts_as_silent_list_class
|
198
|
+
.where("#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}")
|
199
|
+
.first
|
200
200
|
end
|
201
201
|
|
202
202
|
# Return the next lower item in the list.
|
203
203
|
def lower_item
|
204
204
|
return nil unless in_list?
|
205
|
-
acts_as_silent_list_class
|
206
|
-
|
207
|
-
|
205
|
+
acts_as_silent_list_class
|
206
|
+
.where("#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}")
|
207
|
+
.first
|
208
208
|
end
|
209
209
|
|
210
210
|
# Test if this record is in a list
|
211
211
|
def in_list?
|
212
212
|
!not_in_list?
|
213
213
|
end
|
214
|
-
|
214
|
+
|
215
215
|
def not_in_list?
|
216
216
|
send(position_column).nil?
|
217
217
|
end
|
218
|
-
|
218
|
+
|
219
219
|
def default_position
|
220
220
|
acts_as_silent_list_class.columns_hash[position_column.to_s].default
|
221
221
|
end
|
@@ -252,9 +252,9 @@ module ActiveRecord
|
|
252
252
|
conditions = scope_condition
|
253
253
|
conditions = "#{conditions} AND #{self.class.primary_key} != #{except.id}" if except
|
254
254
|
acts_as_silent_list_class.unscoped do
|
255
|
-
|
256
|
-
|
257
|
-
|
255
|
+
acts_as_silent_list_class
|
256
|
+
.where(conditions).order("#{position_column} DESC")
|
257
|
+
.first
|
258
258
|
end
|
259
259
|
end
|
260
260
|
|
@@ -276,45 +276,40 @@ module ActiveRecord
|
|
276
276
|
|
277
277
|
# This has the effect of moving all the higher items up one.
|
278
278
|
def decrement_positions_on_higher_items(position)
|
279
|
-
acts_as_silent_list_class
|
280
|
-
|
281
|
-
|
282
|
-
)
|
279
|
+
acts_as_silent_list_class
|
280
|
+
.where("#{scope_condition} AND #{position_column} <= #{position}")
|
281
|
+
.update_all("#{position_column} = (#{position_column} - 1)")
|
283
282
|
end
|
284
283
|
|
285
284
|
# This has the effect of moving all the lower items up one.
|
286
285
|
def decrement_positions_on_lower_items
|
287
286
|
return unless in_list?
|
288
|
-
acts_as_silent_list_class
|
289
|
-
|
290
|
-
|
291
|
-
)
|
287
|
+
acts_as_silent_list_class
|
288
|
+
.where("#{scope_condition} AND #{position_column} > #{send(position_column).to_i}")
|
289
|
+
.update_all("#{position_column} = (#{position_column} - 1)")
|
292
290
|
end
|
293
291
|
|
294
292
|
# This has the effect of moving all the higher items down one.
|
295
293
|
def increment_positions_on_higher_items
|
296
294
|
return unless in_list?
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
)
|
295
|
+
acts_as_silent_list_class
|
296
|
+
.where("#{scope_condition} AND #{position_column} < #{send(position_column).to_i}")
|
297
|
+
.update_all("#{position_column} = (#{position_column} + 1)")
|
301
298
|
end
|
302
299
|
|
303
300
|
# This has the effect of moving all the lower items down one.
|
304
301
|
def increment_positions_on_lower_items(position)
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
)
|
302
|
+
acts_as_silent_list_class
|
303
|
+
.where("#{scope_condition} AND #{position_column} >= #{position}")
|
304
|
+
.update_all("#{position_column} = (#{position_column} + 1)")
|
309
305
|
end
|
310
306
|
|
311
307
|
# Increments position (<tt>position_column</tt>) of all items in the
|
312
308
|
# list.
|
313
309
|
def increment_positions_on_all_items
|
314
|
-
acts_as_silent_list_class
|
315
|
-
|
316
|
-
|
317
|
-
)
|
310
|
+
acts_as_silent_list_class
|
311
|
+
.where("#{scope_condition}")
|
312
|
+
.update_all("#{position_column} = (#{position_column} + 1)")
|
318
313
|
end
|
319
314
|
|
320
315
|
def insert_at_position(position)
|
@@ -339,7 +334,7 @@ module ActiveRecord
|
|
339
334
|
write_attribute(attr, value)
|
340
335
|
changed_attributes.delete(attr)
|
341
336
|
end
|
342
|
-
self.class.
|
337
|
+
self.class.where({:id => id}).update_all({attr => value})
|
343
338
|
end
|
344
339
|
end
|
345
340
|
end
|
@@ -1,5 +1,13 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
# class ActsAsSilentListTest < ActiveSupport::TestCase
|
4
|
+
# test "truth" do
|
5
|
+
# assert_kind_of Module, ActsAsSilentList
|
6
|
+
# end
|
7
|
+
# end
|
8
|
+
|
9
|
+
# # NOTE: following now done in helper.rb (better Readability)
|
10
|
+
# require 'helper.rb'
|
3
11
|
|
4
12
|
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
5
13
|
|
@@ -29,7 +37,12 @@ end
|
|
29
37
|
|
30
38
|
# Returns true if ActiveRecord is rails3 version
|
31
39
|
def rails_3
|
32
|
-
defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR >= 3
|
40
|
+
defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR >= 3 && ActiveRecord::VERSION::MAJOR < 4
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns true if ActiveRecord is rails4 version
|
44
|
+
def rails_4_or_above
|
45
|
+
defined?(ActiveRecord::VERSION) && ActiveRecord::VERSION::MAJOR >= 4
|
33
46
|
end
|
34
47
|
|
35
48
|
def teardown_db
|
@@ -72,17 +85,17 @@ class ZeroBasedMixin < ActiveRecord::Base
|
|
72
85
|
end
|
73
86
|
|
74
87
|
class DefaultScopedMixin < ActiveRecord::Base
|
75
|
-
|
88
|
+
self.table_name = 'mixins'
|
76
89
|
acts_as_silent_list :column => "pos"
|
77
|
-
if rails_3
|
78
|
-
default_scope order('pos ASC')
|
90
|
+
if rails_3 || rails_4_or_above
|
91
|
+
default_scope { order('pos ASC') }
|
79
92
|
else
|
80
93
|
default_scope :order => 'pos ASC'
|
81
94
|
end
|
82
95
|
end
|
83
96
|
|
84
97
|
class CallbackMixin < ActiveRecord::Base
|
85
|
-
|
98
|
+
self.table_name = 'mixins'
|
86
99
|
acts_as_silent_list :column => "pos"
|
87
100
|
|
88
101
|
before_save :store_before_save
|
@@ -132,25 +145,25 @@ class ZeroBasedTest < Test::Unit::TestCase
|
|
132
145
|
end
|
133
146
|
|
134
147
|
def test_reordering
|
135
|
-
assert_equal [1, 2, 3, 4], ZeroBasedMixin.
|
148
|
+
assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order(:pos).map(&:id)
|
136
149
|
|
137
150
|
ListMixin.find(2).move_lower
|
138
|
-
assert_equal [1, 3, 2, 4], ZeroBasedMixin.
|
151
|
+
assert_equal [1, 3, 2, 4], ZeroBasedMixin.where(parent_id: 5).order(:pos).map(&:id)
|
139
152
|
|
140
153
|
ListMixin.find(2).move_higher
|
141
|
-
assert_equal [1, 2, 3, 4], ZeroBasedMixin.
|
154
|
+
assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order(:pos).map(&:id)
|
142
155
|
|
143
156
|
ListMixin.find(1).move_to_bottom
|
144
|
-
assert_equal [2, 3, 4, 1], ZeroBasedMixin.
|
157
|
+
assert_equal [2, 3, 4, 1], ZeroBasedMixin.where(parent_id: 5).order(:pos).map(&:id)
|
145
158
|
|
146
159
|
ListMixin.find(1).move_to_top
|
147
|
-
assert_equal [1, 2, 3, 4], ZeroBasedMixin.
|
160
|
+
assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order(:pos).map(&:id)
|
148
161
|
|
149
162
|
ListMixin.find(2).move_to_bottom
|
150
|
-
assert_equal [1, 3, 4, 2], ZeroBasedMixin.
|
163
|
+
assert_equal [1, 3, 4, 2], ZeroBasedMixin.where(parent_id: 5).order(:pos).map(&:id)
|
151
164
|
|
152
165
|
ListMixin.find(4).move_to_top
|
153
|
-
assert_equal [4, 1, 3, 2], ZeroBasedMixin.
|
166
|
+
assert_equal [4, 1, 3, 2], ZeroBasedMixin.where(parent_id: 5).order(:pos).map(&:id)
|
154
167
|
end
|
155
168
|
|
156
169
|
def test_insert_at
|
@@ -203,31 +216,31 @@ class ListTest < Test::Unit::TestCase
|
|
203
216
|
end
|
204
217
|
|
205
218
|
def test_reordering
|
206
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
219
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
207
220
|
|
208
221
|
ListMixin.find(2).move_lower
|
209
|
-
assert_equal [1, 3, 2, 4], ListMixin.
|
222
|
+
assert_equal [1, 3, 2, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
210
223
|
|
211
224
|
ListMixin.find(2).move_higher
|
212
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
225
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
213
226
|
|
214
227
|
ListMixin.find(1).move_to_bottom
|
215
|
-
assert_equal [2, 3, 4, 1], ListMixin.
|
228
|
+
assert_equal [2, 3, 4, 1], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
216
229
|
|
217
230
|
ListMixin.find(1).move_to_top
|
218
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
231
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
219
232
|
|
220
233
|
ListMixin.find(2).move_to_bottom
|
221
|
-
assert_equal [1, 3, 4, 2], ListMixin.
|
234
|
+
assert_equal [1, 3, 4, 2], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
222
235
|
|
223
236
|
ListMixin.find(4).move_to_top
|
224
|
-
assert_equal [4, 1, 3, 2], ListMixin.
|
237
|
+
assert_equal [4, 1, 3, 2], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
225
238
|
end
|
226
239
|
|
227
240
|
def test_move_to_bottom_with_next_to_last_item
|
228
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
241
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
229
242
|
ListMixin.find(3).move_to_bottom
|
230
|
-
assert_equal [1, 2, 4, 3], ListMixin.
|
243
|
+
assert_equal [1, 2, 4, 3], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
231
244
|
end
|
232
245
|
|
233
246
|
def test_next_prev
|
@@ -301,11 +314,11 @@ class ListTest < Test::Unit::TestCase
|
|
301
314
|
end
|
302
315
|
|
303
316
|
def test_delete_middle
|
304
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
317
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
305
318
|
|
306
319
|
ListMixin.find(2).destroy
|
307
320
|
|
308
|
-
assert_equal [1, 3, 4], ListMixin.
|
321
|
+
assert_equal [1, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
309
322
|
|
310
323
|
assert_equal 1, ListMixin.find(1).pos
|
311
324
|
assert_equal 2, ListMixin.find(3).pos
|
@@ -313,7 +326,7 @@ class ListTest < Test::Unit::TestCase
|
|
313
326
|
|
314
327
|
ListMixin.find(1).destroy
|
315
328
|
|
316
|
-
assert_equal [3, 4], ListMixin.
|
329
|
+
assert_equal [3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
317
330
|
|
318
331
|
assert_equal 1, ListMixin.find(3).pos
|
319
332
|
assert_equal 2, ListMixin.find(4).pos
|
@@ -329,7 +342,7 @@ class ListTest < Test::Unit::TestCase
|
|
329
342
|
def test_nil_scope
|
330
343
|
new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create
|
331
344
|
new2.move_higher
|
332
|
-
assert_equal [new2, new1, new3], ListMixin.
|
345
|
+
assert_equal [new2, new1, new3], ListMixin.where(parent_id: nil).order(:pos)
|
333
346
|
end
|
334
347
|
|
335
348
|
def test_remove_from_list_should_then_fail_in_list?
|
@@ -339,11 +352,11 @@ class ListTest < Test::Unit::TestCase
|
|
339
352
|
end
|
340
353
|
|
341
354
|
def test_remove_from_list_should_set_position_to_nil
|
342
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
355
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
343
356
|
|
344
357
|
ListMixin.find(2).remove_from_list
|
345
358
|
|
346
|
-
assert_equal [2, 1, 3, 4], ListMixin.
|
359
|
+
assert_equal [2, 1, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
347
360
|
|
348
361
|
assert_equal 1, ListMixin.find(1).pos
|
349
362
|
assert_equal nil, ListMixin.find(2).pos
|
@@ -352,12 +365,12 @@ class ListTest < Test::Unit::TestCase
|
|
352
365
|
end
|
353
366
|
|
354
367
|
def test_remove_before_destroy_does_not_shift_lower_items_twice
|
355
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
368
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
356
369
|
|
357
370
|
ListMixin.find(2).remove_from_list
|
358
371
|
ListMixin.find(2).destroy
|
359
372
|
|
360
|
-
assert_equal [1, 3, 4], ListMixin.
|
373
|
+
assert_equal [1, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
361
374
|
|
362
375
|
assert_equal 1, ListMixin.find(1).pos
|
363
376
|
assert_equal 2, ListMixin.find(3).pos
|
@@ -365,7 +378,7 @@ class ListTest < Test::Unit::TestCase
|
|
365
378
|
end
|
366
379
|
|
367
380
|
def test_before_destroy_callbacks_do_not_update_position_to_nil_before_deleting_the_record
|
368
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
381
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
369
382
|
|
370
383
|
# We need to trigger all the before_destroy callbacks without actually
|
371
384
|
# destroying the record so we can see the affect the callbacks have on
|
@@ -374,13 +387,19 @@ class ListTest < Test::Unit::TestCase
|
|
374
387
|
list = ListMixin.find(2)
|
375
388
|
if list.respond_to?(:run_callbacks)
|
376
389
|
# Refactored to work according to Rails3 ActiveRSupport Callbacks <http://api.rubyonrails.org/classes/ActiveSupport/Callbacks.html>
|
377
|
-
|
378
|
-
|
390
|
+
# Refactored again to also work with Rails4 which does not allow to pick only before callbacks to fire
|
391
|
+
if rails_3
|
392
|
+
list.run_callbacks :destroy, :before
|
393
|
+
elsif rails_4_or_above
|
394
|
+
list.run_callbacks :destroy
|
395
|
+
else
|
396
|
+
list.run_callbacks :before_destroy
|
397
|
+
end
|
379
398
|
else
|
380
399
|
list.send(:callback, :before_destroy)
|
381
400
|
end
|
382
401
|
|
383
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
402
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
384
403
|
|
385
404
|
assert_equal 1, ListMixin.find(1).pos
|
386
405
|
assert_equal 2, ListMixin.find(2).pos
|
@@ -389,32 +408,32 @@ class ListTest < Test::Unit::TestCase
|
|
389
408
|
end
|
390
409
|
|
391
410
|
def test_before_create_callback_adds_to_bottom
|
392
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
411
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
393
412
|
|
394
413
|
new = ListMixin.create(:parent_id => 5)
|
395
414
|
assert_equal 5, new.pos
|
396
415
|
assert !new.first?
|
397
416
|
assert new.last?
|
398
417
|
|
399
|
-
assert_equal [1, 2, 3, 4, 5], ListMixin.
|
418
|
+
assert_equal [1, 2, 3, 4, 5], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
400
419
|
end
|
401
420
|
|
402
421
|
def test_before_create_callback_adds_to_given_position
|
403
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
422
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
404
423
|
|
405
424
|
new = ListMixin.create(:pos => 1, :parent_id => 5)
|
406
425
|
assert_equal 1, new.pos
|
407
426
|
assert new.first?
|
408
427
|
assert !new.last?
|
409
428
|
|
410
|
-
assert_equal [5, 1, 2, 3, 4], ListMixin.
|
429
|
+
assert_equal [5, 1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
411
430
|
|
412
431
|
new = ListMixin.create(:pos => 3, :parent_id => 5)
|
413
432
|
assert_equal 3, new.pos
|
414
433
|
assert !new.first?
|
415
434
|
assert !new.last?
|
416
435
|
|
417
|
-
assert_equal [5, 1, 6, 2, 3, 4], ListMixin.
|
436
|
+
assert_equal [5, 1, 6, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
418
437
|
end
|
419
438
|
|
420
439
|
def test_insert_at_top
|
@@ -423,7 +442,7 @@ class ListTest < Test::Unit::TestCase
|
|
423
442
|
new.insert_at_top
|
424
443
|
|
425
444
|
assert new.first?
|
426
|
-
assert_equal [5, 1, 2, 3, 4], ListMixin.
|
445
|
+
assert_equal [5, 1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
427
446
|
end
|
428
447
|
|
429
448
|
def test_insert_at_bottom
|
@@ -432,7 +451,7 @@ class ListTest < Test::Unit::TestCase
|
|
432
451
|
new.insert_at_bottom
|
433
452
|
|
434
453
|
assert new.last?
|
435
|
-
assert_equal [1, 2, 3, 4, 5], ListMixin.
|
454
|
+
assert_equal [1, 2, 3, 4, 5], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
436
455
|
end
|
437
456
|
end
|
438
457
|
|
@@ -448,31 +467,31 @@ class ListSubTest < Test::Unit::TestCase
|
|
448
467
|
end
|
449
468
|
|
450
469
|
def test_reordering
|
451
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
470
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
452
471
|
|
453
472
|
ListMixin.find(2).move_lower
|
454
|
-
assert_equal [1, 3, 2, 4], ListMixin.
|
473
|
+
assert_equal [1, 3, 2, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
455
474
|
|
456
475
|
ListMixin.find(2).move_higher
|
457
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
476
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
458
477
|
|
459
478
|
ListMixin.find(1).move_to_bottom
|
460
|
-
assert_equal [2, 3, 4, 1], ListMixin.
|
479
|
+
assert_equal [2, 3, 4, 1], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
461
480
|
|
462
481
|
ListMixin.find(1).move_to_top
|
463
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
482
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
464
483
|
|
465
484
|
ListMixin.find(2).move_to_bottom
|
466
|
-
assert_equal [1, 3, 4, 2], ListMixin.
|
485
|
+
assert_equal [1, 3, 4, 2], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
467
486
|
|
468
487
|
ListMixin.find(4).move_to_top
|
469
|
-
assert_equal [4, 1, 3, 2], ListMixin.
|
488
|
+
assert_equal [4, 1, 3, 2], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
470
489
|
end
|
471
490
|
|
472
491
|
def test_move_to_bottom_with_next_to_last_item
|
473
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
492
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
474
493
|
ListMixin.find(3).move_to_bottom
|
475
|
-
assert_equal [1, 2, 4, 3], ListMixin.
|
494
|
+
assert_equal [1, 2, 4, 3], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
476
495
|
end
|
477
496
|
|
478
497
|
def test_next_prev
|
@@ -524,11 +543,11 @@ class ListSubTest < Test::Unit::TestCase
|
|
524
543
|
end
|
525
544
|
|
526
545
|
def test_delete_middle
|
527
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
546
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
528
547
|
|
529
548
|
ListMixin.find(2).destroy
|
530
549
|
|
531
|
-
assert_equal [1, 3, 4], ListMixin.
|
550
|
+
assert_equal [1, 3, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
532
551
|
|
533
552
|
assert_equal 1, ListMixin.find(1).pos
|
534
553
|
assert_equal 2, ListMixin.find(3).pos
|
@@ -536,7 +555,7 @@ class ListSubTest < Test::Unit::TestCase
|
|
536
555
|
|
537
556
|
ListMixin.find(1).destroy
|
538
557
|
|
539
|
-
assert_equal [3, 4], ListMixin.
|
558
|
+
assert_equal [3, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
540
559
|
|
541
560
|
assert_equal 1, ListMixin.find(3).pos
|
542
561
|
assert_equal 2, ListMixin.find(4).pos
|
@@ -556,31 +575,31 @@ class ArrayScopeListTest < Test::Unit::TestCase
|
|
556
575
|
end
|
557
576
|
|
558
577
|
def test_reordering
|
559
|
-
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.
|
578
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
560
579
|
|
561
580
|
ArrayScopeListMixin.find(2).move_lower
|
562
|
-
assert_equal [1, 3, 2, 4], ArrayScopeListMixin.
|
581
|
+
assert_equal [1, 3, 2, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
563
582
|
|
564
583
|
ArrayScopeListMixin.find(2).move_higher
|
565
|
-
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.
|
584
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
566
585
|
|
567
586
|
ArrayScopeListMixin.find(1).move_to_bottom
|
568
|
-
assert_equal [2, 3, 4, 1], ArrayScopeListMixin.
|
587
|
+
assert_equal [2, 3, 4, 1], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
569
588
|
|
570
589
|
ArrayScopeListMixin.find(1).move_to_top
|
571
|
-
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.
|
590
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
572
591
|
|
573
592
|
ArrayScopeListMixin.find(2).move_to_bottom
|
574
|
-
assert_equal [1, 3, 4, 2], ArrayScopeListMixin.
|
593
|
+
assert_equal [1, 3, 4, 2], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
575
594
|
|
576
595
|
ArrayScopeListMixin.find(4).move_to_top
|
577
|
-
assert_equal [4, 1, 3, 2], ArrayScopeListMixin.
|
596
|
+
assert_equal [4, 1, 3, 2], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
578
597
|
end
|
579
598
|
|
580
599
|
def test_move_to_bottom_with_next_to_last_item
|
581
|
-
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.
|
600
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
582
601
|
ArrayScopeListMixin.find(3).move_to_bottom
|
583
|
-
assert_equal [1, 2, 4, 3], ArrayScopeListMixin.
|
602
|
+
assert_equal [1, 2, 4, 3], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
584
603
|
end
|
585
604
|
|
586
605
|
def test_next_prev
|
@@ -654,11 +673,11 @@ class ArrayScopeListTest < Test::Unit::TestCase
|
|
654
673
|
end
|
655
674
|
|
656
675
|
def test_delete_middle
|
657
|
-
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.
|
676
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
658
677
|
|
659
678
|
ArrayScopeListMixin.find(2).destroy
|
660
679
|
|
661
|
-
assert_equal [1, 3, 4], ArrayScopeListMixin.
|
680
|
+
assert_equal [1, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
662
681
|
|
663
682
|
assert_equal 1, ArrayScopeListMixin.find(1).pos
|
664
683
|
assert_equal 2, ArrayScopeListMixin.find(3).pos
|
@@ -666,7 +685,7 @@ class ArrayScopeListTest < Test::Unit::TestCase
|
|
666
685
|
|
667
686
|
ArrayScopeListMixin.find(1).destroy
|
668
687
|
|
669
|
-
assert_equal [3, 4], ArrayScopeListMixin.
|
688
|
+
assert_equal [3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
670
689
|
|
671
690
|
assert_equal 1, ArrayScopeListMixin.find(3).pos
|
672
691
|
assert_equal 2, ArrayScopeListMixin.find(4).pos
|
@@ -679,11 +698,11 @@ class ArrayScopeListTest < Test::Unit::TestCase
|
|
679
698
|
end
|
680
699
|
|
681
700
|
def test_remove_from_list_should_set_position_to_nil
|
682
|
-
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.
|
701
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
683
702
|
|
684
703
|
ArrayScopeListMixin.find(2).remove_from_list
|
685
704
|
|
686
|
-
assert_equal [2, 1, 3, 4], ArrayScopeListMixin.
|
705
|
+
assert_equal [2, 1, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
687
706
|
|
688
707
|
assert_equal 1, ArrayScopeListMixin.find(1).pos
|
689
708
|
assert_equal nil, ArrayScopeListMixin.find(2).pos
|
@@ -692,12 +711,12 @@ class ArrayScopeListTest < Test::Unit::TestCase
|
|
692
711
|
end
|
693
712
|
|
694
713
|
def test_remove_before_destroy_does_not_shift_lower_items_twice
|
695
|
-
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.
|
714
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
696
715
|
|
697
716
|
ArrayScopeListMixin.find(2).remove_from_list
|
698
717
|
ArrayScopeListMixin.find(2).destroy
|
699
718
|
|
700
|
-
assert_equal [1, 3, 4], ArrayScopeListMixin.
|
719
|
+
assert_equal [1, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
701
720
|
|
702
721
|
assert_equal 1, ArrayScopeListMixin.find(1).pos
|
703
722
|
assert_equal 2, ArrayScopeListMixin.find(3).pos
|
@@ -738,26 +757,26 @@ class ZeroBasedTestWithDefault < Test::Unit::TestCase
|
|
738
757
|
assert new.last?
|
739
758
|
end
|
740
759
|
|
741
|
-
|
742
|
-
assert_equal [1, 2, 3, 4], ZeroBasedMixin.
|
760
|
+
def test_reordering
|
761
|
+
assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order(:pos).map(&:id)
|
743
762
|
|
744
763
|
ListMixin.find(2).move_lower
|
745
|
-
assert_equal [1, 3, 2, 4], ZeroBasedMixin.
|
764
|
+
assert_equal [1, 3, 2, 4], ZeroBasedMixin.where(parent_id: 5).order(:pos).map(&:id)
|
746
765
|
|
747
766
|
ListMixin.find(2).move_higher
|
748
|
-
assert_equal [1, 2, 3, 4], ZeroBasedMixin.
|
767
|
+
assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order(:pos).map(&:id)
|
749
768
|
|
750
769
|
ListMixin.find(1).move_to_bottom
|
751
|
-
assert_equal [2, 3, 4, 1], ZeroBasedMixin.
|
770
|
+
assert_equal [2, 3, 4, 1], ZeroBasedMixin.where(parent_id: 5).order(:pos).map(&:id)
|
752
771
|
|
753
772
|
ListMixin.find(1).move_to_top
|
754
|
-
assert_equal [1, 2, 3, 4], ZeroBasedMixin.
|
773
|
+
assert_equal [1, 2, 3, 4], ZeroBasedMixin.where(parent_id: 5).order(:pos).map(&:id)
|
755
774
|
|
756
775
|
ListMixin.find(2).move_to_bottom
|
757
|
-
assert_equal [1, 3, 4, 2], ZeroBasedMixin.
|
776
|
+
assert_equal [1, 3, 4, 2], ZeroBasedMixin.where(parent_id: 5).order(:pos).map(&:id)
|
758
777
|
|
759
778
|
ListMixin.find(4).move_to_top
|
760
|
-
assert_equal [4, 1, 3, 2], ZeroBasedMixin.
|
779
|
+
assert_equal [4, 1, 3, 2], ZeroBasedMixin.where(parent_id: 5).order(:pos).map(&:id)
|
761
780
|
end
|
762
781
|
|
763
782
|
def test_insert_at
|
@@ -810,31 +829,31 @@ class ListTestWithDefault < Test::Unit::TestCase
|
|
810
829
|
end
|
811
830
|
|
812
831
|
def test_reordering
|
813
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
832
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
814
833
|
|
815
834
|
ListMixin.find(2).move_lower
|
816
|
-
assert_equal [1, 3, 2, 4], ListMixin.
|
835
|
+
assert_equal [1, 3, 2, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
817
836
|
|
818
837
|
ListMixin.find(2).move_higher
|
819
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
838
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
820
839
|
|
821
840
|
ListMixin.find(1).move_to_bottom
|
822
|
-
assert_equal [2, 3, 4, 1], ListMixin.
|
841
|
+
assert_equal [2, 3, 4, 1], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
823
842
|
|
824
843
|
ListMixin.find(1).move_to_top
|
825
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
844
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
826
845
|
|
827
846
|
ListMixin.find(2).move_to_bottom
|
828
|
-
assert_equal [1, 3, 4, 2], ListMixin.
|
847
|
+
assert_equal [1, 3, 4, 2], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
829
848
|
|
830
849
|
ListMixin.find(4).move_to_top
|
831
|
-
assert_equal [4, 1, 3, 2], ListMixin.
|
850
|
+
assert_equal [4, 1, 3, 2], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
832
851
|
end
|
833
852
|
|
834
853
|
def test_move_to_bottom_with_next_to_last_item
|
835
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
854
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
836
855
|
ListMixin.find(3).move_to_bottom
|
837
|
-
assert_equal [1, 2, 4, 3], ListMixin.
|
856
|
+
assert_equal [1, 2, 4, 3], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
838
857
|
end
|
839
858
|
|
840
859
|
def test_next_prev
|
@@ -908,11 +927,11 @@ class ListTestWithDefault < Test::Unit::TestCase
|
|
908
927
|
end
|
909
928
|
|
910
929
|
def test_delete_middle
|
911
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
930
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
912
931
|
|
913
932
|
ListMixin.find(2).destroy
|
914
933
|
|
915
|
-
assert_equal [1, 3, 4], ListMixin.
|
934
|
+
assert_equal [1, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
916
935
|
|
917
936
|
assert_equal 1, ListMixin.find(1).pos
|
918
937
|
assert_equal 2, ListMixin.find(3).pos
|
@@ -920,7 +939,7 @@ class ListTestWithDefault < Test::Unit::TestCase
|
|
920
939
|
|
921
940
|
ListMixin.find(1).destroy
|
922
941
|
|
923
|
-
assert_equal [3, 4], ListMixin.
|
942
|
+
assert_equal [3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
924
943
|
|
925
944
|
assert_equal 1, ListMixin.find(3).pos
|
926
945
|
assert_equal 2, ListMixin.find(4).pos
|
@@ -936,7 +955,7 @@ class ListTestWithDefault < Test::Unit::TestCase
|
|
936
955
|
def test_nil_scope
|
937
956
|
new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create
|
938
957
|
new2.move_higher
|
939
|
-
assert_equal [new2, new1, new3], ListMixin.
|
958
|
+
assert_equal [new2, new1, new3], ListMixin.where(parent_id: nil).order(:pos)
|
940
959
|
end
|
941
960
|
|
942
961
|
def test_remove_from_list_should_then_fail_in_list?
|
@@ -946,11 +965,11 @@ class ListTestWithDefault < Test::Unit::TestCase
|
|
946
965
|
end
|
947
966
|
|
948
967
|
def test_remove_from_list_should_set_position_to_nil
|
949
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
968
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
950
969
|
|
951
970
|
ListMixin.find(2).remove_from_list
|
952
971
|
|
953
|
-
assert_equal [2, 1, 3, 4], ListMixin.
|
972
|
+
assert_equal [2, 1, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
954
973
|
|
955
974
|
assert_equal 1, ListMixin.find(1).pos
|
956
975
|
assert_equal nil, ListMixin.find(2).pos
|
@@ -959,12 +978,12 @@ class ListTestWithDefault < Test::Unit::TestCase
|
|
959
978
|
end
|
960
979
|
|
961
980
|
def test_remove_before_destroy_does_not_shift_lower_items_twice
|
962
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
981
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
963
982
|
|
964
983
|
ListMixin.find(2).remove_from_list
|
965
984
|
ListMixin.find(2).destroy
|
966
985
|
|
967
|
-
assert_equal [1, 3, 4], ListMixin.
|
986
|
+
assert_equal [1, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
968
987
|
|
969
988
|
assert_equal 1, ListMixin.find(1).pos
|
970
989
|
assert_equal 2, ListMixin.find(3).pos
|
@@ -972,7 +991,7 @@ class ListTestWithDefault < Test::Unit::TestCase
|
|
972
991
|
end
|
973
992
|
|
974
993
|
def test_before_destroy_callbacks_do_not_update_position_to_nil_before_deleting_the_record
|
975
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
994
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
976
995
|
|
977
996
|
# We need to trigger all the before_destroy callbacks without actually
|
978
997
|
# destroying the record so we can see the affect the callbacks have on
|
@@ -981,13 +1000,19 @@ class ListTestWithDefault < Test::Unit::TestCase
|
|
981
1000
|
list = ListMixin.find(2)
|
982
1001
|
if list.respond_to?(:run_callbacks)
|
983
1002
|
# Refactored to work according to Rails3 ActiveRSupport Callbacks <http://api.rubyonrails.org/classes/ActiveSupport/Callbacks.html>
|
984
|
-
|
985
|
-
|
1003
|
+
# Refactored again to also work with Rails4 which does not allow to pick only before callbacks to fire
|
1004
|
+
if rails_3
|
1005
|
+
list.run_callbacks :destroy, :before
|
1006
|
+
elsif rails_4_or_above
|
1007
|
+
list.run_callbacks :destroy
|
1008
|
+
else
|
1009
|
+
list.run_callbacks :before_destroy
|
1010
|
+
end
|
986
1011
|
else
|
987
1012
|
list.send(:callback, :before_destroy)
|
988
1013
|
end
|
989
1014
|
|
990
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
1015
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
991
1016
|
|
992
1017
|
assert_equal 1, ListMixin.find(1).pos
|
993
1018
|
assert_equal 2, ListMixin.find(2).pos
|
@@ -996,32 +1021,32 @@ class ListTestWithDefault < Test::Unit::TestCase
|
|
996
1021
|
end
|
997
1022
|
|
998
1023
|
def test_before_create_callback_adds_to_bottom
|
999
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
1024
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
1000
1025
|
|
1001
1026
|
new = ListMixin.create(:parent_id => 5)
|
1002
1027
|
assert_equal 5, new.pos
|
1003
1028
|
assert !new.first?
|
1004
1029
|
assert new.last?
|
1005
1030
|
|
1006
|
-
assert_equal [1, 2, 3, 4, 5], ListMixin.
|
1031
|
+
assert_equal [1, 2, 3, 4, 5], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
1007
1032
|
end
|
1008
1033
|
|
1009
1034
|
def test_before_create_callback_adds_to_given_position
|
1010
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
1035
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
1011
1036
|
|
1012
1037
|
new = ListMixin.create(:pos => 1, :parent_id => 5)
|
1013
1038
|
assert_equal 1, new.pos
|
1014
1039
|
assert new.first?
|
1015
1040
|
assert !new.last?
|
1016
1041
|
|
1017
|
-
assert_equal [5, 1, 2, 3, 4], ListMixin.
|
1042
|
+
assert_equal [5, 1, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
1018
1043
|
|
1019
1044
|
new = ListMixin.create(:pos => 3, :parent_id => 5)
|
1020
1045
|
assert_equal 3, new.pos
|
1021
1046
|
assert !new.first?
|
1022
1047
|
assert !new.last?
|
1023
1048
|
|
1024
|
-
assert_equal [5, 1, 6, 2, 3, 4], ListMixin.
|
1049
|
+
assert_equal [5, 1, 6, 2, 3, 4], ListMixin.where(parent_id: 5).order(:pos).map(&:id)
|
1025
1050
|
end
|
1026
1051
|
end
|
1027
1052
|
|
@@ -1037,31 +1062,31 @@ class ListSubTestWithDefault < Test::Unit::TestCase
|
|
1037
1062
|
end
|
1038
1063
|
|
1039
1064
|
def test_reordering
|
1040
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
1065
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
1041
1066
|
|
1042
1067
|
ListMixin.find(2).move_lower
|
1043
|
-
assert_equal [1, 3, 2, 4], ListMixin.
|
1068
|
+
assert_equal [1, 3, 2, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
1044
1069
|
|
1045
1070
|
ListMixin.find(2).move_higher
|
1046
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
1071
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
1047
1072
|
|
1048
1073
|
ListMixin.find(1).move_to_bottom
|
1049
|
-
assert_equal [2, 3, 4, 1], ListMixin.
|
1074
|
+
assert_equal [2, 3, 4, 1], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
1050
1075
|
|
1051
1076
|
ListMixin.find(1).move_to_top
|
1052
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
1077
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
1053
1078
|
|
1054
1079
|
ListMixin.find(2).move_to_bottom
|
1055
|
-
assert_equal [1, 3, 4, 2], ListMixin.
|
1080
|
+
assert_equal [1, 3, 4, 2], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
1056
1081
|
|
1057
1082
|
ListMixin.find(4).move_to_top
|
1058
|
-
assert_equal [4, 1, 3, 2], ListMixin.
|
1083
|
+
assert_equal [4, 1, 3, 2], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
1059
1084
|
end
|
1060
1085
|
|
1061
1086
|
def test_move_to_bottom_with_next_to_last_item
|
1062
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
1087
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
1063
1088
|
ListMixin.find(3).move_to_bottom
|
1064
|
-
assert_equal [1, 2, 4, 3], ListMixin.
|
1089
|
+
assert_equal [1, 2, 4, 3], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
1065
1090
|
end
|
1066
1091
|
|
1067
1092
|
def test_next_prev
|
@@ -1113,11 +1138,11 @@ class ListSubTestWithDefault < Test::Unit::TestCase
|
|
1113
1138
|
end
|
1114
1139
|
|
1115
1140
|
def test_delete_middle
|
1116
|
-
assert_equal [1, 2, 3, 4], ListMixin.
|
1141
|
+
assert_equal [1, 2, 3, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
1117
1142
|
|
1118
1143
|
ListMixin.find(2).destroy
|
1119
1144
|
|
1120
|
-
assert_equal [1, 3, 4], ListMixin.
|
1145
|
+
assert_equal [1, 3, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
1121
1146
|
|
1122
1147
|
assert_equal 1, ListMixin.find(1).pos
|
1123
1148
|
assert_equal 2, ListMixin.find(3).pos
|
@@ -1125,7 +1150,7 @@ class ListSubTestWithDefault < Test::Unit::TestCase
|
|
1125
1150
|
|
1126
1151
|
ListMixin.find(1).destroy
|
1127
1152
|
|
1128
|
-
assert_equal [3, 4], ListMixin.
|
1153
|
+
assert_equal [3, 4], ListMixin.where(parent_id: 5000).order(:pos).map(&:id)
|
1129
1154
|
|
1130
1155
|
assert_equal 1, ListMixin.find(3).pos
|
1131
1156
|
assert_equal 2, ListMixin.find(4).pos
|
@@ -1145,31 +1170,31 @@ class ArrayScopeListTestWithDefault < Test::Unit::TestCase
|
|
1145
1170
|
end
|
1146
1171
|
|
1147
1172
|
def test_reordering
|
1148
|
-
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.
|
1173
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1149
1174
|
|
1150
1175
|
ArrayScopeListMixin.find(2).move_lower
|
1151
|
-
assert_equal [1, 3, 2, 4], ArrayScopeListMixin.
|
1176
|
+
assert_equal [1, 3, 2, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1152
1177
|
|
1153
1178
|
ArrayScopeListMixin.find(2).move_higher
|
1154
|
-
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.
|
1179
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1155
1180
|
|
1156
1181
|
ArrayScopeListMixin.find(1).move_to_bottom
|
1157
|
-
assert_equal [2, 3, 4, 1], ArrayScopeListMixin.
|
1182
|
+
assert_equal [2, 3, 4, 1], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1158
1183
|
|
1159
1184
|
ArrayScopeListMixin.find(1).move_to_top
|
1160
|
-
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.
|
1185
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1161
1186
|
|
1162
1187
|
ArrayScopeListMixin.find(2).move_to_bottom
|
1163
|
-
assert_equal [1, 3, 4, 2], ArrayScopeListMixin.
|
1188
|
+
assert_equal [1, 3, 4, 2], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1164
1189
|
|
1165
1190
|
ArrayScopeListMixin.find(4).move_to_top
|
1166
|
-
assert_equal [4, 1, 3, 2], ArrayScopeListMixin.
|
1191
|
+
assert_equal [4, 1, 3, 2], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1167
1192
|
end
|
1168
1193
|
|
1169
1194
|
def test_move_to_bottom_with_next_to_last_item
|
1170
|
-
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.
|
1195
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1171
1196
|
ArrayScopeListMixin.find(3).move_to_bottom
|
1172
|
-
assert_equal [1, 2, 4, 3], ArrayScopeListMixin.
|
1197
|
+
assert_equal [1, 2, 4, 3], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1173
1198
|
end
|
1174
1199
|
|
1175
1200
|
def test_next_prev
|
@@ -1243,11 +1268,11 @@ class ArrayScopeListTestWithDefault < Test::Unit::TestCase
|
|
1243
1268
|
end
|
1244
1269
|
|
1245
1270
|
def test_delete_middle
|
1246
|
-
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.
|
1271
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1247
1272
|
|
1248
1273
|
ArrayScopeListMixin.find(2).destroy
|
1249
1274
|
|
1250
|
-
assert_equal [1, 3, 4], ArrayScopeListMixin.
|
1275
|
+
assert_equal [1, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1251
1276
|
|
1252
1277
|
assert_equal 1, ArrayScopeListMixin.find(1).pos
|
1253
1278
|
assert_equal 2, ArrayScopeListMixin.find(3).pos
|
@@ -1255,7 +1280,7 @@ class ArrayScopeListTestWithDefault < Test::Unit::TestCase
|
|
1255
1280
|
|
1256
1281
|
ArrayScopeListMixin.find(1).destroy
|
1257
1282
|
|
1258
|
-
assert_equal [3, 4], ArrayScopeListMixin.
|
1283
|
+
assert_equal [3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1259
1284
|
|
1260
1285
|
assert_equal 1, ArrayScopeListMixin.find(3).pos
|
1261
1286
|
assert_equal 2, ArrayScopeListMixin.find(4).pos
|
@@ -1268,11 +1293,11 @@ class ArrayScopeListTestWithDefault < Test::Unit::TestCase
|
|
1268
1293
|
end
|
1269
1294
|
|
1270
1295
|
def test_remove_from_list_should_set_position_to_nil
|
1271
|
-
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.
|
1296
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1272
1297
|
|
1273
1298
|
ArrayScopeListMixin.find(2).remove_from_list
|
1274
1299
|
|
1275
|
-
assert_equal [2, 1, 3, 4], ArrayScopeListMixin.
|
1300
|
+
assert_equal [2, 1, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1276
1301
|
|
1277
1302
|
assert_equal 1, ArrayScopeListMixin.find(1).pos
|
1278
1303
|
assert_equal nil, ArrayScopeListMixin.find(2).pos
|
@@ -1281,12 +1306,12 @@ class ArrayScopeListTestWithDefault < Test::Unit::TestCase
|
|
1281
1306
|
end
|
1282
1307
|
|
1283
1308
|
def test_remove_before_destroy_does_not_shift_lower_items_twice
|
1284
|
-
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.
|
1309
|
+
assert_equal [1, 2, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1285
1310
|
|
1286
1311
|
ArrayScopeListMixin.find(2).remove_from_list
|
1287
1312
|
ArrayScopeListMixin.find(2).destroy
|
1288
1313
|
|
1289
|
-
assert_equal [1, 3, 4], ArrayScopeListMixin.
|
1314
|
+
assert_equal [1, 3, 4], ArrayScopeListMixin.where(parent_id: 5, parent_type: :ParentClass).order(:pos).map(&:id)
|
1290
1315
|
|
1291
1316
|
assert_equal 1, ArrayScopeListMixin.find(1).pos
|
1292
1317
|
assert_equal 2, ArrayScopeListMixin.find(3).pos
|
@@ -1323,25 +1348,25 @@ class DefaultScopedTest < Test::Unit::TestCase
|
|
1323
1348
|
end
|
1324
1349
|
|
1325
1350
|
def test_reordering
|
1326
|
-
assert_equal [1, 2, 3, 4], DefaultScopedMixin.
|
1351
|
+
assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
|
1327
1352
|
|
1328
1353
|
DefaultScopedMixin.find(2).move_lower
|
1329
|
-
assert_equal [1, 3, 2, 4], DefaultScopedMixin.
|
1354
|
+
assert_equal [1, 3, 2, 4], DefaultScopedMixin.all.map(&:id)
|
1330
1355
|
|
1331
1356
|
DefaultScopedMixin.find(2).move_higher
|
1332
|
-
assert_equal [1, 2, 3, 4], DefaultScopedMixin.
|
1357
|
+
assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
|
1333
1358
|
|
1334
1359
|
DefaultScopedMixin.find(1).move_to_bottom
|
1335
|
-
assert_equal [2, 3, 4, 1], DefaultScopedMixin.
|
1360
|
+
assert_equal [2, 3, 4, 1], DefaultScopedMixin.all.map(&:id)
|
1336
1361
|
|
1337
1362
|
DefaultScopedMixin.find(1).move_to_top
|
1338
|
-
assert_equal [1, 2, 3, 4], DefaultScopedMixin.
|
1363
|
+
assert_equal [1, 2, 3, 4], DefaultScopedMixin.all.map(&:id)
|
1339
1364
|
|
1340
1365
|
DefaultScopedMixin.find(2).move_to_bottom
|
1341
|
-
assert_equal [1, 3, 4, 2], DefaultScopedMixin.
|
1366
|
+
assert_equal [1, 3, 4, 2], DefaultScopedMixin.all.map(&:id)
|
1342
1367
|
|
1343
1368
|
DefaultScopedMixin.find(4).move_to_top
|
1344
|
-
assert_equal [4, 1, 3, 2], DefaultScopedMixin.
|
1369
|
+
assert_equal [4, 1, 3, 2], DefaultScopedMixin.all.map(&:id)
|
1345
1370
|
end
|
1346
1371
|
|
1347
1372
|
def test_insert_at
|