class ServerTemplatesController < ApplicationController before_action :set_server_template, only: [ :show, :edit, :update, :destroy ] def index @server_templates = current_user.server_templates.order(created_at: :desc) end def show authorize_user! @overlays = Overlay.for_user(current_user).order(:name) end def new @server_template = current_user.server_templates.build end def create @server_template = current_user.server_templates.build(server_template_params) if @server_template.save Activity.log(current_user, "created", "ServerTemplate", @server_template.id, { name: @server_template.name }) redirect_to @server_template, notice: "Template created successfully!" else render :new, status: :unprocessable_entity end end def edit authorize_user! end def update authorize_user! if @server_template.update(server_template_params) Activity.log(current_user, "updated", "ServerTemplate", @server_template.id, { name: @server_template.name }) redirect_to @server_template, notice: "Template updated successfully!" else render :edit, status: :unprocessable_entity end end def destroy authorize_user! @server_template.destroy Activity.log(current_user, "deleted", "ServerTemplate", @server_template.id, { name: @server_template.name }) redirect_to server_templates_path, notice: "Template deleted successfully!" end private def set_server_template @server_template = ServerTemplate.find(params[:id]) end def server_template_params params.require(:server_template).permit(:name) end def authorize_user! redirect_to server_templates_path, alert: "Not authorized" unless @server_template.user_id == current_user.id end end