discourse/app/models/topic_list.rb

66 lines
1.5 KiB
Ruby
Raw Normal View History

2013-02-05 13:16:51 -06:00
require_dependency 'avatar_lookup'
class TopicList
include ActiveModel::Serialization
2013-04-02 15:52:51 -05:00
attr_accessor :more_topics_url,
:draft,
:draft_key,
:draft_sequence,
:filter
def initialize(filter, current_user, topics)
@filter = filter
2013-02-05 13:16:51 -06:00
@current_user = current_user
@topics_input = topics
end
# Lazy initialization
def topics
return @topics if @topics.present?
@topics = @topics_input.to_a
# Attach some data for serialization to each topic
@topic_lookup = TopicUser.lookup_for(@current_user, @topics) if @current_user.present?
# Create a lookup for all the user ids we need
user_ids = []
2013-02-07 09:45:24 -06:00
@topics.each do |ft|
2013-02-05 13:16:51 -06:00
user_ids << ft.user_id << ft.last_post_user_id << ft.featured_user_ids
end
avatar_lookup = AvatarLookup.new(user_ids)
2013-02-07 09:45:24 -06:00
@topics.each do |ft|
2013-02-05 13:16:51 -06:00
ft.user_data = @topic_lookup[ft.id] if @topic_lookup.present?
ft.posters = ft.posters_summary(avatar_lookup: avatar_lookup)
2013-04-02 15:52:51 -05:00
ft.topic_list = self
2013-02-05 13:16:51 -06:00
end
return @topics
end
def topic_ids
return [] if @topics_input.blank?
@topics_input.map {|t| t.id}
end
2013-02-05 13:16:51 -06:00
def attributes
hash = {'more_topics_url' => page}
if @category.present?
hash['category'] = @category
end
hash
2013-02-05 13:16:51 -06:00
end
def has_rank_details?
# Only moderators can see rank details
return false unless @current_user && @current_user.staff?
# Only show them on 'Hot'
return @filter == :hot
end
2013-02-05 13:16:51 -06:00
end