Skip to content

Commit

Permalink
Admins don't see uncategorized on /categories if it has no topics and…
Browse files Browse the repository at this point in the history
… allow_uncategorized_topics is false
  • Loading branch information
nlalonde committed Jan 15, 2014
1 parent 812f195 commit 89908cd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def self.resolve_permissions(permissions)
[read_restricted, mapped]
end

def uncatgorized?
def uncategorized?
id == SiteSetting.uncategorized_category_id
end
end
Expand Down
14 changes: 10 additions & 4 deletions app/models/category_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,19 @@ def find_categories
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

Expand Down
2 changes: 1 addition & 1 deletion lib/guardian/category_guardian.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def can_edit_category?(category)
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)
Expand Down
8 changes: 8 additions & 0 deletions spec/components/category_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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
Expand All @@ -45,12 +46,19 @@
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
Expand Down

0 comments on commit 89908cd

Please sign in to comment.