annotate app/controllers/pastes_controller.rb @ 494:e508d3226214 default tip

Update gems
author nanaya <me@nanaya.net>
date Sun, 29 Oct 2023 23:24:13 +0900
parents f11862e58af4
children
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
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
2 # GET /1
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
3 # GET /1.txt
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
4 def show
212
186b4674bcbe Add controller support for is_private flag
Edho Arief <edho@myconan.net>
parents: 211
diff changeset
5 @paste = Paste.safe_find(params[:id])
329
430dadffd91e Fix coding style.
edogawaconan <me@myconan.net>
parents: 321
diff changeset
6 return head :not_found unless @paste
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
7
464
f11862e58af4 Canonicalize 0-prefixed ids as well
nanaya <me@nanaya.pro>
parents: 417
diff changeset
8 if params[:id] != @paste.to_param
f11862e58af4 Canonicalize 0-prefixed ids as well
nanaya <me@nanaya.pro>
parents: 417
diff changeset
9 redirect_to :action => :show,
f11862e58af4 Canonicalize 0-prefixed ids as well
nanaya <me@nanaya.pro>
parents: 417
diff changeset
10 :id => @paste.to_param,
f11862e58af4 Canonicalize 0-prefixed ids as well
nanaya <me@nanaya.pro>
parents: 417
diff changeset
11 :format => params[:format]
f11862e58af4 Canonicalize 0-prefixed ids as well
nanaya <me@nanaya.pro>
parents: 417
diff changeset
12 return
f11862e58af4 Canonicalize 0-prefixed ids as well
nanaya <me@nanaya.pro>
parents: 417
diff changeset
13 end
f11862e58af4 Canonicalize 0-prefixed ids as well
nanaya <me@nanaya.pro>
parents: 417
diff changeset
14
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
15 respond_to do |format|
366
26545fe719ca Remove caching system.
edogawaconan <me@myconan.net>
parents: 356
diff changeset
16 format.html
26545fe719ca Remove caching system.
edogawaconan <me@myconan.net>
parents: 356
diff changeset
17 format.txt
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
18 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
19 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
20
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
21 # GET /
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
22 def new
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
23 @paste = Paste.new
189
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
24 @paste.set_paste_key
240
16251b94eb6c Whoops, forgot to secure the "new paste based on ##"
edogawaconan <me@myconan.net>
parents: 229
diff changeset
25 @paste.paste = Paste.safe_find(params[:base]).try(:paste)
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
26
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
27 respond_to do |format|
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
28 format.html # new.html.erb
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
29 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
30 end
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
31
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
32 # POST /
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
33 # POST /pastes.json
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
34 # POST /pastes.txt
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
35 def create
373
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
36 created, @paste, @fresh = Paste.graceful_create paste_params.merge(:ip => request.remote_ip)
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
37
373
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
38 respond_to do |format|
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
39 if created
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
40 if @fresh
329
430dadffd91e Fix coding style.
edogawaconan <me@myconan.net>
parents: 321
diff changeset
41 format.html { redirect_to @paste, :notice => "Paste was successfully created." }
32
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
42 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
43 else
373
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
44 format.html { redirect_to paste_path(@paste) }
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
45 format.json { render :json => @paste }
32
59ef6698fa0d Rescue from not unique error by redirecting to previously pasted paste.
Edho Arief <edho@myconan.net>
parents: 12
diff changeset
46 end
373
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
47 else
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
48 flash.now[:alert] = @paste.errors.full_messages.to_sentence
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
49 format.html { render :action => "new" }
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
50 format.json { render :json => @paste.errors, :status => :unprocessable_entity }
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
51 end
373
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
52 format.txt
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
189
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
56 def destroy
212
186b4674bcbe Add controller support for is_private flag
Edho Arief <edho@myconan.net>
parents: 211
diff changeset
57 @paste = Paste.safe_find(params[:id])
265
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 240
diff changeset
58 if @paste.safe_destroy(params[:paste][:key])
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 240
diff changeset
59 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
60 else
283
200488650a86 flash.now !!
edogawaconan <me@myconan.net>
parents: 265
diff changeset
61 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
62 render :show
189
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
63 end
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
64 end
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
65
211
5e1d728975a4 Refactor
Edho Arief <edho@myconan.net>
parents: 191
diff changeset
66 private
320
612bc7c32324 Use memcached for caching.
edogawaconan <me@myconan.net>
parents: 316
diff changeset
67
211
5e1d728975a4 Refactor
Edho Arief <edho@myconan.net>
parents: 191
diff changeset
68 def paste_params
417
080dd141898c Add support for explicit highlighter languages
nanaya <me@myconan.net>
parents: 378
diff changeset
69 params.require(:paste).permit(:paste, :paste_gzip, :paste_gzip_base64, :is_private, :key, :language)
211
5e1d728975a4 Refactor
Edho Arief <edho@myconan.net>
parents: 191
diff changeset
70 end
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
71 end