- Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathapplication_controller.rb
98 lines (82 loc) · 2.43 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
93
94
95
96
97
98
classApplicationController < ActionController::Base
# include ReactOnRails::Controller
includeClearance::Controller
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgerywith: :exception
before_action:strip_and_redirect_on_www
after_action:record_user_access
protected
defadmin?
signed_in? && current_user.admin?
end
helper_method:admin?
defdeny_access(flash_message=nil)
respond_todo |format|
format.json{renderjson: {type: :unauthorized},status: 401}
format.any(:js){head:unauthorized}
format.any{redirect_request(flash_message)}
end
end
defdom_id(klass,id)
[ActionView::RecordIdentifier.dom_class(klass),id].join('_')
end
defrecord_user_access
ifsigned_in?
current_user.update_columns(last_request_at: Time.now,last_ip: remote_ip)
end
end
defdefault_store_data
{
currentUser: {item: serialize(current_user)}
}
end
defstore_data(props=nil)
@store_data ||= default_store_data
return@store_dataifprops.nil?
@store_data.merge!(props)
end
helper_method:store_data
defstrip_and_redirect_on_www
ifRails.env.production?
ifrequest.env['HTTP_HOST'] != ENV['DOMAIN']
redirect_torequest.url.sub("//www.","//"),status: 301
end
end
end
defredirect_to_back_or_default(default=root_url)
ifrequest.env["HTTP_REFERER"].present?andrequest.env["HTTP_REFERER"] != request.env["REQUEST_URI"]
redirect_to:back
else
redirect_todefault
end
end
defbackground
Thread.newdo
yield
ActiveRecord::Base.connection.close
end
end
defserialize(obj,serializer=nil)
serializer ||= ActiveModel::Serializer.serializer_for(obj)
serializer.new(obj,root: false,scope: current_user).as_jsonifobj
end
defremote_ip
(request.env['HTTP_X_FORWARDED_FOR'] || request.remote_ip).split(",").first
end
defcaptcha_valid_user?(response,remoteip)
returntrueif !ENV['CAPTCHA_SECRET']
resp=Faraday.post(
"https://www.google.com/recaptcha/api/siteverify",
secret: ENV['CAPTCHA_SECRET'],
response: response,
remoteip: remoteip
)
logger.inforesp.body
JSON.parse(resp.body)['success']
end
defseo_protip_path(protip)
slug_protips_path(id: protip.public_id,slug: protip.slug)
end
helper_method:seo_protip_path
end