2013-02-05 14:16:51 -05:00
|
|
|
class CategoryList
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
2013-05-28 21:15:30 -04:00
|
|
|
attr_accessor :categories,
|
|
|
|
:uncategorized,
|
|
|
|
:draft,
|
|
|
|
:draft_key,
|
|
|
|
:draft_sequence
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-08-17 17:23:16 -04:00
|
|
|
def initialize(guardian=nil, options={})
|
2013-05-28 14:54:00 -04:00
|
|
|
@guardian = guardian || Guardian.new
|
2013-10-17 02:44:56 -04:00
|
|
|
@options = options
|
2013-05-28 14:54:00 -04:00
|
|
|
|
|
|
|
find_categories
|
2016-08-17 17:23:16 -04:00
|
|
|
end
|
2013-05-28 15:56:46 -04:00
|
|
|
|
2016-08-17 17:23:16 -04:00
|
|
|
def preload_key
|
|
|
|
"categories_list".freeze
|
2013-05-28 14:54:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Find a list of all categories to associate the topics with
|
|
|
|
def find_categories
|
2016-08-17 17:23:16 -04:00
|
|
|
@categories = Category.includes(:topic_only_relative_url, subcategories: [:topic_only_relative_url]).secured(@guardian)
|
|
|
|
@categories = @categories.where(suppress_from_homepage: false) if @options[:is_homepage]
|
2014-05-21 10:37:37 -04:00
|
|
|
|
2016-08-17 17:23:16 -04:00
|
|
|
unless SiteSetting.allow_uncategorized_topics
|
|
|
|
# TODO: also make sure the uncategorized is empty
|
|
|
|
@categories = @categories.where("id <> #{SiteSetting.uncategorized_category_id}")
|
2014-07-02 15:10:39 -04:00
|
|
|
end
|
|
|
|
|
2014-05-16 11:33:44 -04:00
|
|
|
if SiteSetting.fixed_category_positions
|
2016-08-17 17:23:16 -04:00
|
|
|
@categories = @categories.order(:position, :id)
|
2014-05-16 11:33:44 -04:00
|
|
|
else
|
|
|
|
@categories = @categories.order('COALESCE(categories.posts_week, 0) DESC')
|
|
|
|
.order('COALESCE(categories.posts_month, 0) DESC')
|
|
|
|
.order('COALESCE(categories.posts_year, 0) DESC')
|
2014-05-21 10:37:37 -04:00
|
|
|
.order('id ASC')
|
2014-05-16 11:33:44 -04:00
|
|
|
end
|
2013-05-28 14:54:00 -04:00
|
|
|
|
2014-05-16 11:33:44 -04:00
|
|
|
@categories = @categories.to_a
|
2015-09-07 12:52:53 -04:00
|
|
|
|
2015-09-15 08:58:22 -04:00
|
|
|
category_user = {}
|
|
|
|
unless @guardian.anonymous?
|
|
|
|
category_user = Hash[*CategoryUser.where(user: @guardian.user).pluck(:category_id, :notification_level).flatten]
|
|
|
|
end
|
|
|
|
|
2015-09-07 12:52:53 -04:00
|
|
|
allowed_topic_create = Set.new(Category.topic_create_allowed(@guardian).pluck(:id))
|
|
|
|
@categories.each do |category|
|
2015-09-15 08:58:22 -04:00
|
|
|
category.notification_level = category_user[category.id]
|
2015-09-07 12:52:53 -04:00
|
|
|
category.permission = CategoryGroup.permission_types[:full] if allowed_topic_create.include?(category.id)
|
2015-09-15 08:58:22 -04:00
|
|
|
category.has_children = category.subcategories.present?
|
2015-09-07 12:52:53 -04:00
|
|
|
end
|
|
|
|
|
2016-08-17 17:23:16 -04:00
|
|
|
subcategories = {}
|
|
|
|
to_delete = Set.new
|
|
|
|
@categories.each do |c|
|
|
|
|
if c.parent_category_id.present?
|
|
|
|
subcategories[c.parent_category_id] ||= []
|
|
|
|
subcategories[c.parent_category_id] << c.id
|
|
|
|
to_delete << c
|
2014-01-15 14:11:19 -05:00
|
|
|
end
|
2013-05-28 14:54:00 -04:00
|
|
|
end
|
|
|
|
|
2016-08-17 17:23:16 -04:00
|
|
|
@categories.each { |c| c.subcategory_ids = subcategories[c.id] }
|
2015-06-10 14:36:47 -04:00
|
|
|
|
2016-08-17 17:23:16 -04:00
|
|
|
@categories.delete_if { |c| to_delete.include?(c) }
|
2015-06-10 14:36:47 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|