Check for a second page of messages without counting the room

The original room shows its welcome box until it holds more than a page of
messages, and paged? answered that with a COUNT(*) over every message in
the room on each visit. Asking whether there is a row past the first page
answers the same question while reading at most a page of index entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NuTtwJKhb77Dv7EQqv2C3C
This commit is contained in:
Marcello Costagliola
2026-10-05 01:03:39 +02:00
parent 254dd1d46f
commit cf0ba58d8d
2 changed files with 13 additions and 1 deletions
+1 -1
View File
@@ -49,7 +49,7 @@ module Message::Pagination
end
def paged?
count > PAGE_SIZE
offset(PAGE_SIZE).exists?
end
end
end
+12
View File
@@ -65,6 +65,18 @@ class MessageTest < ActiveSupport::TestCase
assert_equal room.messages.ordered.first(Message::Pagination::PAGE_SIZE).map(&:id), room.messages.first_page.map(&:id)
end
test "paged? tells whether a room has more than a page of messages without counting them all" do
room = Rooms::Closed.create!(name: "Paging", creator: users(:david))
Message.insert_all Array.new(Message::Pagination::PAGE_SIZE) { |i| { room_id: room.id, creator_id: users(:david).id, client_message_id: "paging-#{i}" } }
assert_no_queries_match(/COUNT/i) do
assert_not room.messages.paged?
Message.insert_all [ { room_id: room.id, creator_id: users(:david).id, client_message_id: "paging-next" } ]
assert room.messages.paged?
end
end
private
def create_new_message_in(room)
room.messages.create!(creator: users(:jason), body: "Hello", client_message_id: "123")