discourse/app/models/category_featured_topic.rb

53 lines
1.5 KiB
Ruby
Raw Normal View History

2013-05-23 21:35:14 -05: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
#
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
transaction do
2013-02-07 09:45:24 -06:00
Category.all.each do |c|
2013-02-05 13:16:51 -06:00
feature_topics_for(c)
CategoryFeaturedUser.feature_users_in(c)
end
end
end
def self.feature_topics_for(c)
return if c.blank?
2013-02-07 09:45:24 -06:00
2013-02-05 13:16:51 -06:00
CategoryFeaturedTopic.transaction do
exec_sql "DELETE FROM category_featured_topics WHERE category_id = :category_id", category_id: c.id
exec_sql "INSERT INTO category_featured_topics (category_id, topic_id, created_at, updated_at)
SELECT :category_id,
ft.id,
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP
FROM topics AS ft
WHERE ft.category_id = :category_id
AND ft.visible
AND ft.deleted_at IS NULL
2013-04-03 10:45:53 -05:00
AND ft.archetype <> :private_message
2013-02-05 13:16:51 -06:00
ORDER BY ft.bumped_at DESC
2013-02-07 09:45:24 -06:00
LIMIT :featured_limit",
2013-02-05 13:16:51 -06:00
category_id: c.id,
2013-04-03 10:45:53 -05:00
private_message: Archetype.private_message,
2013-02-05 13:16:51 -06:00
featured_limit: SiteSetting.category_featured_topics
end
end
end