annotate app/controllers/pastes_controller.rb @ 48:f649b46fca4f

Cache all the things! Or just the show page.
author Edho Arief <edho@myconan.net>
date Thu, 04 Oct 2012 03:05:43 +0700
parents 59ef6698fa0d
children c09020fc73f5
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
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
3 # GET /pastes/1
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
4 # GET /pastes/1.json
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
5 def show
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
6 @paste = Paste.find(params[:id])
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
7
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
8 respond_to do |format|
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
9 format.html # show.html.erb
7
3a1f6c2e195d Added raw paste view.
Edho Arief <edho@myconan.net>
parents: 4
diff changeset
10 format.txt # show.html.erb
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
11 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
12 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
13
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
14 # GET /pastes/new
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
15 # GET /pastes/new.json
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
16 def new
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
17 @paste = Paste.new
10
56c17679105b Moar pastes.
Edho Arief <edho@myconan.net>
parents: 7
diff changeset
18 begin
56c17679105b Moar pastes.
Edho Arief <edho@myconan.net>
parents: 7
diff changeset
19 @paste.paste = Paste.find(params[:base]).paste
56c17679105b Moar pastes.
Edho Arief <edho@myconan.net>
parents: 7
diff changeset
20 rescue
56c17679105b Moar pastes.
Edho Arief <edho@myconan.net>
parents: 7
diff changeset
21 nil
56c17679105b Moar pastes.
Edho Arief <edho@myconan.net>
parents: 7
diff changeset
22 end
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
23
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
24 respond_to do |format|
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
25 format.html # new.html.erb
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
26 format.json { render :json => @paste }
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
27 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
28 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
29
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
30 # POST /pastes
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
31 # POST /pastes.json
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
32 def create
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
33 @paste = Paste.new
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
34 @paste.paste = params[:paste][:paste]
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
35 @paste.ip = request.remote_ip
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
36
32
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
37 begin
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
38 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
39 if @paste.save
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
40 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
41 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
42 else
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
43 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
44 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
45 end
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
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
48 redirect_to paste_path(Paste.where(:ip => @paste.ip, :paste_hash => @paste.paste_hash).first)
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
49 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
50 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
51
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
52 end