discourse/spec/components/concern/positionable_spec.rb

52 lines
1.3 KiB
Ruby
Raw Normal View History

2013-10-18 03:09:30 -04:00
require "spec_helper"
2014-04-19 00:00:40 -04:00
describe Positionable do
2013-10-18 03:09:30 -04:00
def positions
TestItem.order('position asc, id asc').pluck(:id)
end
context "move_to" do
before do
class TestItem < ActiveRecord::Base
2014-04-19 00:00:40 -04:00
include Positionable
2013-10-18 03:09:30 -04:00
end
Topic.exec_sql("create temporary table test_items(id int primary key, position int)")
end
after do
Topic.exec_sql("drop table test_items")
# import is making my life hard, we need to nuke this out of orbit
des = ActiveSupport::DescendantsTracker.class_variable_get :@@direct_descendants
des[ActiveRecord::Base].delete(TestItem)
end
it "can position stuff correctly" do
5.times do |i|
Topic.exec_sql("insert into test_items(id,position) values(#{i}, #{i})")
end
positions.should == [0,1,2,3,4]
TestItem.find(3).move_to(0)
positions.should == [3,0,1,2,4]
TestItem.pluck(:position).sort.should == [0,1,2,3,4]
2013-10-21 01:14:09 -04:00
TestItem.find(3).move_to(1)
positions.should == [0,3,1,2,4]
2013-10-18 03:09:30 -04:00
# this is somewhat odd, but when there is no such position, not much we can do
2013-10-18 03:09:30 -04:00
TestItem.find(1).move_to(5)
2013-10-21 01:14:09 -04:00
positions.should == [0,3,2,4,1]
2013-10-18 03:09:30 -04:00
TestItem.pluck(:position).sort.should == [0,1,2,3,4]
item = TestItem.new
item.id = 7
item.save
item.position.should == 5
2013-10-18 03:09:30 -04:00
end
end
end