mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-09-10 02:32:02 +09:00
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:
@@ -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)
|
||||
|
||||
@@ -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 || ""
|
||||
|
||||
Reference in New Issue
Block a user