Class: ApplicationController
- Inherits:
-
Object
- Object
- ApplicationController
- Defined in:
- app/controllers/application_controller.rb
Direct Known Subclasses
ActivityLogsController, ApiController, AuthController, CartController, DashboardController, PagesController, PostsController, ProductsController, ProjectsController
Instance Attribute Summary collapse
-
#req ⇒ Object
readonly
Returns the value of attribute req.
-
#res ⇒ Object
readonly
Returns the value of attribute res.
Instance Method Summary collapse
-
#csrf_token ⇒ Object
CSRF Helper for views.
-
#initialize(req, res) ⇒ ApplicationController
constructor
A new instance of ApplicationController.
-
#log_activity(action, target = nil, details = nil) ⇒ Object
Activity Logger.
- #params ⇒ Object
- #redirect_to(path) ⇒ Object
- #render(template, **locals) ⇒ Object
- #render_json(data, status: 200) ⇒ Object
- #session ⇒ Object
-
#validate!(required_params) ⇒ Object
Validation Helper.
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
#req ⇒ Object (readonly)
Returns the value of attribute req.
2 3 4 |
# File 'app/controllers/application_controller.rb', line 2 def req @req end |
#res ⇒ Object (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
#csrf_token ⇒ Object
CSRF Helper for views
45 46 47 |
# File 'app/controllers/application_controller.rb', line 45 def csrf_token @req.env['eks_cent.csrf_token'] end |
#log_activity(action, target = nil, details = nil) ⇒ Object
Activity Logger
50 51 52 53 54 |
# File 'app/controllers/application_controller.rb', line 50 def log_activity(action, target = nil, details = nil) user_id = session['user_id'] || session[:user_id] return unless user_id ActivityLog.log(user_id, action, target, details) 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.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 |
#render_json(data, status: 200) ⇒ Object
30 31 32 |
# File 'app/controllers/application_controller.rb', line 30 def render_json(data, status: 200) @res.json(data, status: status) 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
Validation Helper
35 36 37 38 39 40 41 42 |
# File 'app/controllers/application_controller.rb', line 35 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 |