Files
turbovault-app/app/controllers/collections_controller.rb
2026-03-29 02:37:49 -04:00

79 lines
2.1 KiB
Ruby

# typed: true
class CollectionsController < ApplicationController
extend T::Sig
before_action :require_authentication
before_action :set_collection, only: [ :show, :edit, :update, :destroy, :games ]
sig { void }
def index
@root_collections = current_user.collections.root_collections.order(:name)
end
sig { void }
def show
@games = @collection.games.includes(:platform, :genres).page(params[:page]).per(25)
end
sig { void }
def new
@collection = current_user.collections.build
@collections = current_user.collections.root_collections.order(:name)
end
sig { void }
def create
@collection = current_user.collections.build(collection_params)
if @collection.save
redirect_to @collection, notice: "Collection was successfully created."
else
@collections = current_user.collections.root_collections.order(:name)
render :new, status: :unprocessable_entity
end
end
sig { void }
def edit
@collections = current_user.collections.root_collections.where.not(id: @collection.id).order(:name)
end
sig { void }
def update
if @collection.update(collection_params)
redirect_to @collection, notice: "Collection was successfully updated."
else
@collections = current_user.collections.root_collections.where.not(id: @collection.id).order(:name)
render :edit, status: :unprocessable_entity
end
end
sig { void }
def destroy
@collection.destroy
redirect_to collections_path, notice: "Collection was successfully deleted."
end
sig { void }
def games
# Same as show, but maybe with different view
@games = @collection.games.includes(:platform, :genres).page(params[:page]).per(25)
render :show
end
private
sig { void }
def set_collection
@collection = current_user.collections.find(params[:id])
end
sig { returns(T.untyped) }
def collection_params
permitted = params.require(:collection).permit(:name, :description, :parent_collection_id)
# Convert empty string to nil for parent_collection_id
permitted[:parent_collection_id] = nil if permitted[:parent_collection_id].blank?
permitted
end
end