annotate app/controllers/pastes_controller.rb @ 66:e0817b84c01b

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