diff app/controllers/pastes_controller.rb @ 211:5e1d728975a4

Refactor
author Edho Arief <edho@myconan.net>
date Sun, 12 May 2013 02:41:08 +0900
parents 4e409ac7082d
children 186b4674bcbe
line wrap: on
line diff
--- 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