2013-02-05 14:16:51 -05:00
|
|
|
class CategoryFeaturedTopic < ActiveRecord::Base
|
|
|
|
belongs_to :category
|
|
|
|
belongs_to :topic
|
|
|
|
|
|
|
|
# Populates the category featured topics
|
|
|
|
def self.feature_topics
|
|
|
|
transaction do
|
2013-02-07 10:45:24 -05:00
|
|
|
Category.all.each do |c|
|
2013-02-05 14:16:51 -05:00
|
|
|
feature_topics_for(c)
|
|
|
|
CategoryFeaturedUser.feature_users_in(c)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.feature_topics_for(c)
|
2013-02-28 13:54:12 -05:00
|
|
|
return if c.blank?
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
CategoryFeaturedTopic.transaction do
|
2013-05-28 14:54:00 -04:00
|
|
|
CategoryFeaturedTopic.delete_all(category_id: c.id)
|
2013-06-04 21:22:47 -04:00
|
|
|
|
|
|
|
# fake an admin
|
|
|
|
admin = User.new
|
|
|
|
admin.admin = true
|
|
|
|
admin.id = -1
|
|
|
|
|
2013-06-20 16:07:53 -04:00
|
|
|
query = TopicQuery.new(admin, per_page: SiteSetting.category_featured_topics, except_topic_id: c.topic_id, visible: true)
|
2013-05-28 14:54:00 -04:00
|
|
|
results = query.list_category(c)
|
|
|
|
if results.present?
|
|
|
|
results.topic_ids.each_with_index do |topic_id, idx|
|
|
|
|
c.category_featured_topics.create(topic_id: topic_id, rank: idx)
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == 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 20:48:58 -04:00
|
|
|
# rank :integer default(0), not null
|
2013-08-13 16:09:27 -04:00
|
|
|
# id :integer not null, primary key
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2013-06-16 20:48:58 -04:00
|
|
|
# cat_featured_threads (category_id,topic_id) UNIQUE
|
|
|
|
# index_category_featured_topics_on_category_id_and_rank (category_id,rank)
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
|