mirror of
https://github.com/discourse/discourse.git
synced 2025-05-30 07:11:34 +08:00
Allow categories with null position, which means sort them based on activity. Mix absolutely positioned (position is not null) categories with null position categories.
This commit is contained in:
@ -4,11 +4,11 @@ require 'category_list'
|
||||
describe CategoryList do
|
||||
|
||||
let(:user) { Fabricate(:user) }
|
||||
let(:admin) { Fabricate(:admin) }
|
||||
let(:category_list) { CategoryList.new(Guardian.new user) }
|
||||
|
||||
context "security" do
|
||||
it "properly hide secure categories" do
|
||||
admin = Fabricate(:admin)
|
||||
user = Fabricate(:user)
|
||||
|
||||
cat = Fabricate(:category)
|
||||
@ -73,4 +73,35 @@ describe CategoryList do
|
||||
|
||||
end
|
||||
|
||||
describe 'category order' do
|
||||
let(:category_ids) { CategoryList.new(Guardian.new(admin)).categories.map(&:id) - [SiteSetting.uncategorized_category_id] }
|
||||
|
||||
before do
|
||||
uncategorized = Category.find(SiteSetting.uncategorized_category_id)
|
||||
uncategorized.position = 100
|
||||
uncategorized.save
|
||||
end
|
||||
|
||||
it "returns topics in specified order" do
|
||||
cat1, cat2 = Fabricate(:category, position: 1), Fabricate(:category, position: 0)
|
||||
category_ids.should == [cat2.id, cat1.id]
|
||||
end
|
||||
|
||||
it "returns default order categories" do
|
||||
cat1, cat2 = Fabricate(:category, position: nil), Fabricate(:category, position: nil)
|
||||
category_ids.should include(cat1.id)
|
||||
category_ids.should include(cat2.id)
|
||||
end
|
||||
|
||||
it "mixes default order categories with absolute position categories" do
|
||||
cat1, cat2, cat3 = Fabricate(:category, position: 0), Fabricate(:category, position: 2), Fabricate(:category, position: nil)
|
||||
category_ids.should == [cat1.id, cat3.id, cat2.id]
|
||||
end
|
||||
|
||||
it "handles duplicate position values" do
|
||||
cat1, cat2, cat3, cat4 = Fabricate(:category, position: 0), Fabricate(:category, position: 0), Fabricate(:category, position: nil), Fabricate(:category, position: 0)
|
||||
category_ids.should == [cat1.id, cat2.id, cat4.id, cat3.id]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -37,8 +37,7 @@ describe Concern::Positionable do
|
||||
TestItem.find(3).move_to(1)
|
||||
positions.should == [0,3,1,2,4]
|
||||
|
||||
# this is somewhat odd, but when there is not positioning
|
||||
# not much we can do
|
||||
# this is somewhat odd, but when there is no such position, not much we can do
|
||||
TestItem.find(1).move_to(5)
|
||||
positions.should == [0,3,2,4,1]
|
||||
|
||||
@ -47,7 +46,34 @@ describe Concern::Positionable do
|
||||
item = TestItem.new
|
||||
item.id = 7
|
||||
item.save
|
||||
item.position.should == 5
|
||||
item.position.should be_nil
|
||||
end
|
||||
|
||||
it "can set records to have null position" do
|
||||
5.times do |i|
|
||||
Topic.exec_sql("insert into test_items(id,position) values(#{i}, #{i})")
|
||||
end
|
||||
|
||||
TestItem.find(2).use_default_position
|
||||
TestItem.find(2).position.should be_nil
|
||||
|
||||
TestItem.find(1).move_to(4)
|
||||
TestItem.order('id ASC').pluck(:position).should == [0,4,nil,2,3]
|
||||
end
|
||||
|
||||
it "can maintain null positions when moving things around" do
|
||||
5.times do |i|
|
||||
Topic.exec_sql("insert into test_items(id,position) values(#{i}, null)")
|
||||
end
|
||||
|
||||
TestItem.find(2).move_to(0)
|
||||
TestItem.order('id asc').pluck(:position).should == [nil,nil,0,nil,nil]
|
||||
TestItem.find(0).move_to(4)
|
||||
TestItem.order('id asc').pluck(:position).should == [4,nil,0,nil,nil]
|
||||
TestItem.find(2).move_to(1)
|
||||
TestItem.order('id asc').pluck(:position).should == [4,nil,1,nil,nil]
|
||||
TestItem.find(0).move_to(1)
|
||||
TestItem.order('id asc').pluck(:position).should == [1,nil,2,nil,nil]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user