FEATURE: Add on_preload for TopicView (#13122)

This allows plugins to load data on fetched posts before each post is
individually serialized.
This commit is contained in:
Daniel Waterworth
2021-05-24 11:46:57 -05:00
committed by GitHub
parent fc6e7dd3f1
commit ca809d2c40
2 changed files with 38 additions and 0 deletions

View File

@ -15,6 +15,23 @@ describe TopicView do
let(:topic_view) { TopicView.new(topic.id, evil_trout) }
context "preload" do
it "allows preloading of data" do
preloaded_topic_view = nil
preloader = lambda do |view|
preloaded_topic_view = view
end
TopicView.on_preload(&preloader)
expect(preloaded_topic_view).to eq(nil)
topic_view
expect(preloaded_topic_view).to eq(topic_view)
TopicView.cancel_preload(&preloader)
end
end
it "raises a not found error if the topic doesn't exist" do
expect { TopicView.new(1231232, evil_trout) }.to raise_error(Discourse::NotFound)
end