fix spec, categories without position are now always at the end of the list

This commit is contained in:
Sam 2013-12-17 14:36:07 +11:00
parent 29ba471db6
commit ea307931a7
2 changed files with 13 additions and 32 deletions

View File

@ -53,37 +53,19 @@ class CategoryList
# Find a list of all categories to associate the topics with
def find_categories
@absolute_position_categories = Category
.includes(:featured_users)
.secured(@guardian)
.where('position IS NOT NULL')
.order('position ASC')
@default_position_categories = Category
.includes(:featured_users)
.secured(@guardian)
.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')
@categories = Category
.includes(:featured_users)
.secured(@guardian)
.order('position asc')
.order('COALESCE(categories.posts_week, 0) DESC')
.order('COALESCE(categories.posts_month, 0) DESC')
.order('COALESCE(categories.posts_year, 0) DESC')
.to_a
if latest_post_only?
@absolute_position_categories = @absolute_position_categories.includes(:latest_post => {:topic => :last_poster} )
@default_position_categories = @default_position_categories.includes(:latest_post => {:topic => :last_poster} )
@categories = @categories.includes(:latest_post => {:topic => :last_poster} )
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 = {}
to_delete = Set.new
@categories.each do |c|

View File

@ -12,13 +12,12 @@ describe CategoryList do
user = Fabricate(:user)
cat = Fabricate(:category)
topic = Fabricate(:topic, category: cat)
Fabricate(:topic, category: cat)
cat.set_permissions(:admins => :full)
cat.save
# uncategorized + this
CategoryList.new(Guardian.new admin).categories.count.should == 2
CategoryList.new(Guardian.new user).categories.count.should == 0
CategoryList.new(Guardian.new nil).categories.count.should == 0
end
@ -46,7 +45,7 @@ describe CategoryList do
end
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)
category_list.categories.should have(3).categories
category_list.categories.should include(topic_category)
@ -93,9 +92,9 @@ describe CategoryList do
category_ids.should include(cat2.id)
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)
category_ids.should == [cat1.id, cat3.id, cat2.id]
category_ids.should == [cat1.id, cat2.id, cat3.id]
end
it "handles duplicate position values" do