diff --git a/app/models/category_featured_topic.rb b/app/models/category_featured_topic.rb index 9540edf4d..2704aff84 100644 --- a/app/models/category_featured_topic.rb +++ b/app/models/category_featured_topic.rb @@ -5,18 +5,25 @@ class CategoryFeaturedTopic < ActiveRecord::Base # Populates the category featured topics def self.feature_topics transaction do - Category.all.each do |c| - feature_topics_for(c) - CategoryFeaturedUser.feature_users_in(c) + current = {} + CategoryFeaturedTopic.select(:topic_id, :category_id).order(:rank).each do |f| + (current[f.category_id] ||= []) << f.topic_id + end + Category.select(:id, :topic_id).all.each do |c| + + feature_topics_for(c, current[c.id] || []) + CategoryFeaturedUser.feature_users_in(c.id) end end end - def self.feature_topics_for(c) + def self.feature_topics_for(c, existing=nil) return if c.blank? query = TopicQuery.new(self.fake_admin, per_page: SiteSetting.category_featured_topics, except_topic_ids: [c.topic_id], visible: true) - results = query.list_category(c).topic_ids.to_a + results = query.list_category(c).topic_ids + + return if results == existing CategoryFeaturedTopic.transaction do CategoryFeaturedTopic.delete_all(category_id: c.id) diff --git a/app/models/category_featured_user.rb b/app/models/category_featured_user.rb index 8a7fc6a4b..3bb639c16 100644 --- a/app/models/category_featured_user.rb +++ b/app/models/category_featured_user.rb @@ -6,7 +6,14 @@ class CategoryFeaturedUser < ActiveRecord::Base 5 end - def self.feature_users_in(category) + def self.feature_users_in(category_or_category_id) + category_id = + if Fixnum === category_or_category_id + category_or_category_id + else + category_or_category_id.id + end + # Figure out most recent posters in the category most_recent_user_ids = exec_sql " SELECT x.user_id @@ -21,12 +28,17 @@ class CategoryFeaturedUser < ActiveRecord::Base ) AS x ORDER BY x.created_at DESC LIMIT :max_featured_users; - ", category_id: category.id, max_featured_users: max_featured_users + ", category_id: category_id, max_featured_users: max_featured_users + + user_ids = most_recent_user_ids.map{|uc| uc['user_id'].to_i} + current = CategoryFeaturedUser.where(category_id: category_id).order(:id).pluck(:user_id) + + return if current == user_ids transaction do - CategoryFeaturedUser.delete_all category_id: category.id - most_recent_user_ids.each do |uc| - create(category_id: category.id, user_id: uc['user_id']) + CategoryFeaturedUser.delete_all category_id: category_id + user_ids.each do |user_id| + create(category_id: category_id, user_id: user_id) end end diff --git a/app/models/topic_list.rb b/app/models/topic_list.rb index 117d82765..715468b96 100644 --- a/app/models/topic_list.rb +++ b/app/models/topic_list.rb @@ -42,8 +42,8 @@ class TopicList end def topic_ids - return [] if @topics_input.blank? - @topics_input.map {|t| t.id} + return [] unless @topics_input + @topics_input.pluck(:id) end def attributes