Refactor message loading in RoomsController to use combined scopes

- Simplified message queries in RoomsController#find_messages by replacing multiple includes and preloads with consolidated scopes: with_creator, with_attachment_details, and with_boosts.
- Defined new scopes in Message model to handle rich text, attachments, and boosts associations for cleaner and more maintainable code.
This commit is contained in:
Raul Popadineti
2025-09-17 13:39:30 +03:00
parent e89a834cde
commit 03d1c45d97
2 changed files with 8 additions and 6 deletions
+1 -5
View File
@@ -32,11 +32,7 @@ class RoomsController < ApplicationController
end
def find_messages
messages = @room.messages.with_rich_text_body_and_embeds
.with_attached_attachment
.preload(creator: :avatar_attachment)
.includes(attachment_blob: :variant_records)
.includes(boosts: :booster)
messages = @room.messages.with_creator.with_attachment_details.with_boosts
if show_first_message = messages.find_by(id: params[:message_id])
@messages = messages.page_around(show_first_message)
+7 -1
View File
@@ -12,7 +12,13 @@ class Message < ApplicationRecord
after_create_commit -> { room.receive(self) }
scope :ordered, -> { order(:created_at) }
scope :with_creator, -> { includes(:creator) }
scope :with_creator, -> { preload(creator: :avatar_attachment) }
scope :with_attachment_details, -> {
with_rich_text_body_and_embeds
with_attached_attachment
.includes(attachment_blob: :variant_records)
}
scope :with_boosts, -> { includes(boosts: :booster) }
def plain_text_body
body.to_plain_text.presence || attachment&.filename&.to_s || ""