Merge pull request #322: Cache boosts with their message instead of one by one

Reviewed and merged by GPT on behalf of DHH.
This commit is contained in:
GPT on behalf of DHH
2026-10-07 10:32:12 +02:00
4 changed files with 60 additions and 19 deletions
@@ -13,6 +13,15 @@ class Messages::BoostsControllerTest < ActionDispatch::IntegrationTest
assert_select ".message__boost-inline a.boost__action[data-action='soft-keyboard#open']"
end
test "index looks up the boosters of all boosts at once" do
@message.boosts.create! booster: users(:jason), content: "🎉"
queries_with_two_boosts = count_queries { get message_boosts_url(@message) }
boost = @message.boosts.create! booster: users(:kevin), content: "👀"
assert_equal queries_with_two_boosts, count_queries { get message_boosts_url(@message) }
assert_select "#" + dom_id(boost)
end
test "create" do
assert_turbo_stream_broadcasts [ @message.room, :messages ], count: 1 do
assert_difference -> { @message.boosts.count }, 1 do
@@ -30,4 +39,12 @@ class Messages::BoostsControllerTest < ActionDispatch::IntegrationTest
end
end
end
private
def count_queries(&block)
count = 0
counter = ->(*, payload) { count += 1 unless payload[:name] == "SCHEMA" || payload[:cached] }
ActiveSupport::Notifications.subscribed(counter, "sql.active_record", &block)
count
end
end
+26
View File
@@ -27,6 +27,32 @@ class MessagesCachingTest < ActionDispatch::IntegrationTest
end
end
test "boosts are cached inside their message instead of one fragment each" do
with_memory_cache do
cache_keys = []
subscriber = ActiveSupport::Notifications.subscribe(/\Acache_(read|read_multi|write|write_multi)\.active_support\z/) do |*, payload|
cache_keys.concat(payload[:key].is_a?(Hash) ? payload[:key].keys : Array(payload[:key]))
end
get room_messages_url(rooms(:watercooler))
assert_response :success
assert_select "#" + dom_id(boosts(:fourth_by_bender))
assert_select "#" + dom_id(boosts(:thirteenth))
assert_empty cache_keys.grep(%r{messages/boosts/_boost})
boost = messages(:fourth).boosts.create! booster: users(:jason), content: "🎉"
get room_messages_url(rooms(:watercooler))
refreshed = response.body
Rails.cache.clear
get room_messages_url(rooms(:watercooler))
assert_equal refreshed, response.body
assert_select "##{dom_id(messages(:fourth))} ##{dom_id(boost)}", text: /🎉/
ensure
ActiveSupport::Notifications.unsubscribe(subscriber)
end
end
private
def with_memory_cache
old_cache = Rails.cache