Admins don't see uncategorized on /categories if it has no topics and allow_uncategorized_topics is false

This commit is contained in:
Neil Lalonde 2014-01-15 14:11:19 -05:00
parent 812f195cae
commit 89908cdb47
4 changed files with 20 additions and 6 deletions

View File

@ -339,7 +339,7 @@ SQL
[read_restricted, mapped] [read_restricted, mapped]
end end
def uncatgorized? def uncategorized?
id == SiteSetting.uncategorized_category_id id == SiteSetting.uncategorized_category_id
end end
end end

View File

@ -113,13 +113,19 @@ class CategoryList
end 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 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 # 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? 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
end end

View File

@ -11,7 +11,7 @@ module CategoryGuardian
end end
def can_delete_category?(category) def can_delete_category?(category)
is_staff? && category.topic_count == 0 && !category.uncatgorized? is_staff? && category.topic_count == 0 && !category.uncategorized?
end end
def can_see_category?(category) def can_see_category?(category)

View File

@ -34,6 +34,7 @@ describe CategoryList do
end end
it "returns empty categories for those who can create them" do 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) Guardian.any_instance.expects(:can_create?).with(Category).returns(true)
category_list.categories.should_not be_blank category_list.categories.should_not be_blank
end end
@ -45,12 +46,19 @@ 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
SiteSetting.stubs(:allow_uncategorized_topics).returns(true)
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)
end 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 end
context "with a topic in a category" do context "with a topic in a category" do