A platform for community discussion. Free, open, simple.
FIX: Ensure suppressed categories do not produce any featured topics. (#7863)
Bianca Nenciu8e133de83118
diff --git a/app/models/category.rb b/app/models/category.rbindex 9ba9d914b5c..6009164df89 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -72,6 +72,7 @@ class Category < ActiveRecord::Baseafter_save :clear_url_cacheafter_save :index_searchafter_save :update_reviewables+ after_save :clear_featured_cacheafter_destroy :reset_topic_ids_cacheafter_destroy :publish_category_deletion@@ -568,6 +569,10 @@ class Category < ActiveRecord::Base@@url_cache.clearend+ def clear_featured_cache+ CategoryFeaturedTopic.clear_exclude_category_ids+ end+def full_slug(separator = "-")start_idx = "#{Discourse.base_uri}/c/".lengthurl[start_idx..-1].gsub("/", separator)diff --git a/app/models/category_featured_topic.rb b/app/models/category_featured_topic.rbindex 9ffb283b18f..fef24c07516 100644--- a/app/models/category_featured_topic.rb+++ b/app/models/category_featured_topic.rb@@ -38,6 +38,16 @@ class CategoryFeaturedTopic < ActiveRecord::Baseendend+ @@exclude_category_ids = DistributedCache.new('excluded_category_ids_from_featured')++ def self.cached_exclude_category_ids+ @@exclude_category_ids['ids'] ||= Category.where(suppress_from_latest: true).pluck(:id)+ end++ def self.clear_exclude_category_ids+ @@exclude_category_ids.clear+ end+def self.clear_batch!$redis.del(NEXT_CATEGORY_ID_KEY)end@@ -49,7 +59,8 @@ class CategoryFeaturedTopic < ActiveRecord::Baseper_page: c.num_featured_topics,except_topic_ids: [c.topic_id],visible: true,- no_definitions: true+ no_definitions: true,+ exclude_category_ids: CategoryFeaturedTopic.cached_exclude_category_ids}# It may seem a bit odd that we are running 2 queries here, when admindiff --git a/spec/models/category_featured_topic_spec.rb b/spec/models/category_featured_topic_spec.rbindex 0812237863b..f994fe53718 100644--- a/spec/models/category_featured_topic_spec.rb+++ b/spec/models/category_featured_topic_spec.rb@@ -53,6 +53,20 @@ describe CategoryFeaturedTopic doexpect(CategoryFeaturedTopic.count).to be(1)end+ it 'should not include topics from suppressed categories' do+ CategoryFeaturedTopic.feature_topics_for(category)+ expect(+ CategoryFeaturedTopic.where(category_id: category.id).order('rank asc').pluck(:topic_id)+ ).to contain_exactly(category_post.topic.id)++ category.update(suppress_from_latest: true)++ CategoryFeaturedTopic.feature_topics_for(category)+ expect(+ CategoryFeaturedTopic.where(category_id: category.id).order('rank asc').pluck(:topic_id)+ ).to_not contain_exactly(category_post.topic.id)+ end+it 'should feature stuff in the correct order' docategory = Fabricate(:category, num_featured_topics: 2)_t5 = Fabricate(:topic, category_id: category.id, bumped_at: 12.minutes.ago)