From 89908cdb47787e3c021e02664ff4a47ec4332326 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 15 Jan 2014 14:11:19 -0500 Subject: [PATCH] Admins don't see uncategorized on /categories if it has no topics and allow_uncategorized_topics is false --- app/models/category.rb | 2 +- app/models/category_list.rb | 14 ++++++++++---- lib/guardian/category_guardian.rb | 2 +- spec/components/category_list_spec.rb | 8 ++++++++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index 33573b0c0be..88f4f324abe 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -339,7 +339,7 @@ SQL [read_restricted, mapped] end - def uncatgorized? + def uncategorized? id == SiteSetting.uncategorized_category_id end end diff --git a/app/models/category_list.rb b/app/models/category_list.rb index e9d951d576f..5e1033a8055 100644 --- a/app/models/category_list.rb +++ b/app/models/category_list.rb @@ -113,13 +113,19 @@ class CategoryList end - # Remove any empty topics unless we can create them (so we can see the controls) + # Remove any empty categories unless we can create them (so we can see the controls) def prune_empty - unless @guardian.can_create?(Category) + if !@guardian.can_create?(Category) # Remove categories with no featured topics unless we have the ability to edit one - @categories.delete_if { |c| + @categories.delete_if do |c| c.displayable_topics.blank? && c.description.blank? - } + end + elsif !SiteSetting.allow_uncategorized_topics + # Don't show uncategorized to admins either, if uncategorized topics are not allowed + # and there are none. + @categories.delete_if do |c| + c.uncategorized? && c.displayable_topics.blank? + end end end diff --git a/lib/guardian/category_guardian.rb b/lib/guardian/category_guardian.rb index 7c91bbb9a62..610484c01ed 100644 --- a/lib/guardian/category_guardian.rb +++ b/lib/guardian/category_guardian.rb @@ -11,7 +11,7 @@ module CategoryGuardian end def can_delete_category?(category) - is_staff? && category.topic_count == 0 && !category.uncatgorized? + is_staff? && category.topic_count == 0 && !category.uncategorized? end def can_see_category?(category) diff --git a/spec/components/category_list_spec.rb b/spec/components/category_list_spec.rb index 2c41deab79c..02f7030a605 100644 --- a/spec/components/category_list_spec.rb +++ b/spec/components/category_list_spec.rb @@ -34,6 +34,7 @@ describe CategoryList do end it "returns empty categories for those who can create them" do + SiteSetting.stubs(:allow_uncategorized_topics).returns(true) Guardian.any_instance.expects(:can_create?).with(Category).returns(true) category_list.categories.should_not be_blank end @@ -45,12 +46,19 @@ describe CategoryList do end it 'returns the empty category and a non-empty category for those who can create them' do + SiteSetting.stubs(:allow_uncategorized_topics).returns(true) 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) end + it "doesn't return empty uncategorized category to admins if allow_uncategorized_topics is false" do + SiteSetting.stubs(:allow_uncategorized_topics).returns(false) + CategoryList.new(Guardian.new(user)).categories.should be_empty + CategoryList.new(Guardian.new(admin)).categories.map(&:id).should_not include(SiteSetting.uncategorized_category_id) + end + end context "with a topic in a category" do