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
|
2014-01-30 07:32:04 +08:00
|
|
|
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)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-30 07:32:04 +08:00
|
|
|
def self.feature_topics_for(c, existing=nil)
|
2013-03-01 02:54:12 +08:00
|
|
|
return if c.blank?
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2014-02-08 06:01:31 +08:00
|
|
|
query = TopicQuery.new(self.fake_admin, per_page: SiteSetting.category_featured_topics, except_topic_ids: [c.topic_id], visible: true, no_definitions: true)
|
2014-01-30 07:32:04 +08:00
|
|
|
results = query.list_category(c).topic_ids
|
|
|
|
|
|
|
|
return if results == existing
|
2013-09-30 14:59:16 +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)
|
2013-09-30 14:59:16 +08:00
|
|
|
if results
|
|
|
|
results.each_with_index do |topic_id, idx|
|
2013-05-29 02:54:00 +08:00
|
|
|
c.category_featured_topics.create(topic_id: topic_id, rank: idx)
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-31 01:39:31 +08:00
|
|
|
private
|
|
|
|
def self.fake_admin
|
|
|
|
# fake an admin
|
|
|
|
admin = User.new
|
|
|
|
admin.admin = true
|
|
|
|
admin.id = -1
|
|
|
|
admin
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
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
|
2014-04-15 13:53:48 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-06-17 08:48:58 +08:00
|
|
|
# rank :integer default(0), not null
|
2013-08-14 04:09:27 +08:00
|
|
|
# id :integer not null, primary key
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2013-06-17 08:48:58 +08:00
|
|
|
# cat_featured_threads (category_id,topic_id) UNIQUE
|
|
|
|
# index_category_featured_topics_on_category_id_and_rank (category_id,rank)
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|