Class: ApplicationController

Inherits:
ControllerBase show all
Defined in:
lib/scaffold/app/controllers/application_controller.rb

Constant Summary

Constants included from ControllerCallbacks

ControllerCallbacks::METHODS

Instance Attribute Summary

Attributes inherited from ControllerBase

#params, #req, #res

Instance Method Summary collapse

Methods inherited from ControllerBase

#form_authenticity_token, #initialize, #invoke_action, #link_to, protect_from_forgery, #root_url

Methods included from ControllerCallbacks

#before_action

Constructor Details

This class inherits a constructor from ControllerBase

Instance Method Details

#current_userObject



5
6
7
# File 'lib/scaffold/app/controllers/application_controller.rb', line 5

def current_user
  @current_user ||= User.find_by(session_token: session[:session_token])
end

#ensure_loginObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/scaffold/app/controllers/application_controller.rb', line 9

def 
  unless logged_in?
    flash[:errors] = ['must be logged in']
    redirect_to new_sessions_url
    
    true
  else
    false
  end
end

#ensure_logoutObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/scaffold/app/controllers/application_controller.rb', line 20

def ensure_logout
  if logged_in?
    flash[:errors] = ['already logged in']
    redirect_to bands_url

    true
  else
    false
  end
end

#logged_in?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/scaffold/app/controllers/application_controller.rb', line 42

def logged_in?
  !!current_user
end

#login(user) ⇒ Object



31
32
33
34
# File 'lib/scaffold/app/controllers/application_controller.rb', line 31

def (user)
  @current_user = user
  session[:session_token] = user.reset_token!
end

#logoutObject



36
37
38
39
40
# File 'lib/scaffold/app/controllers/application_controller.rb', line 36

def logout
  current_user.reset_token!
  session[:session_token] = nil
  @current_user = nil
end