diff --git a/app/models/category_list.rb b/app/models/category_list.rb index f3ae8a1d007..ccc23d88b65 100644 --- a/app/models/category_list.rb +++ b/app/models/category_list.rb @@ -87,13 +87,15 @@ class CategoryList @categories = @categories.to_a category_user = {} + default_notification_level = nil unless @guardian.anonymous? category_user = Hash[*CategoryUser.where(user: @guardian.user).pluck(:category_id, :notification_level).flatten] + default_notification_level = CategoryUser.notification_levels[:regular] end allowed_topic_create = Set.new(Category.topic_create_allowed(@guardian).pluck(:id)) @categories.each do |category| - category.notification_level = category_user[category.id] + category.notification_level = category_user[category.id] || default_notification_level category.permission = CategoryGroup.permission_types[:full] if allowed_topic_create.include?(category.id) category.has_children = category.subcategories.present? end diff --git a/spec/models/category_list_spec.rb b/spec/models/category_list_spec.rb index c3d5efb88f8..ac7a46542ad 100644 --- a/spec/models/category_list_spec.rb +++ b/spec/models/category_list_spec.rb @@ -81,6 +81,28 @@ describe CategoryList do end end + context "notification level" do + it "returns 'regular' as default notification level" do + category = category_list.categories.find { |c| c.id == topic_category.id } + expect(category.notification_level).to eq(NotificationLevels.all[:regular]) + end + + it "returns the users notication level" do + CategoryUser.set_notification_level_for_category(user, NotificationLevels.all[:watching], topic_category.id) + category_list = CategoryList.new(Guardian.new(user)) + category = category_list.categories.find { |c| c.id == topic_category.id } + + expect(category.notification_level).to eq(NotificationLevels.all[:watching]) + end + + it "returns no notication level for anonymous users" do + category_list = CategoryList.new(Guardian.new(nil)) + category = category_list.categories.find { |c| c.id == topic_category.id } + + expect(category.notification_level).to be_nil + end + end + end describe 'category order' do