2013-02-05 14:16:51 -05:00
|
|
|
class CategoryList
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
|
|
|
attr_accessor :categories, :topic_users, :uncategorized
|
|
|
|
|
2013-05-13 04:04:03 -04:00
|
|
|
def initialize(guardian)
|
2013-05-13 04:47:32 -04:00
|
|
|
guardian ||= Guardian.new
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
@categories = Category
|
2013-03-23 11:02:59 -04:00
|
|
|
.includes(featured_topics: [:category])
|
2013-02-05 14:16:51 -05:00
|
|
|
.includes(:featured_users)
|
2013-04-05 16:09:27 -04:00
|
|
|
.where('topics.visible' => true)
|
2013-05-13 04:04:03 -04:00
|
|
|
.secured(guardian)
|
2013-04-05 16:09:27 -04:00
|
|
|
.order('categories.topics_week desc, categories.topics_month desc, categories.topics_year desc')
|
2013-05-10 02:47:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
@categories = @categories.to_a
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Support for uncategorized topics
|
|
|
|
uncategorized_topics = Topic
|
|
|
|
.listable_topics
|
|
|
|
.where(category_id: nil)
|
|
|
|
.topic_list_order
|
|
|
|
.limit(SiteSetting.category_featured_topics)
|
|
|
|
if uncategorized_topics.present?
|
|
|
|
|
|
|
|
totals = Topic.exec_sql("SELECT SUM(CASE WHEN created_at >= (CURRENT_TIMESTAMP - INTERVAL '1 WEEK') THEN 1 ELSE 0 END) as topics_week,
|
|
|
|
SUM(CASE WHEN created_at >= (CURRENT_TIMESTAMP - INTERVAL '1 MONTH') THEN 1 ELSE 0 END) as topics_month,
|
|
|
|
SUM(CASE WHEN created_at >= (CURRENT_TIMESTAMP - INTERVAL '1 YEAR') THEN 1 ELSE 0 END) as topics_year,
|
|
|
|
COUNT(*) AS topic_count
|
2013-02-07 10:45:24 -05:00
|
|
|
FROM topics
|
|
|
|
WHERE topics.visible
|
2013-02-05 14:16:51 -05:00
|
|
|
AND topics.deleted_at IS NULL
|
|
|
|
AND topics.category_id IS NULL
|
|
|
|
AND topics.archetype <> '#{Archetype.private_message}'").first
|
|
|
|
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
uncategorized = Category.new({name: SiteSetting.uncategorized_name,
|
2013-02-12 17:39:31 -05:00
|
|
|
slug: Slug.for(SiteSetting.uncategorized_name),
|
2013-05-03 17:55:04 -04:00
|
|
|
color: SiteSetting.uncategorized_color,
|
|
|
|
text_color: SiteSetting.uncategorized_text_color,
|
2013-02-05 14:16:51 -05:00
|
|
|
featured_topics: uncategorized_topics}.merge(totals))
|
|
|
|
|
|
|
|
# Find the appropriate place to insert it:
|
|
|
|
insert_at = nil
|
|
|
|
@categories.each_with_index do |c, idx|
|
|
|
|
if totals['topics_week'].to_i > (c.topics_week || 0)
|
|
|
|
insert_at = idx
|
|
|
|
break
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@categories.insert(insert_at || @categories.size, uncategorized)
|
|
|
|
end
|
|
|
|
|
2013-05-13 04:04:03 -04:00
|
|
|
unless guardian.can_create?(Category)
|
2013-05-07 15:31:49 -04:00
|
|
|
# Remove categories with no featured topics unless we have the ability to edit one
|
2013-02-28 13:54:12 -05:00
|
|
|
@categories.delete_if { |c| c.featured_topics.blank? }
|
2013-05-07 15:31:49 -04:00
|
|
|
else
|
|
|
|
# Show all categories to people who have the ability to edit and delete categories
|
2013-05-07 15:40:18 -04:00
|
|
|
if @categories.size > 0
|
|
|
|
@categories.insert(@categories.size, *Category.where('id not in (?)', @categories.map(&:id).compact).to_a)
|
|
|
|
else
|
|
|
|
@categories = Category.all.to_a
|
|
|
|
end
|
2013-02-21 10:42:27 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# Get forum topic user records if appropriate
|
2013-05-13 04:04:03 -04:00
|
|
|
if guardian.current_user
|
2013-02-05 14:16:51 -05:00
|
|
|
topics = []
|
2013-02-28 13:54:12 -05:00
|
|
|
@categories.each { |c| topics << c.featured_topics }
|
2013-02-05 14:16:51 -05:00
|
|
|
topics << @uncategorized
|
|
|
|
|
|
|
|
topics.flatten! if topics.present?
|
|
|
|
topics.compact! if topics.present?
|
|
|
|
|
2013-05-13 04:04:03 -04:00
|
|
|
topic_lookup = TopicUser.lookup_for(guardian.current_user, topics)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# Attach some data for serialization to each topic
|
2013-02-28 13:54:12 -05:00
|
|
|
topics.each { |ft| ft.user_data = topic_lookup[ft.id] }
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|