view app/controllers/pastes_controller.rb @ 465:f109a8aacad5

Update gems
author nanaya <me@nanaya.pro>
date Fri, 31 May 2019 09:10:28 +0900
parents f11862e58af4
children d2ef59c9a4b4
line wrap: on
line source

class PastesController < ApplicationController
  # GET /1
  # GET /1.txt
  def show
    @paste = Paste.safe_find(params[:id])
    return head :not_found unless @paste

    if params[:id] != @paste.to_param
      redirect_to :action => :show,
        :id => @paste.to_param,
        :format => params[:format]
      return
    end

    respond_to do |format|
      format.html
      format.txt
    end
  end

  # GET /
  def new
    @paste = Paste.new
    @paste.set_paste_key
    @paste.paste = Paste.safe_find(params[:base]).try(:paste)

    respond_to do |format|
      format.html # new.html.erb
    end
  end

  # POST /
  # POST /pastes.json
  # POST /pastes.txt
  def create
    created, @paste, @fresh = Paste.graceful_create paste_params.merge(:ip => request.remote_ip)

    respond_to do |format|
      if created
        if @fresh
          format.html { redirect_to @paste, :notice => "Paste was successfully created." }
          format.json { render :json => @paste, :status => :created, :location => @paste }
        else
          format.html { redirect_to paste_path(@paste) }
          format.json { render :json => @paste }
        end
      else
        flash.now[:alert] = @paste.errors.full_messages.to_sentence
        format.html { render :action => "new" }
        format.json { render :json => @paste.errors, :status => :unprocessable_entity }
      end
      format.txt
    end
  end

  def destroy
    @paste = Paste.safe_find(params[:id])
    if @paste.safe_destroy(params[:paste][:key])
      redirect_to root_path, :notice => "Paste ##{params[:id]} deleted"
    else
      flash.now[:alert] = @paste.errors.full_messages.to_sentence
      render :show
    end
  end

  private

  def paste_params
    params.require(:paste).permit(:paste, :paste_gzip, :paste_gzip_base64, :is_private, :key, :language)
  end
end