# HG changeset patch # User Edho Arief # Date 1368294068 -32400 # Node ID 5e1d728975a48a9ebc872baa6611c869d0f690db # Parent d59731c3c7bf5abb6faf7f60ae169a2644d2fe02 Refactor diff -r d59731c3c7bf -r 5e1d728975a4 app/controllers/pastes_controller.rb --- a/app/controllers/pastes_controller.rb Sun May 12 02:40:13 2013 +0900 +++ b/app/controllers/pastes_controller.rb Sun May 12 02:41:08 2013 +0900 @@ -4,11 +4,6 @@ # GET /1 # GET /1.txt def show - id = params[:id].to_i - if id.to_s != params[:id] - redirect_to paste_path(id) - return - end @paste = Paste.find(params[:id]) expires_in 1.year, :public => true @@ -36,22 +31,13 @@ # POST /pastes.json # POST /pastes.txt def create - @paste = Paste.new - @paste.ip = request.remote_ip - if params[:paste] - if params[:paste][:paste_gzip_base64] - # 1. decode from base64 - # 2. create StringIO from decoded string - # 3. unzip and read the stream - params[:paste][:paste] = Zlib::GzipReader.new(StringIO.new(Base64.decode64(params[:paste][:paste_gzip_base64]))).read - end - unless params[:paste][:paste].blank? - @paste.paste = params[:paste][:paste] - end - if params[:paste][:key] - @paste.key = params[:paste][:key] - end + if params[:paste] && params[:paste][:paste_gzip_base64] + # 1. decode from base64 + # 2. create StringIO from decoded string + # 3. unzip and read the stream + params[:paste][:paste] = Zlib::GzipReader.new(StringIO.new(Base64.decode64(params[:paste][:paste_gzip_base64].delete))).read end + @paste = Paste.new(paste_params.merge(ip: request.remote_ip)) begin respond_to do |format| @@ -76,7 +62,7 @@ end def destroy - @paste = Paste.find(params[:id].to_i) + @paste = Paste.find(params[:id]) if @paste.key == params[:paste][:key] @paste.destroy expire_page :controller => 'pastes', :action => 'show', :id => @paste.id @@ -88,4 +74,8 @@ end end + private + def paste_params + params.require(:paste).permit(:paste, :key) + end end