From ed9c21e42c3e34955013fe34ba8443841bafbedd Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Thu, 25 Oct 2018 20:34:39 -0400 Subject: [PATCH] FEATURE: hide muted categories from /categories list (#6531) --- app/models/category_list.rb | 5 +++++ spec/models/category_list_spec.rb | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/app/models/category_list.rb b/app/models/category_list.rb index e8bf8aefab8..f59f36c3cde 100644 --- a/app/models/category_list.rb +++ b/app/models/category_list.rb @@ -20,6 +20,7 @@ class CategoryList find_categories prune_empty + prune_muted find_user_data sort_unpinned trim_results @@ -136,6 +137,10 @@ class CategoryList @categories.delete_if { |c| c.uncategorized? && c.displayable_topics.blank? } end + def prune_muted + @categories.delete_if { |c| c.notification_level == CategoryUser.notification_levels[:muted] } + end + # Attach some data for serialization to each topic def find_user_data if @guardian.current_user && @all_topics.present? diff --git a/spec/models/category_list_spec.rb b/spec/models/category_list_spec.rb index ac7a46542ad..f9d7ff79d16 100644 --- a/spec/models/category_list_spec.rb +++ b/spec/models/category_list_spec.rb @@ -47,6 +47,16 @@ describe CategoryList do expect(CategoryList.new(Guardian.new(nil), include_topics: true).categories.find { |x| x.name == private_cat.name }).to eq(nil) end + it "properly hide muted categories" do + cat_muted = Fabricate(:category) + CategoryUser.create!(user_id: user.id, + category_id: cat_muted.id, + notification_level: CategoryUser.notification_levels[:muted]) + + # uncategorized + cat_muted for admin + expect(CategoryList.new(Guardian.new admin).categories.count).to eq(2) + expect(CategoryList.new(Guardian.new user).categories.count).to eq(1) + end end context "with a category" do