acts_as_list 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.rdoc +35 -0
- data/Rakefile +31 -0
- data/acts_as_list.gemspec +31 -0
- data/init.rb +2 -0
- data/lib/acts_as_list.rb +2 -254
- data/lib/acts_as_list/active_record/acts/list.rb +271 -0
- data/lib/acts_as_list/version.rb +7 -0
- data/test/helper.rb +15 -0
- data/test/test_list.rb +659 -0
- metadata +106 -31
- data/Manifest +0 -6
- data/README +0 -34
- data/acts-as-list.gemspec +0 -58
- data/tasks/deployment.rake +0 -2
- data/test/list_test.rb +0 -369
metadata
CHANGED
@@ -1,67 +1,142 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_list
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
|
-
-
|
13
|
+
- David Heinemeier Hansson
|
14
|
+
- Swanand Pagnis
|
15
|
+
- Quinn Chaffee
|
8
16
|
autorequire:
|
9
17
|
bindir: bin
|
10
18
|
cert_chain: []
|
11
19
|
|
12
|
-
date:
|
20
|
+
date: 2011-06-10 00:00:00 +05:30
|
13
21
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
|
17
|
-
|
22
|
+
dependencies:
|
23
|
+
- !ruby/object:Gem::Dependency
|
24
|
+
name: bundler
|
25
|
+
prerelease: false
|
26
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
28
|
+
requirements:
|
29
|
+
- - ~>
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
hash: 23
|
32
|
+
segments:
|
33
|
+
- 1
|
34
|
+
- 0
|
35
|
+
- 0
|
36
|
+
version: 1.0.0
|
37
|
+
type: :development
|
38
|
+
version_requirements: *id001
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
name: activerecord
|
41
|
+
prerelease: false
|
42
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
hash: 15571
|
48
|
+
segments:
|
49
|
+
- 1
|
50
|
+
- 15
|
51
|
+
- 4
|
52
|
+
- 7794
|
53
|
+
version: 1.15.4.7794
|
54
|
+
type: :development
|
55
|
+
version_requirements: *id002
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rdoc
|
58
|
+
prerelease: false
|
59
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
type: :development
|
69
|
+
version_requirements: *id003
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: sqlite3-ruby
|
72
|
+
prerelease: false
|
73
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 3
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
type: :development
|
83
|
+
version_requirements: *id004
|
84
|
+
description: This "acts_as" extension provides the capabilities for sorting and reordering a number of objects in a list. The class that has this specified needs to have a "position" column defined as an integer on the mapped database table.
|
85
|
+
email:
|
86
|
+
- swanand.pagnis@gmail.com
|
18
87
|
executables: []
|
19
88
|
|
20
89
|
extensions: []
|
21
90
|
|
22
|
-
extra_rdoc_files:
|
23
|
-
|
24
|
-
- README
|
25
|
-
- tasks/deployment.rake
|
91
|
+
extra_rdoc_files: []
|
92
|
+
|
26
93
|
files:
|
27
|
-
-
|
94
|
+
- .gemtest
|
95
|
+
- .gitignore
|
96
|
+
- Gemfile
|
97
|
+
- README.rdoc
|
98
|
+
- Rakefile
|
99
|
+
- acts_as_list.gemspec
|
100
|
+
- init.rb
|
28
101
|
- lib/acts_as_list.rb
|
29
|
-
-
|
30
|
-
-
|
31
|
-
-
|
32
|
-
- test/
|
102
|
+
- lib/acts_as_list/active_record/acts/list.rb
|
103
|
+
- lib/acts_as_list/version.rb
|
104
|
+
- test/helper.rb
|
105
|
+
- test/test_list.rb
|
33
106
|
has_rdoc: true
|
34
|
-
homepage: http://github.com/
|
107
|
+
homepage: http://github.com/swanandp/acts_as_list
|
35
108
|
licenses: []
|
36
109
|
|
37
110
|
post_install_message:
|
38
|
-
rdoc_options:
|
39
|
-
|
40
|
-
- --inline-source
|
41
|
-
- --title
|
42
|
-
- Acts-as-list
|
43
|
-
- --main
|
44
|
-
- README
|
111
|
+
rdoc_options: []
|
112
|
+
|
45
113
|
require_paths:
|
46
114
|
- lib
|
47
115
|
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
48
117
|
requirements:
|
49
118
|
- - ">="
|
50
119
|
- !ruby/object:Gem::Version
|
120
|
+
hash: 3
|
121
|
+
segments:
|
122
|
+
- 0
|
51
123
|
version: "0"
|
52
|
-
version:
|
53
124
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
none: false
|
54
126
|
requirements:
|
55
127
|
- - ">="
|
56
128
|
- !ruby/object:Gem::Version
|
129
|
+
hash: 3
|
130
|
+
segments:
|
131
|
+
- 0
|
57
132
|
version: "0"
|
58
|
-
version:
|
59
133
|
requirements: []
|
60
134
|
|
61
|
-
rubyforge_project:
|
62
|
-
rubygems_version: 1.3.
|
135
|
+
rubyforge_project: acts_as_list
|
136
|
+
rubygems_version: 1.3.7
|
63
137
|
signing_key:
|
64
|
-
specification_version:
|
65
|
-
summary:
|
138
|
+
specification_version: 3
|
139
|
+
summary: A gem allowing a active_record model to act_as_list.
|
66
140
|
test_files:
|
67
|
-
- test/
|
141
|
+
- test/helper.rb
|
142
|
+
- test/test_list.rb
|
data/Manifest
DELETED
data/README
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
ActsAsList
|
2
|
-
==========
|
3
|
-
|
4
|
-
This acts_as extension provides the capabilities for sorting and reordering a number of objects in a list. The class that has this specified needs to have a +position+ column defined as an integer on the mapped database table.
|
5
|
-
|
6
|
-
Install
|
7
|
-
=======
|
8
|
-
|
9
|
-
Specify it in your Rails config.
|
10
|
-
|
11
|
-
config.gem 'ryanb-acts-as-list', :lib => 'acts_as_list', :source => 'http://gems.github.com'
|
12
|
-
|
13
|
-
Then install it.
|
14
|
-
|
15
|
-
rake gems:install
|
16
|
-
|
17
|
-
|
18
|
-
Example
|
19
|
-
=======
|
20
|
-
|
21
|
-
class TodoList < ActiveRecord::Base
|
22
|
-
has_many :todo_items, :order => "position"
|
23
|
-
end
|
24
|
-
|
25
|
-
class TodoItem < ActiveRecord::Base
|
26
|
-
belongs_to :todo_list
|
27
|
-
acts_as_list :scope => :todo_list
|
28
|
-
end
|
29
|
-
|
30
|
-
todo_list.first.move_to_bottom
|
31
|
-
todo_list.last.move_higher
|
32
|
-
|
33
|
-
|
34
|
-
Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license
|
data/acts-as-list.gemspec
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
|
2
|
-
# Gem::Specification for Acts-as-list-0.1.2
|
3
|
-
# Originally generated by Echoe
|
4
|
-
|
5
|
-
Gem::Specification.new do |s|
|
6
|
-
s.name = %q{acts_as_list}
|
7
|
-
s.version = "0.1.2"
|
8
|
-
|
9
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
|
-
s.authors = ["Ryan Bates, Rails Core"]
|
11
|
-
s.date = %q{2008-07-21}
|
12
|
-
s.description = %q{Gem version of acts_as_list Rails plugin.}
|
13
|
-
s.email = %q{ryan (at) railscasts (dot) com}
|
14
|
-
s.extra_rdoc_files = ["lib/acts_as_list.rb", "README", "tasks/deployment.rake"]
|
15
|
-
s.files = ["acts-as-list.gemspec", "lib/acts_as_list.rb", "Manifest", "README", "tasks/deployment.rake", "test/list_test.rb"]
|
16
|
-
s.has_rdoc = true
|
17
|
-
s.homepage = %q{http://github.com/ryanb/acts-as-list}
|
18
|
-
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Acts-as-list", "--main", "README"]
|
19
|
-
s.require_paths = ["lib"]
|
20
|
-
s.rubyforge_project = %q{acts-as-list}
|
21
|
-
s.rubygems_version = %q{1.2.0}
|
22
|
-
s.summary = %q{Gem version of acts_as_list Rails plugin.}
|
23
|
-
s.test_files = ["test/list_test.rb"]
|
24
|
-
|
25
|
-
if s.respond_to? :specification_version then
|
26
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
27
|
-
s.specification_version = 2
|
28
|
-
|
29
|
-
if current_version >= 3 then
|
30
|
-
else
|
31
|
-
end
|
32
|
-
else
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
|
37
|
-
# # Original Rakefile source (requires the Echoe gem):
|
38
|
-
#
|
39
|
-
# require 'rubygems'
|
40
|
-
# require 'rake'
|
41
|
-
#
|
42
|
-
# begin
|
43
|
-
# require 'echoe'
|
44
|
-
#
|
45
|
-
# Echoe.new('acts-as-list', '0.1.2') do |p|
|
46
|
-
# p.summary = "Gem version of acts_as_list Rails plugin."
|
47
|
-
# p.description = "Gem version of acts_as_list Rails plugin."
|
48
|
-
# p.url = "http://github.com/ryanb/acts-as-list"
|
49
|
-
# p.author = ['Ryan Bates', 'Rails Core']
|
50
|
-
# p.email = "ryan (at) railscasts (dot) com"
|
51
|
-
# end
|
52
|
-
#
|
53
|
-
# rescue LoadError => boom
|
54
|
-
# puts "You are missing a dependency required for meta-operations on this gem."
|
55
|
-
# puts "#{boom.to_s.capitalize}."
|
56
|
-
# end
|
57
|
-
#
|
58
|
-
# Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/tasks/deployment.rake
DELETED
data/test/list_test.rb
DELETED
@@ -1,369 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
gem 'activerecord', '>= 1.15.4.7794'
|
5
|
-
require 'active_record'
|
6
|
-
|
7
|
-
require "#{File.dirname(__FILE__)}/../lib/acts_as_list"
|
8
|
-
|
9
|
-
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
10
|
-
|
11
|
-
def setup_db
|
12
|
-
ActiveRecord::Schema.define(:version => 1) do
|
13
|
-
create_table :mixins do |t|
|
14
|
-
t.column :pos, :integer
|
15
|
-
t.column :parent_id, :integer
|
16
|
-
t.column :created_at, :datetime
|
17
|
-
t.column :updated_at, :datetime
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def teardown_db
|
23
|
-
ActiveRecord::Base.connection.tables.each do |table|
|
24
|
-
ActiveRecord::Base.connection.drop_table(table)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
class Mixin < ActiveRecord::Base
|
29
|
-
end
|
30
|
-
|
31
|
-
class ListMixin < Mixin
|
32
|
-
acts_as_list :column => "pos", :scope => :parent
|
33
|
-
|
34
|
-
def self.table_name() "mixins" end
|
35
|
-
end
|
36
|
-
|
37
|
-
class ListMixinSub1 < ListMixin
|
38
|
-
end
|
39
|
-
|
40
|
-
class ListMixinSub2 < ListMixin
|
41
|
-
end
|
42
|
-
|
43
|
-
class ListWithStringScopeMixin < ActiveRecord::Base
|
44
|
-
acts_as_list :column => "pos", :scope => 'parent_id = #{parent_id}'
|
45
|
-
|
46
|
-
def self.table_name() "mixins" end
|
47
|
-
end
|
48
|
-
|
49
|
-
|
50
|
-
class ListTest < Test::Unit::TestCase
|
51
|
-
|
52
|
-
def setup
|
53
|
-
setup_db
|
54
|
-
(1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 5 }
|
55
|
-
end
|
56
|
-
|
57
|
-
def teardown
|
58
|
-
teardown_db
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_reordering
|
62
|
-
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
63
|
-
|
64
|
-
ListMixin.find(2).move_lower
|
65
|
-
assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
66
|
-
|
67
|
-
ListMixin.find(2).move_higher
|
68
|
-
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
69
|
-
|
70
|
-
ListMixin.find(1).move_to_bottom
|
71
|
-
assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
72
|
-
|
73
|
-
ListMixin.find(1).move_to_top
|
74
|
-
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
75
|
-
|
76
|
-
ListMixin.find(2).move_to_bottom
|
77
|
-
assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
78
|
-
|
79
|
-
ListMixin.find(4).move_to_top
|
80
|
-
assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_move_to_bottom_with_next_to_last_item
|
84
|
-
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
85
|
-
ListMixin.find(3).move_to_bottom
|
86
|
-
assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_next_prev
|
90
|
-
assert_equal ListMixin.find(2), ListMixin.find(1).lower_item
|
91
|
-
assert_nil ListMixin.find(1).higher_item
|
92
|
-
assert_equal ListMixin.find(3), ListMixin.find(4).higher_item
|
93
|
-
assert_nil ListMixin.find(4).lower_item
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_injection
|
97
|
-
item = ListMixin.new(:parent_id => 1)
|
98
|
-
assert_equal "parent_id = 1", item.scope_condition
|
99
|
-
assert_equal "pos", item.position_column
|
100
|
-
end
|
101
|
-
|
102
|
-
def test_insert
|
103
|
-
new = ListMixin.create(:parent_id => 20)
|
104
|
-
assert_equal 1, new.pos
|
105
|
-
assert new.first?
|
106
|
-
assert new.last?
|
107
|
-
|
108
|
-
new = ListMixin.create(:parent_id => 20)
|
109
|
-
assert_equal 2, new.pos
|
110
|
-
assert !new.first?
|
111
|
-
assert new.last?
|
112
|
-
|
113
|
-
new = ListMixin.create(:parent_id => 20)
|
114
|
-
assert_equal 3, new.pos
|
115
|
-
assert !new.first?
|
116
|
-
assert new.last?
|
117
|
-
|
118
|
-
new = ListMixin.create(:parent_id => 0)
|
119
|
-
assert_equal 1, new.pos
|
120
|
-
assert new.first?
|
121
|
-
assert new.last?
|
122
|
-
end
|
123
|
-
|
124
|
-
def test_insert_at
|
125
|
-
new = ListMixin.create(:parent_id => 20)
|
126
|
-
assert_equal 1, new.pos
|
127
|
-
|
128
|
-
new = ListMixin.create(:parent_id => 20)
|
129
|
-
assert_equal 2, new.pos
|
130
|
-
|
131
|
-
new = ListMixin.create(:parent_id => 20)
|
132
|
-
assert_equal 3, new.pos
|
133
|
-
|
134
|
-
new4 = ListMixin.create(:parent_id => 20)
|
135
|
-
assert_equal 4, new4.pos
|
136
|
-
|
137
|
-
new4.insert_at(3)
|
138
|
-
assert_equal 3, new4.pos
|
139
|
-
|
140
|
-
new.reload
|
141
|
-
assert_equal 4, new.pos
|
142
|
-
|
143
|
-
new.insert_at(2)
|
144
|
-
assert_equal 2, new.pos
|
145
|
-
|
146
|
-
new4.reload
|
147
|
-
assert_equal 4, new4.pos
|
148
|
-
|
149
|
-
new5 = ListMixin.create(:parent_id => 20)
|
150
|
-
assert_equal 5, new5.pos
|
151
|
-
|
152
|
-
new5.insert_at(1)
|
153
|
-
assert_equal 1, new5.pos
|
154
|
-
|
155
|
-
new4.reload
|
156
|
-
assert_equal 5, new4.pos
|
157
|
-
end
|
158
|
-
|
159
|
-
def test_delete_middle
|
160
|
-
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
161
|
-
|
162
|
-
ListMixin.find(2).destroy
|
163
|
-
|
164
|
-
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
165
|
-
|
166
|
-
assert_equal 1, ListMixin.find(1).pos
|
167
|
-
assert_equal 2, ListMixin.find(3).pos
|
168
|
-
assert_equal 3, ListMixin.find(4).pos
|
169
|
-
|
170
|
-
ListMixin.find(1).destroy
|
171
|
-
|
172
|
-
assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
173
|
-
|
174
|
-
assert_equal 1, ListMixin.find(3).pos
|
175
|
-
assert_equal 2, ListMixin.find(4).pos
|
176
|
-
end
|
177
|
-
|
178
|
-
def test_with_string_based_scope
|
179
|
-
new = ListWithStringScopeMixin.create(:parent_id => 500)
|
180
|
-
assert_equal 1, new.pos
|
181
|
-
assert new.first?
|
182
|
-
assert new.last?
|
183
|
-
end
|
184
|
-
|
185
|
-
def test_nil_scope
|
186
|
-
new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create
|
187
|
-
new2.move_higher
|
188
|
-
assert_equal [new2, new1, new3], ListMixin.find(:all, :conditions => 'parent_id IS NULL', :order => 'pos')
|
189
|
-
end
|
190
|
-
|
191
|
-
|
192
|
-
def test_remove_from_list_should_then_fail_in_list?
|
193
|
-
assert_equal true, ListMixin.find(1).in_list?
|
194
|
-
ListMixin.find(1).remove_from_list
|
195
|
-
assert_equal false, ListMixin.find(1).in_list?
|
196
|
-
end
|
197
|
-
|
198
|
-
def test_remove_from_list_should_set_position_to_nil
|
199
|
-
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
200
|
-
|
201
|
-
ListMixin.find(2).remove_from_list
|
202
|
-
|
203
|
-
assert_equal [2, 1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
204
|
-
|
205
|
-
assert_equal 1, ListMixin.find(1).pos
|
206
|
-
assert_equal nil, ListMixin.find(2).pos
|
207
|
-
assert_equal 2, ListMixin.find(3).pos
|
208
|
-
assert_equal 3, ListMixin.find(4).pos
|
209
|
-
end
|
210
|
-
|
211
|
-
def test_remove_before_destroy_does_not_shift_lower_items_twice
|
212
|
-
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
213
|
-
|
214
|
-
ListMixin.find(2).remove_from_list
|
215
|
-
ListMixin.find(2).destroy
|
216
|
-
|
217
|
-
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
218
|
-
|
219
|
-
assert_equal 1, ListMixin.find(1).pos
|
220
|
-
assert_equal 2, ListMixin.find(3).pos
|
221
|
-
assert_equal 3, ListMixin.find(4).pos
|
222
|
-
end
|
223
|
-
|
224
|
-
# special thanks to openhood on github
|
225
|
-
def test_delete_middle_with_holes
|
226
|
-
# first we check everything is at expected place
|
227
|
-
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
228
|
-
|
229
|
-
# then we create a hole in the list, say you're working with existing data in which you already have holes
|
230
|
-
# or your scope is very complex
|
231
|
-
ListMixin.delete(2)
|
232
|
-
|
233
|
-
# we ensure the hole is really here
|
234
|
-
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
235
|
-
assert_equal 1, ListMixin.find(1).pos
|
236
|
-
assert_equal 3, ListMixin.find(3).pos
|
237
|
-
assert_equal 4, ListMixin.find(4).pos
|
238
|
-
|
239
|
-
# can we retrieve lower item despite the hole?
|
240
|
-
assert_equal 3, ListMixin.find(1).lower_item.id
|
241
|
-
|
242
|
-
# can we move an item lower jumping more than one position?
|
243
|
-
ListMixin.find(1).move_lower
|
244
|
-
assert_equal [3, 1, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
245
|
-
assert_equal 2, ListMixin.find(3).pos
|
246
|
-
assert_equal 3, ListMixin.find(1).pos
|
247
|
-
assert_equal 4, ListMixin.find(4).pos
|
248
|
-
|
249
|
-
# create another hole
|
250
|
-
ListMixin.delete(1)
|
251
|
-
|
252
|
-
# can we retrieve higher item despite the hole?
|
253
|
-
assert_equal 3, ListMixin.find(4).higher_item.id
|
254
|
-
|
255
|
-
# can we move an item higher jumping more than one position?
|
256
|
-
ListMixin.find(4).move_higher
|
257
|
-
assert_equal [4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
258
|
-
assert_equal 2, ListMixin.find(4).pos
|
259
|
-
assert_equal 3, ListMixin.find(3).pos
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
class ListSubTest < Test::Unit::TestCase
|
264
|
-
|
265
|
-
def setup
|
266
|
-
setup_db
|
267
|
-
(1..4).each { |i| ((i % 2 == 1) ? ListMixinSub1 : ListMixinSub2).create! :pos => i, :parent_id => 5000 }
|
268
|
-
end
|
269
|
-
|
270
|
-
def teardown
|
271
|
-
teardown_db
|
272
|
-
end
|
273
|
-
|
274
|
-
def test_reordering
|
275
|
-
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
276
|
-
|
277
|
-
ListMixin.find(2).move_lower
|
278
|
-
assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
279
|
-
|
280
|
-
ListMixin.find(2).move_higher
|
281
|
-
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
282
|
-
|
283
|
-
ListMixin.find(1).move_to_bottom
|
284
|
-
assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
285
|
-
|
286
|
-
ListMixin.find(1).move_to_top
|
287
|
-
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
288
|
-
|
289
|
-
ListMixin.find(2).move_to_bottom
|
290
|
-
assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
291
|
-
|
292
|
-
ListMixin.find(4).move_to_top
|
293
|
-
assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
294
|
-
end
|
295
|
-
|
296
|
-
def test_move_to_bottom_with_next_to_last_item
|
297
|
-
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
298
|
-
ListMixin.find(3).move_to_bottom
|
299
|
-
assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
300
|
-
end
|
301
|
-
|
302
|
-
def test_next_prev
|
303
|
-
assert_equal ListMixin.find(2), ListMixin.find(1).lower_item
|
304
|
-
assert_nil ListMixin.find(1).higher_item
|
305
|
-
assert_equal ListMixin.find(3), ListMixin.find(4).higher_item
|
306
|
-
assert_nil ListMixin.find(4).lower_item
|
307
|
-
end
|
308
|
-
|
309
|
-
def test_injection
|
310
|
-
item = ListMixin.new("parent_id"=>1)
|
311
|
-
assert_equal "parent_id = 1", item.scope_condition
|
312
|
-
assert_equal "pos", item.position_column
|
313
|
-
end
|
314
|
-
|
315
|
-
def test_insert_at
|
316
|
-
new = ListMixin.create("parent_id" => 20)
|
317
|
-
assert_equal 1, new.pos
|
318
|
-
|
319
|
-
new = ListMixinSub1.create("parent_id" => 20)
|
320
|
-
assert_equal 2, new.pos
|
321
|
-
|
322
|
-
new = ListMixinSub2.create("parent_id" => 20)
|
323
|
-
assert_equal 3, new.pos
|
324
|
-
|
325
|
-
new4 = ListMixin.create("parent_id" => 20)
|
326
|
-
assert_equal 4, new4.pos
|
327
|
-
|
328
|
-
new4.insert_at(3)
|
329
|
-
assert_equal 3, new4.pos
|
330
|
-
|
331
|
-
new.reload
|
332
|
-
assert_equal 4, new.pos
|
333
|
-
|
334
|
-
new.insert_at(2)
|
335
|
-
assert_equal 2, new.pos
|
336
|
-
|
337
|
-
new4.reload
|
338
|
-
assert_equal 4, new4.pos
|
339
|
-
|
340
|
-
new5 = ListMixinSub1.create("parent_id" => 20)
|
341
|
-
assert_equal 5, new5.pos
|
342
|
-
|
343
|
-
new5.insert_at(1)
|
344
|
-
assert_equal 1, new5.pos
|
345
|
-
|
346
|
-
new4.reload
|
347
|
-
assert_equal 5, new4.pos
|
348
|
-
end
|
349
|
-
|
350
|
-
def test_delete_middle
|
351
|
-
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
352
|
-
|
353
|
-
ListMixin.find(2).destroy
|
354
|
-
|
355
|
-
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
356
|
-
|
357
|
-
assert_equal 1, ListMixin.find(1).pos
|
358
|
-
assert_equal 2, ListMixin.find(3).pos
|
359
|
-
assert_equal 3, ListMixin.find(4).pos
|
360
|
-
|
361
|
-
ListMixin.find(1).destroy
|
362
|
-
|
363
|
-
assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
364
|
-
|
365
|
-
assert_equal 1, ListMixin.find(3).pos
|
366
|
-
assert_equal 2, ListMixin.find(4).pos
|
367
|
-
end
|
368
|
-
|
369
|
-
end
|