mirror of
https://github.com/discourse/discourse.git
synced 2025-02-04 14:49:29 +08:00
fix spec, categories without position are now always at the end of the list
This commit is contained in:
parent
29ba471db6
commit
ea307931a7
|
@ -53,37 +53,19 @@ class CategoryList
|
||||||
|
|
||||||
# Find a list of all categories to associate the topics with
|
# Find a list of all categories to associate the topics with
|
||||||
def find_categories
|
def find_categories
|
||||||
@absolute_position_categories = Category
|
@categories = Category
|
||||||
.includes(:featured_users)
|
.includes(:featured_users)
|
||||||
.secured(@guardian)
|
.secured(@guardian)
|
||||||
.where('position IS NOT NULL')
|
.order('position asc')
|
||||||
.order('position ASC')
|
.order('COALESCE(categories.posts_week, 0) DESC')
|
||||||
@default_position_categories = Category
|
.order('COALESCE(categories.posts_month, 0) DESC')
|
||||||
.includes(:featured_users)
|
.order('COALESCE(categories.posts_year, 0) DESC')
|
||||||
.secured(@guardian)
|
.to_a
|
||||||
.where('position IS NULL')
|
|
||||||
.order('COALESCE(categories.posts_week, 0) DESC')
|
|
||||||
.order('COALESCE(categories.posts_month, 0) DESC')
|
|
||||||
.order('COALESCE(categories.posts_year, 0) DESC')
|
|
||||||
|
|
||||||
if latest_post_only?
|
if latest_post_only?
|
||||||
@absolute_position_categories = @absolute_position_categories.includes(:latest_post => {:topic => :last_poster} )
|
@categories = @categories.includes(:latest_post => {:topic => :last_poster} )
|
||||||
@default_position_categories = @default_position_categories.includes(:latest_post => {:topic => :last_poster} )
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@default_position_categories = @default_position_categories.to_a
|
|
||||||
@categories = []
|
|
||||||
index = 0
|
|
||||||
@absolute_position_categories.to_a.each do |c|
|
|
||||||
if c.position > index
|
|
||||||
@categories.push(*(@default_position_categories.shift(c.position - index)))
|
|
||||||
end
|
|
||||||
@categories << c
|
|
||||||
index = c.position + 1 if c.position >= index # handles duplicate position values
|
|
||||||
end
|
|
||||||
@categories.push *@default_position_categories # Whatever is left is put on the end
|
|
||||||
|
|
||||||
|
|
||||||
subcategories = {}
|
subcategories = {}
|
||||||
to_delete = Set.new
|
to_delete = Set.new
|
||||||
@categories.each do |c|
|
@categories.each do |c|
|
||||||
|
|
|
@ -12,13 +12,12 @@ describe CategoryList do
|
||||||
user = Fabricate(:user)
|
user = Fabricate(:user)
|
||||||
|
|
||||||
cat = Fabricate(:category)
|
cat = Fabricate(:category)
|
||||||
topic = Fabricate(:topic, category: cat)
|
Fabricate(:topic, category: cat)
|
||||||
cat.set_permissions(:admins => :full)
|
cat.set_permissions(:admins => :full)
|
||||||
cat.save
|
cat.save
|
||||||
|
|
||||||
# uncategorized + this
|
# uncategorized + this
|
||||||
CategoryList.new(Guardian.new admin).categories.count.should == 2
|
CategoryList.new(Guardian.new admin).categories.count.should == 2
|
||||||
|
|
||||||
CategoryList.new(Guardian.new user).categories.count.should == 0
|
CategoryList.new(Guardian.new user).categories.count.should == 0
|
||||||
CategoryList.new(Guardian.new nil).categories.count.should == 0
|
CategoryList.new(Guardian.new nil).categories.count.should == 0
|
||||||
end
|
end
|
||||||
|
@ -46,7 +45,7 @@ describe CategoryList do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns the empty category and a non-empty category for those who can create them' do
|
it 'returns the empty category and a non-empty category for those who can create them' do
|
||||||
category_with_topics = Fabricate(:topic, category: Fabricate(:category))
|
Fabricate(:topic, category: Fabricate(:category))
|
||||||
Guardian.any_instance.expects(:can_create?).with(Category).returns(true)
|
Guardian.any_instance.expects(:can_create?).with(Category).returns(true)
|
||||||
category_list.categories.should have(3).categories
|
category_list.categories.should have(3).categories
|
||||||
category_list.categories.should include(topic_category)
|
category_list.categories.should include(topic_category)
|
||||||
|
@ -93,9 +92,9 @@ describe CategoryList do
|
||||||
category_ids.should include(cat2.id)
|
category_ids.should include(cat2.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "mixes default order categories with absolute position categories" do
|
it "default always at the end" do
|
||||||
cat1, cat2, cat3 = Fabricate(:category, position: 0), Fabricate(:category, position: 2), Fabricate(:category, position: nil)
|
cat1, cat2, cat3 = Fabricate(:category, position: 0), Fabricate(:category, position: 2), Fabricate(:category, position: nil)
|
||||||
category_ids.should == [cat1.id, cat3.id, cat2.id]
|
category_ids.should == [cat1.id, cat2.id, cat3.id]
|
||||||
end
|
end
|
||||||
|
|
||||||
it "handles duplicate position values" do
|
it "handles duplicate position values" do
|
||||||
|
|
Loading…
Reference in New Issue
Block a user