annotate app/controllers/pastes_controller.rb @ 316:61f7f258a6fb

Move from-gzip paste parsing to model.
author edogawaconan <me@myconan.net>
date Sat, 05 Apr 2014 23:23:25 +0900
parents 200488650a86
children 612bc7c32324
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1 class PastesController < ApplicationController
48
f649b46fca4f Cache all the things! Or just the show page.
Edho Arief <edho@myconan.net>
parents: 32
diff changeset
2 caches_page :show
73
f480cdf5f3df Misc whitespace.
Edho Arief <edho@myconan.net>
parents: 69
diff changeset
3
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
4 # GET /1
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
5 # GET /1.txt
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
6 def show
212
186b4674bcbe Add controller support for is_private flag
Edho Arief <edho@myconan.net>
parents: 211
diff changeset
7 @paste = Paste.safe_find(params[:id])
229
388504e43bcf Properly return 404 when requesting invalid paste.
Edho Arief <edho@myconan.net>
parents: 226
diff changeset
8 head :not_found and return unless @paste
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
9
77
2fb80ca710e0 Added cache control so it's cached by browser the first time loaded.
Edho Arief <edho@myconan.net>
parents: 75
diff changeset
10 expires_in 1.year, :public => true
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
11 respond_to do |format|
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
12 format.html # show.html.erb
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
13 format.txt # show.txt.erb
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
14 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
15 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
16
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
17 # GET /
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
18 def new
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
19 @paste = Paste.new
189
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
20 @paste.set_paste_key
240
16251b94eb6c Whoops, forgot to secure the "new paste based on ##"
edogawaconan <me@myconan.net>
parents: 229
diff changeset
21 @paste.paste = Paste.safe_find(params[:base]).try(:paste)
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
22
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
23 respond_to do |format|
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
24 format.html # new.html.erb
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
25 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
26 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
27
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
28 # POST /
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
29 # POST /pastes.json
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
30 # POST /pastes.txt
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
31 def create
211
5e1d728975a4 Refactor
Edho Arief <edho@myconan.net>
parents: 191
diff changeset
32 @paste = Paste.new(paste_params.merge(ip: request.remote_ip))
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
33
32
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
34 begin
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
35 respond_to do |format|
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
36 if @paste.save
181
500c09718fd7 Added fresh paste marker.
Edho Arief <edho@myconan.net>
parents: 151
diff changeset
37 @fresh = true
32
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
38 format.html { redirect_to @paste, :notice => 'Paste was successfully created.' }
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
39 format.json { render :json => @paste, :status => :created, :location => @paste }
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
40 else
283
200488650a86 flash.now !!
edogawaconan <me@myconan.net>
parents: 265
diff changeset
41 flash.now[:alert] = @paste.errors.full_messages.to_sentence
32
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
42 format.html { render :action => "new" }
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
43 format.json { render :json => @paste.errors, :status => :unprocessable_entity }
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
44 end
67
f7b7a39fb48c Allow post in txt format pt. 2 (view and controller).
Edho Arief <edho@myconan.net>
parents: 53
diff changeset
45 format.txt
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
46 end
32
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
47 rescue ActiveRecord::RecordNotUnique
183
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
48 @paste = Paste.where(:ip => @paste.ip, :paste_hash => @paste.paste_hash).first
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
49 respond_to do |format|
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
50 format.html { redirect_to paste_path(@paste) }
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
51 format.json { render :json => @paste }
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
52 format.txt
fc234f8cf3d9 Return on duplicate based on post format.
Edho Arief <edho@myconan.net>
parents: 181
diff changeset
53 end
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
54 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
55 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
56
189
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
57 def destroy
212
186b4674bcbe Add controller support for is_private flag
Edho Arief <edho@myconan.net>
parents: 211
diff changeset
58 @paste = Paste.safe_find(params[:id])
265
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 240
diff changeset
59 if @paste.safe_destroy(params[:paste][:key])
191
4e409ac7082d Also expires the page upon deletion.
Edho Arief <edho@myconan.net>
parents: 190
diff changeset
60 expire_page :controller => 'pastes', :action => 'show', :id => @paste.id
265
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 240
diff changeset
61 redirect_to root_path, :notice => "Paste ##{params[:id]} deleted"
189
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
62 else
283
200488650a86 flash.now !!
edogawaconan <me@myconan.net>
parents: 265
diff changeset
63 flash.now[:alert] = @paste.errors.full_messages.to_sentence
265
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 240
diff changeset
64 render :show
189
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
65 end
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
66 end
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
67
211
5e1d728975a4 Refactor
Edho Arief <edho@myconan.net>
parents: 191
diff changeset
68 private
5e1d728975a4 Refactor
Edho Arief <edho@myconan.net>
parents: 191
diff changeset
69 def paste_params
316
61f7f258a6fb Move from-gzip paste parsing to model.
edogawaconan <me@myconan.net>
parents: 283
diff changeset
70 params.require(:paste).permit(:paste, :paste_gzip, :paste_gzip_base64, :is_private, :key)
211
5e1d728975a4 Refactor
Edho Arief <edho@myconan.net>
parents: 191
diff changeset
71 end
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
72 end