4 require 'active_record'
6 $:.unshift File.dirname(__FILE__) + '/../lib'
7 require File.dirname(__FILE__) + '/../init'
9 ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
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
23 ActiveRecord::Base.connection.tables.each do |table|
24 ActiveRecord::Base.connection.drop_table(table)
28 class Mixin < ActiveRecord::Base
31 class ListMixin < Mixin
32 acts_as_list :column => "pos", :scope => :parent
34 def self.table_name() "mixins" end
37 class ListMixinSub1 < ListMixin
40 class ListMixinSub2 < ListMixin
43 class ListWithStringScopeMixin < ActiveRecord::Base
44 acts_as_list :column => "pos", :scope => 'parent_id = #{parent_id}'
46 def self.table_name() "mixins" end
50 class ListTest < Test::Unit::TestCase
54 (1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 5 }
62 assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
64 ListMixin.find(2).move_lower
65 assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
67 ListMixin.find(2).move_higher
68 assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
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)
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)
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)
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)
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)
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
97 item = ListMixin.new(:parent_id => 1)
98 assert_equal "parent_id = 1", item.scope_condition
99 assert_equal "pos", item.position_column
103 new = ListMixin.create(:parent_id => 20)
104 assert_equal 1, new.pos
108 new = ListMixin.create(:parent_id => 20)
109 assert_equal 2, new.pos
113 new = ListMixin.create(:parent_id => 20)
114 assert_equal 3, new.pos
118 new = ListMixin.create(:parent_id => 0)
119 assert_equal 1, new.pos
125 new = ListMixin.create(:parent_id => 20)
126 assert_equal 1, new.pos
128 new = ListMixin.create(:parent_id => 20)
129 assert_equal 2, new.pos
131 new = ListMixin.create(:parent_id => 20)
132 assert_equal 3, new.pos
134 new4 = ListMixin.create(:parent_id => 20)
135 assert_equal 4, new4.pos
138 assert_equal 3, new4.pos
141 assert_equal 4, new.pos
144 assert_equal 2, new.pos
147 assert_equal 4, new4.pos
149 new5 = ListMixin.create(:parent_id => 20)
150 assert_equal 5, new5.pos
153 assert_equal 1, new5.pos
156 assert_equal 5, new4.pos
159 def test_delete_middle
160 assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
162 ListMixin.find(2).destroy
164 assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
166 assert_equal 1, ListMixin.find(1).pos
167 assert_equal 2, ListMixin.find(3).pos
168 assert_equal 3, ListMixin.find(4).pos
170 ListMixin.find(1).destroy
172 assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
174 assert_equal 1, ListMixin.find(3).pos
175 assert_equal 2, ListMixin.find(4).pos
178 def test_with_string_based_scope
179 new = ListWithStringScopeMixin.create(:parent_id => 500)
180 assert_equal 1, new.pos
186 new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create
188 assert_equal [new2, new1, new3], ListMixin.find(:all, :conditions => 'parent_id IS NULL', :order => 'pos')
193 class ListSubTest < Test::Unit::TestCase
197 (1..4).each { |i| ((i % 2 == 1) ? ListMixinSub1 : ListMixinSub2).create! :pos => i, :parent_id => 5000 }
205 assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
207 ListMixin.find(2).move_lower
208 assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
210 ListMixin.find(2).move_higher
211 assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
213 ListMixin.find(1).move_to_bottom
214 assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
216 ListMixin.find(1).move_to_top
217 assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
219 ListMixin.find(2).move_to_bottom
220 assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
222 ListMixin.find(4).move_to_top
223 assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
226 def test_move_to_bottom_with_next_to_last_item
227 assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
228 ListMixin.find(3).move_to_bottom
229 assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
233 assert_equal ListMixin.find(2), ListMixin.find(1).lower_item
234 assert_nil ListMixin.find(1).higher_item
235 assert_equal ListMixin.find(3), ListMixin.find(4).higher_item
236 assert_nil ListMixin.find(4).lower_item
240 item = ListMixin.new("parent_id"=>1)
241 assert_equal "parent_id = 1", item.scope_condition
242 assert_equal "pos", item.position_column
246 new = ListMixin.create("parent_id" => 20)
247 assert_equal 1, new.pos
249 new = ListMixinSub1.create("parent_id" => 20)
250 assert_equal 2, new.pos
252 new = ListMixinSub2.create("parent_id" => 20)
253 assert_equal 3, new.pos
255 new4 = ListMixin.create("parent_id" => 20)
256 assert_equal 4, new4.pos
259 assert_equal 3, new4.pos
262 assert_equal 4, new.pos
265 assert_equal 2, new.pos
268 assert_equal 4, new4.pos
270 new5 = ListMixinSub1.create("parent_id" => 20)
271 assert_equal 5, new5.pos
274 assert_equal 1, new5.pos
277 assert_equal 5, new4.pos
280 def test_delete_middle
281 assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
283 ListMixin.find(2).destroy
285 assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
287 assert_equal 1, ListMixin.find(1).pos
288 assert_equal 2, ListMixin.find(3).pos
289 assert_equal 3, ListMixin.find(4).pos
291 ListMixin.find(1).destroy
293 assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
295 assert_equal 1, ListMixin.find(3).pos
296 assert_equal 2, ListMixin.find(4).pos