-
Notifications
You must be signed in to change notification settings - Fork 8
/
application_controller.rb
92 lines (73 loc) · 2.14 KB
/
application_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class ApplicationController < ActionController::Base
before_filter do
if current_user && current_user.admin
Rack::MiniProfiler.authorize_request
end
end
protect_from_forgery
include Pagination
include PathHelpers
include ApplicationHelper
PARAMS_TO_PASS_ON_REDIRECT = %i(access-token)
# CanCan Authorization
rescue_from CanCan::AccessDenied do |exception|
if request.format.html?
redirect_to root_url, alert: exception.message
else
render \
status: :forbidden,
content_type: 'text/plain',
text: exception.message
end
end
if defined? PG
# A foreign key constraint exception from the database
rescue_from PG::Error do |exception|
message = exception.message
if message.include?('foreign key constraint')
logger.warn(message)
# shorten the message
message = message.match(/DETAIL: .+/).to_s
redirect_to :back,
flash: {error: "Whatever you tried to do - the server is unable to process your request because of a foreign key constraint. (#{message})" }
else
# anything else
raise exception
end
end
end
protected
def current_ability
@current_ability ||= Ability.new(current_user, params[:'access-token'])
end
def params_to_pass_on_redirect
params.select { |k, _v| PARAMS_TO_PASS_ON_REDIRECT.include?(k) }
end
def authenticate_admin!
unless admin?
flash[:error] = 'you need admin privileges for this action'
redirect_to :root
end
end
def after_sign_in_path_for(resource)
root_path
end
def after_sign_out_path_for(resource)
request.referrer
end
def display_all?
params[:all].present?
end
def paginate_for(collection)
Kaminari.paginate_array(collection).page(params[:page])
end
def locid_for(resource, *commands, **query_components)
locid = URI.escape(resource.locid)
iri = "#{request.base_url}#{locid}"
iri << "///#{commands.join('///')}" if commands.any?
iri << "?#{query_components.to_query}" if query_components.any?
iri
end
helper_method :display_all?
helper_method :locid_for
end