Files
once-campfire/app/controllers/rooms_controller.rb
Raul Popadineti 03d1c45d97 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.
2025-09-17 13:39:30 +03:00

52 lines
1.2 KiB
Ruby

class RoomsController < ApplicationController
before_action :set_room, only: %i[ show destroy ]
before_action :ensure_can_administer, only: %i[ destroy ]
before_action :remember_last_room_visited, only: :show
def index
redirect_to room_url(Current.user.rooms.last)
end
def show
@messages = find_messages
end
def destroy
@room.destroy
broadcast_remove_room
redirect_to root_url
end
private
def set_room
if room = Current.user.rooms.find_by(id: params[:room_id] || params[:id])
@room = room
else
redirect_to root_url, alert: "Room not found or inaccessible"
end
end
def ensure_can_administer
head :forbidden unless Current.user.can_administer?(@room)
end
def find_messages
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)
else
@messages = messages.last_page
end
end
def room_params
params.require(:room).permit(:name)
end
def broadcast_remove_room
broadcast_remove_to :rooms, target: [ @room, :list ]
end
end