discourse/app/models/category_featured_topic.rb

76 lines
2.1 KiB
Ruby
Raw Normal View History

2013-02-05 13:16:51 -06:00
class CategoryFeaturedTopic < ActiveRecord::Base
belongs_to :category
belongs_to :topic
# Populates the category featured topics
def self.feature_topics
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).find_each do |c|
CategoryFeaturedTopic.feature_topics_for(c, current[c.id] || [])
2013-02-05 13:16:51 -06:00
end
end
def self.feature_topics_for(c, existing=nil)
return if c.blank?
2013-02-07 09:45:24 -06:00
query_opts = {
per_page: SiteSetting.category_featured_topics,
except_topic_ids: [c.topic_id],
visible: true,
no_definitions: true
}
# Add topics, even if they're in secured categories:
query = TopicQuery.new(CategoryFeaturedTopic.fake_admin, query_opts)
results = query.list_category_topic_ids(c).uniq
# Add some topics that are visible to everyone:
anon_query = TopicQuery.new(nil, query_opts.merge({except_topic_ids: [c.topic_id] + results}))
results += anon_query.list_category_topic_ids(c).uniq
return if results == existing
2013-02-05 13:16:51 -06:00
CategoryFeaturedTopic.transaction do
CategoryFeaturedTopic.delete_all(category_id: c.id)
if results
results.each_with_index do |topic_id, idx|
begin
c.category_featured_topics.create(topic_id: topic_id, rank: idx)
rescue PG::UniqueViolation
# If another process features this topic, just ignore it
end
end
end
2013-02-05 13:16:51 -06:00
end
end
def self.fake_admin
# fake an admin
admin = User.new
admin.admin = true
admin.id = -1
admin
end
2013-08-30 12:39:31 -05:00
2013-02-05 13:16:51 -06:00
end
# == Schema Information
#
# Table name: category_featured_topics
#
# category_id :integer not null
# topic_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
2013-06-16 19:48:58 -05:00
# rank :integer default(0), not null
2013-08-13 15:09:27 -05:00
# id :integer not null, primary key
#
# Indexes
#
2013-06-16 19:48:58 -05:00
# cat_featured_threads (category_id,topic_id) UNIQUE
# index_category_featured_topics_on_category_id_and_rank (category_id,rank)
#