diff --git a/app/models/topic_list.rb b/app/models/topic_list.rb index e0b3fd8353b..43ec43f1fde 100644 --- a/app/models/topic_list.rb +++ b/app/models/topic_list.rb @@ -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 diff --git a/spec/models/topic_list_spec.rb b/spec/models/topic_list_spec.rb index 319137f0519..fc1fe8f3dbb 100644 --- a/spec/models/topic_list_spec.rb +++ b/spec/models/topic_list_spec.rb @@ -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