mirror of
https://github.com/ryankazokas/turbovault-app.git
synced 2026-04-16 21:02:52 +00:00
66 lines
1.9 KiB
Ruby
66 lines
1.9 KiB
Ruby
class CollectionsController < ApplicationController
|
|
before_action :require_authentication
|
|
before_action :set_collection, only: [ :show, :edit, :update, :destroy, :games ]
|
|
|
|
def index
|
|
@root_collections = current_user.collections.root_collections.order(:name)
|
|
end
|
|
|
|
def show
|
|
@games = @collection.games.includes(:platform, :genres).page(params[:page]).per(25)
|
|
end
|
|
|
|
def new
|
|
@collection = current_user.collections.build
|
|
@collections = current_user.collections.root_collections.order(:name)
|
|
end
|
|
|
|
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
|
|
|
|
def edit
|
|
@collections = current_user.collections.root_collections.where.not(id: @collection.id).order(:name)
|
|
end
|
|
|
|
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
|
|
|
|
def destroy
|
|
@collection.destroy
|
|
redirect_to collections_path, notice: "Collection was successfully deleted."
|
|
end
|
|
|
|
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
|
|
|
|
def set_collection
|
|
@collection = current_user.collections.find(params[:id])
|
|
end
|
|
|
|
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
|