preload owner in topic list

This commit is contained in:
Sam 2017-02-14 17:44:17 -05:00
parent c7253f1d38
commit 13d070c823
3 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,3 @@
{{#if context.topic.owner}}
<a class='assigned-to discourse-tag simple' href="{{context.topic.owner.path}}">@{{context.topic.owner.username}}</a>
{{/if}}

View File

@ -0,0 +1,7 @@
{{#unless context.topic.tags}}
{{#if context.topic.owner}}
<div class='discourse-tags'>
<a class='assigned-to discourse-tag simple' href="{{context.topic.owner.path}}">@{{context.topic.owner.username}}</a>
</div>
{{/if}}
{{/unless}}

View File

@ -66,6 +66,46 @@ after_initialize do
render json: success_json render json: success_json
end end
class ::Topic
def owner
@owner ||
if user_id = custom_fields["assigned_to_id"]
@owner = User.find_by(id: user_id)
end
end
def preload_owner(owner)
@owner = owner
end
end
TopicList.preloaded_custom_fields << "assigned_to_id"
TopicList.on_preload do |topics|
if topics.length > 0
users = User.where("id in (
SELECT value::int
FROM topic_custom_fields
WHERE name = 'assigned_to_id' AND topic_id IN (?)
)", topics.map(&:id))
.select(:id, :email, :username, :uploaded_avatar_id)
map = {}
users.each{|u| map[u.id] = u}
topics.each do |t|
if id = t.custom_fields['assigned_to_id']
t.preload_owner(map[id.to_i])
end
end
end
end
require_dependency 'listable_topic_serializer'
class ::ListableTopicSerializer
has_one :owner, serializer: BasicUserSerializer, embed: :objects
end
require_dependency 'topic_view_serializer' require_dependency 'topic_view_serializer'
class ::TopicViewSerializer class ::TopicViewSerializer
attributes :assigned_to_user attributes :assigned_to_user