2013-02-06 03:16:51 +08: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 23:45:24 +08:00
|
|
|
Category.all.each do |c|
|
2013-02-06 03:16:51 +08:00
|
|
|
feature_topics_for(c)
|
|
|
|
CategoryFeaturedUser.feature_users_in(c)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.feature_topics_for(c)
|
2013-03-01 02:54:12 +08:00
|
|
|
return if c.blank?
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
CategoryFeaturedTopic.transaction do
|
2013-05-29 02:54:00 +08:00
|
|
|
CategoryFeaturedTopic.delete_all(category_id: c.id)
|
|
|
|
query = TopicQuery.new(nil, per_page: SiteSetting.category_featured_topics)
|
|
|
|
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-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-05-24 10:48:32 +08: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
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# cat_featured_threads (category_id,topic_id) UNIQUE
|
|
|
|
#
|
|
|
|
|