Module: BarkestCore::SessionsHelper

Included in:
ApplicationControllerBase
Defined in:
app/helpers/barkest_core/sessions_helper.rb

Overview

This module adds log_in, log_out, current_user, and other session related helper methods.

Based on the tutorial from [www.railstutorial.org](www.railstutorial.org).

Instance Method Summary collapse

Instance Method Details

#current_userObject

Gets the current user.



24
25
26
27
28
29
30
31
32
33
34
# File 'app/helpers/barkest_core/sessions_helper.rb', line 24

def current_user
  if (user_id = session[:user_id])
    @current_user ||= User.find_by(id: user_id)
  elsif (user_id = cookies.signed[:user_id])
    user = User.find_by(id: user_id)
    if user && user.authenticated?(:remember, cookies[:remember_token])
       user
      @current_user = user
    end
  end
end

#current_user?(user) ⇒ Boolean

Is the specified user the current user?

Returns:

  • (Boolean)


38
39
40
# File 'app/helpers/barkest_core/sessions_helper.rb', line 38

def current_user?(user)
  user == current_user
end

#forget(user) ⇒ Object

Removes the user from the permanent cookies.



64
65
66
67
68
# File 'app/helpers/barkest_core/sessions_helper.rb', line 64

def forget(user)
  user.forget
  cookies.delete(:user_id)
  cookies.delete(:remember_token)
end

#log_in(user) ⇒ Object

Logs in the given user.



10
11
12
# File 'app/helpers/barkest_core/sessions_helper.rb', line 10

def (user)
  session[:user_id] = user.id
end

#log_outObject

Logs out any currently logged in user.



16
17
18
19
20
# File 'app/helpers/barkest_core/sessions_helper.rb', line 16

def log_out
  forget current_user
  session.delete(:user_id)
  @current_user = nil
end

#logged_in?Boolean

Is a user currently logged in?

Returns:

  • (Boolean)


50
51
52
# File 'app/helpers/barkest_core/sessions_helper.rb', line 50

def logged_in?
  !current_user.nil?
end

#redirect_back_or(default) ⇒ Object

A helper redirect method primarily used after logon to redirect to the page that requested a logon.

There may be other uses, but logon is the most likely use case.



74
75
76
77
# File 'app/helpers/barkest_core/sessions_helper.rb', line 74

def redirect_back_or(default)
  redirect_to(session[:forwarding_url] || default)
  session.delete(:forwarding_url)
end

#remember(user) ⇒ Object

Stores the user id to the permanent cookies to keep the user logged in.



56
57
58
59
60
# File 'app/helpers/barkest_core/sessions_helper.rb', line 56

def remember(user)
  user.remember
  cookies.permanent.signed[:user_id] = user.id
  cookies.permanent[:remember_token] = user.remember_token
end

#store_locationObject

Stores the current request URL to the session for use with redirect_back_or.



82
83
84
# File 'app/helpers/barkest_core/sessions_helper.rb', line 82

def store_location
  session[:forwarding_url] = request.url if request.get?
end

#store_location_and_redirect_to(url) ⇒ Object

Stores the current request URL to the session and redirects to the specified URL.



88
89
90
91
# File 'app/helpers/barkest_core/sessions_helper.rb', line 88

def store_location_and_redirect_to(url)
  store_location
  redirect_to url
end

#system_admin?Boolean

Is the current user a system administrator?

Returns:

  • (Boolean)


44
45
46
# File 'app/helpers/barkest_core/sessions_helper.rb', line 44

def system_admin?
  current_user && current_user.system_admin?
end