Class: ApplicationController

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/application_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(req, res) ⇒ ApplicationController

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

#reqObject (readonly)

Returns the value of attribute req.



2
3
4
# File 'app/controllers/application_controller.rb', line 2

def req
  @req
end

#resObject (readonly)

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_tokenObject

CSRF Helper for views



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
  # Find all Cloudinary URLs in markdown
  urls = content.scan(/https?:\/\/res\.cloudinary\.com\/[^\/]+\/image\/upload\/[^\s\)]+/)
  urls.each { |url| delete_from_storage(url) }
  
  # Also find local uploads
  local_urls = content.scan(/\/img\/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')
    # Extract public_id from Cloudinary URL
    # Example: https://res.cloudinary.com/demo/image/upload/v12345/sample.jpg -> sample
    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?('/img/uploads/')
    path = File.join(APP_ROOT, 'public', url)
    FileUtils.rm(path) if File.exist?(path) rescue nil
  end
end

#paramsObject



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.headers['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)
  # Pass controller instance variables to the view
  instance_variables.each do |var|
    locals[var.to_s.delete('@').to_sym] ||= instance_variable_get(var)
  end
  @res.render(template, **locals)
end

#sessionObject



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

Validation Helper



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