Class: ApplicationController
- Inherits:
-
Object
- Object
- ApplicationController
show all
- Defined in:
- app/controllers/application_controller.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of ApplicationController.
4
5
6
7
|
# File 'app/controllers/application_controller.rb', line 4
def initialize(req, res)
@req = req
@res = res
end
|
Instance Attribute Details
#req ⇒ Object
Returns the value of attribute req.
2
3
4
|
# File 'app/controllers/application_controller.rb', line 2
def req
@req
end
|
#res ⇒ Object
Returns the value of attribute res.
2
3
4
|
# File 'app/controllers/application_controller.rb', line 2
def res
@res
end
|
Instance Method Details
#boolean_param(val) ⇒ Object
45
46
47
|
# File 'app/controllers/application_controller.rb', line 45
def boolean_param(val)
['1', 'true', 'on'].include?(val.to_s)
end
|
#csrf_token ⇒ Object
41
42
43
|
# File 'app/controllers/application_controller.rb', line 41
def csrf_token
@req.env['eks_cent.csrf_token']
end
|
#delete_all_images_from_content(content) ⇒ Object
67
68
69
70
71
72
73
74
75
76
|
# File 'app/controllers/application_controller.rb', line 67
def delete_all_images_from_content(content)
return unless content
urls = content.scan(/https?:\/\/res\.cloudinary\.com\/[^\/]+\/image\/upload\/[^\s\)]+/)
urls.each { |url| delete_from_storage(url) }
local_urls = content.scan(/\/images\/uploads\/[^\s\)]+/)
local_urls.each { |url| delete_from_storage(url) }
end
|
#delete_from_storage(url) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'app/controllers/application_controller.rb', line 49
def delete_from_storage(url)
return unless url && !url.empty?
if url.include?('cloudinary.com')
public_id = url.split('/').last.split('.').first
begin
require 'cloudinary'
Cloudinary::Uploader.destroy(public_id)
rescue => e
puts "⚠ Error deleting from Cloudinary: #{e.message}"
end
elsif url.start_with?('/images/uploads/')
path = File.join(APP_ROOT, 'public', url)
FileUtils.rm(path) if File.exist?(path) rescue nil
end
end
|
#params ⇒ Object
9
10
11
|
# File 'app/controllers/application_controller.rb', line 9
def params
@req.params
end
|
#redirect_to(path) ⇒ Object
25
26
27
28
|
# File 'app/controllers/application_controller.rb', line 25
def redirect_to(path)
@res.status = 302
@res.['Location'] = path
end
|
#render(template, **locals) ⇒ Object
17
18
19
20
21
22
23
|
# File 'app/controllers/application_controller.rb', line 17
def render(template, **locals)
instance_variables.each do |var|
locals[var.to_s.delete('@').to_sym] ||= instance_variable_get(var)
end
@res.render(template, **locals)
end
|
#session ⇒ Object
13
14
15
|
# File 'app/controllers/application_controller.rb', line 13
def session
@req.env['eks_cent.session'] ||= @req.env['rack.session'] || {}
end
|
#validate!(required_params) ⇒ Object
31
32
33
34
35
36
37
38
|
# File 'app/controllers/application_controller.rb', line 31
def validate!(required_params)
missing = required_params.select { |p| params[p.to_s].nil? || params[p.to_s].empty? }
unless missing.empty?
@res.status = 400
render 'error', layout: false, message: "Missing required parameters: #{missing.join(', ')}"
throw(:halt)
end
end
|