FEATURE: allow plugins to preload data in topic list

This commit is contained in:
Sam 2017-02-14 16:29:06 -05:00
parent 21e21b9882
commit 89d5e8ab4b
2 changed files with 44 additions and 1 deletions

View File

@ -6,6 +6,25 @@ class TopicList
cattr_accessor :preloaded_custom_fields
self.preloaded_custom_fields = Set.new
def self.on_preload(blk)
(@preload ||= Set.new) << blk
end
def self.cancel_preload(blk)
if @preload
@preload.delete blk
if @preload.length == 0
@preload = nil
end
end
end
def self.preload(topics)
if @preload
@preload.each{|preload| preload.call(topics)}
end
end
attr_accessor :more_topics_url,
:prev_topics_url,
:draft,
@ -97,6 +116,8 @@ class TopicList
Topic.preload_custom_fields(@topics, preloaded_custom_fields)
end
TopicList.preload(@topics)
@topics
end

View File

@ -1,7 +1,12 @@
require 'rails_helper'
describe TopicList do
let!(:topic) { Fabricate(:topic) }
let!(:topic) {
t = Fabricate(:topic)
t.allowed_user_ids = [t.user.id]
t
}
let(:user) { topic.user }
let(:topic_list) { TopicList.new("liked", user, [topic]) }
@ -23,6 +28,23 @@ describe TopicList do
end
end
context "preload" do
it "allows preloading of data" do
preloaded_topic = false
preloader = lambda do |topics|
expect(topics.length).to eq(1)
preloaded_topic = true
end
TopicList.on_preload(preloader)
topic_list.topics
expect(preloaded_topic).to eq(true)
TopicList.cancel_preload(preloader)
end
end
context "DiscourseTagging enabled" do
before do
SiteSetting.tagging_enabled = true