aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/application_controller.rb
blob: 80d441565b456d113c36c4cc1102f3c5f8022e95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

class ApplicationController < ActionController::Base
  before_action :user_authenticated!
  before_action :set_searches

  helper_method :current_user

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def user_authenticated!
    return if current_user

    redirect_to new_sessions_path
  end

  def set_searches
    @searches = Search.order(:name)
  end

  # Redirect to the search that was last used by the current user.
  def redirect_to_search!
    search_id = current_user&.last_search_id

    if search_id
      redirect_to search_path(search_id)
    else
      redirect_to root_url
    end
  end
end