98 lines
2.8 KiB
Ruby
98 lines
2.8 KiB
Ruby
class RoomsController < ApplicationController
|
|
before_action :set_room, only: %i[ show edit update destroy ]
|
|
|
|
# GET /rooms or /rooms.json
|
|
# 1. Übersicht aller Räume (Kacheldesign)
|
|
def index
|
|
@rooms = Room.all.order(
|
|
Room.arel_table[:building].asc.nulls_last,
|
|
Room.arel_table[:floor].asc.nulls_last,
|
|
:name
|
|
# :building, :floor, :name
|
|
)
|
|
|
|
# NEU: Filtert die Räume live nach Name, Gebäude oder Etage
|
|
if params[:query].present?
|
|
query_str = "%#{params[:query]}%"
|
|
@rooms = @rooms.where(
|
|
"rooms.name LIKE :q OR rooms.building LIKE :q OR rooms.floor LIKE :q",
|
|
q: query_str
|
|
)
|
|
end
|
|
end
|
|
|
|
# GET /rooms/1 or /rooms/1.json
|
|
# 2. Detailansicht eines Raums mit integrierter Live-Suche für dessen Inventar
|
|
def show
|
|
# Basis-Abfrage: Alle Items dieses Raums laden
|
|
@items = @room.items.includes(:category, :user).order(created_at: :desc)
|
|
|
|
# Wenn in der Suchleiste getippt wird, filtert der Controller live
|
|
if params[:query].present?
|
|
query_str = "%#{params[:query]}%"
|
|
@items = @items.where(
|
|
"items.name LIKE :q OR items.sku LIKE :q OR items.serial_number LIKE :q OR items.sticker_id LIKE :q",
|
|
q: query_str
|
|
)
|
|
end
|
|
end
|
|
|
|
# GET /rooms/new
|
|
def new
|
|
@room = Room.new
|
|
end
|
|
|
|
# GET /rooms/1/edit
|
|
def edit
|
|
end
|
|
|
|
# POST /rooms or /rooms.json
|
|
def create
|
|
@room = Room.new(room_params)
|
|
|
|
respond_to do |format|
|
|
if @room.save
|
|
format.html { redirect_to @room, notice: "Room was successfully created." }
|
|
format.json { render :show, status: :created, location: @room }
|
|
else
|
|
format.html { render :new, status: :unprocessable_content }
|
|
format.json { render json: @room.errors, status: :unprocessable_content }
|
|
end
|
|
end
|
|
end
|
|
|
|
# PATCH/PUT /rooms/1 or /rooms/1.json
|
|
def update
|
|
respond_to do |format|
|
|
if @room.update(room_params)
|
|
format.html { redirect_to @room, notice: "Room was successfully updated.", status: :see_other }
|
|
format.json { render :show, status: :ok, location: @room }
|
|
else
|
|
format.html { render :edit, status: :unprocessable_content }
|
|
format.json { render json: @room.errors, status: :unprocessable_content }
|
|
end
|
|
end
|
|
end
|
|
|
|
# DELETE /rooms/1 or /rooms/1.json
|
|
def destroy
|
|
@room.destroy!
|
|
|
|
respond_to do |format|
|
|
format.html { redirect_to rooms_path, notice: "Room was successfully destroyed.", status: :see_other }
|
|
format.json { head :no_content }
|
|
end
|
|
end
|
|
|
|
private
|
|
# Use callbacks to share common setup or constraints between actions.
|
|
def set_room
|
|
@room = Room.find(params.expect(:id))
|
|
end
|
|
|
|
def room_params
|
|
# :description wurde hier restlos entfernt
|
|
params.require(:room).permit(:name, :building, :floor)
|
|
end
|
|
end
|