mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
PERF: Allow preloading 'recent time read' for a user (#9076)
This will be used when serializing multiple user cards
This commit is contained in:
@@ -2287,4 +2287,32 @@ describe User do
|
||||
expect(user.approved).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#recent_time_read" do
|
||||
fab!(:user) { Fabricate(:user) }
|
||||
fab!(:user2) { Fabricate(:user) }
|
||||
|
||||
before_all do
|
||||
UserVisit.create(user_id: user.id, visited_at: 1.minute.ago, posts_read: 1, mobile: false, time_read: 10)
|
||||
UserVisit.create(user_id: user.id, visited_at: 2.days.ago, posts_read: 1, mobile: false, time_read: 20)
|
||||
UserVisit.create(user_id: user.id, visited_at: 1.week.ago, posts_read: 1, mobile: false, time_read: 30)
|
||||
UserVisit.create(user_id: user.id, visited_at: 1.year.ago, posts_read: 1, mobile: false, time_read: 40) # Old, should be ignored
|
||||
UserVisit.create(user_id: user2.id, visited_at: 1.minute.ago, posts_read: 1, mobile: false, time_read: 50)
|
||||
end
|
||||
|
||||
it "calculates correctly" do
|
||||
expect(user.recent_time_read).to eq(60)
|
||||
expect(user2.recent_time_read).to eq(50)
|
||||
end
|
||||
|
||||
it "preloads correctly" do
|
||||
User.preload_recent_time_read([user, user2])
|
||||
|
||||
expect(user.instance_variable_get(:@recent_time_read)).to eq(60)
|
||||
expect(user2.instance_variable_get(:@recent_time_read)).to eq(50)
|
||||
|
||||
expect(user.recent_time_read).to eq(60)
|
||||
expect(user2.recent_time_read).to eq(50)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user