From ea307931a747c0ff20ccccb3b1049c0bbe239d54 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 17 Dec 2013 14:36:07 +1100 Subject: [PATCH] fix spec, categories without position are now always at the end of the list --- app/models/category_list.rb | 36 +++++++-------------------- spec/components/category_list_spec.rb | 9 +++---- 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/app/models/category_list.rb b/app/models/category_list.rb index df862a4cef1..e9d951d576f 100644 --- a/app/models/category_list.rb +++ b/app/models/category_list.rb @@ -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| diff --git a/spec/components/category_list_spec.rb b/spec/components/category_list_spec.rb index cc9e7163530..a6dc448f022 100644 --- a/spec/components/category_list_spec.rb +++ b/spec/components/category_list_spec.rb @@ -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