annotate app/controllers/pastes_controller.rb @ 373:6e3e1e7b0212

Handle unique error in model instead of controller.
author nanaya <me@myconan.net>
date Sat, 06 Jun 2015 22:12:56 +0900
parents 26545fe719ca
children e84276bf344a
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
320
612bc7c32324 Use memcached for caching.
edogawaconan <me@myconan.net>
parents: 316
diff changeset
2 before_action :lowercase_path, :only => :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])
329
430dadffd91e Fix coding style.
edogawaconan <me@myconan.net>
parents: 321
diff changeset
8 return head :not_found unless @paste
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
9
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
10 respond_to do |format|
366
26545fe719ca Remove caching system.
edogawaconan <me@myconan.net>
parents: 356
diff changeset
11 format.html
26545fe719ca Remove caching system.
edogawaconan <me@myconan.net>
parents: 356
diff changeset
12 format.txt
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
13 end
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
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
16 # GET /
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
17 def new
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
18 @paste = Paste.new
189
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
19 @paste.set_paste_key
240
16251b94eb6c Whoops, forgot to secure the "new paste based on ##"
edogawaconan <me@myconan.net>
parents: 229
diff changeset
20 @paste.paste = Paste.safe_find(params[:base]).try(:paste)
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
21
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
22 respond_to do |format|
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
23 format.html # new.html.erb
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
24 end
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
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
27 # POST /
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
28 # POST /pastes.json
74
48598fc65c20 Fixed comments on pastes_controller.
Edho Arief <edho@myconan.net>
parents: 73
diff changeset
29 # POST /pastes.txt
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
30 def create
373
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
31 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
32
373
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
33 respond_to do |format|
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
34 if created
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
35 if @fresh
329
430dadffd91e Fix coding style.
edogawaconan <me@myconan.net>
parents: 321
diff changeset
36 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
37 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
38 else
373
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
39 format.html { redirect_to paste_path(@paste) }
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
40 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
41 end
373
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
42 else
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
43 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
44 format.html { render :action => "new" }
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
45 format.json { render :json => @paste.errors, :status => :unprocessable_entity }
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
46 end
373
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 366
diff changeset
47 format.txt
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
48 end
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
189
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
51 def destroy
212
186b4674bcbe Add controller support for is_private flag
Edho Arief <edho@myconan.net>
parents: 211
diff changeset
52 @paste = Paste.safe_find(params[:id])
265
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 240
diff changeset
53 if @paste.safe_destroy(params[:paste][:key])
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 240
diff changeset
54 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
55 else
283
200488650a86 flash.now !!
edogawaconan <me@myconan.net>
parents: 265
diff changeset
56 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
57 render :show
189
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
58 end
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
59 end
b4b7a29b70f6 Initial work for paste deletion.
Edho Arief <edho@myconan.net>
parents: 183
diff changeset
60
211
5e1d728975a4 Refactor
Edho Arief <edho@myconan.net>
parents: 191
diff changeset
61 private
320
612bc7c32324 Use memcached for caching.
edogawaconan <me@myconan.net>
parents: 316
diff changeset
62
612bc7c32324 Use memcached for caching.
edogawaconan <me@myconan.net>
parents: 316
diff changeset
63 def lowercase_path
612bc7c32324 Use memcached for caching.
edogawaconan <me@myconan.net>
parents: 316
diff changeset
64 correct_path = request.fullpath.downcase
329
430dadffd91e Fix coding style.
edogawaconan <me@myconan.net>
parents: 321
diff changeset
65 unless correct_path == request.fullpath
430dadffd91e Fix coding style.
edogawaconan <me@myconan.net>
parents: 321
diff changeset
66 redirect_to correct_path, :status => :moved_permanently
430dadffd91e Fix coding style.
edogawaconan <me@myconan.net>
parents: 321
diff changeset
67 end
320
612bc7c32324 Use memcached for caching.
edogawaconan <me@myconan.net>
parents: 316
diff changeset
68 end
612bc7c32324 Use memcached for caching.
edogawaconan <me@myconan.net>
parents: 316
diff changeset
69
211
5e1d728975a4 Refactor
Edho Arief <edho@myconan.net>
parents: 191
diff changeset
70 def paste_params
316
61f7f258a6fb Move from-gzip paste parsing to model.
edogawaconan <me@myconan.net>
parents: 283
diff changeset
71 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
72 end
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
73 end