Class: UsersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/users_controller.rb

Overview

app/controllers/users_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



51
52
# File 'app/controllers/users_controller.rb', line 51

def create
end

#destroyObject



67
68
69
70
71
# File 'app/controllers/users_controller.rb', line 67

def destroy
  @current_user.destroy!
  reset_session
  redirect_to welcome_path, notice: "Account successfully deleted"
end

#editObject



59
60
# File 'app/controllers/users_controller.rb', line 59

def edit
end

#indexObject



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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/users_controller.rb', line 5

def index
  @all_roles = Role.all
  @all_games = Game.where.not(id: -1).pluck(:name)

  # Base query to fetch users
  if params[:search].present?
    search_term = params[:search].downcase
    @users = User.left_joins(roles: :game)
                 .where("LOWER(users.first_name) LIKE :search
                         OR LOWER(users.last_name) LIKE :search
                         OR LOWER(roles.role) LIKE :search
                         OR LOWER(games.name) LIKE :search", search: "%#{search_term}%")
                 .distinct
  else
    @users = User.all
  end

  # Collect user IDs based on filters
  filtered_user_ids = Set.new

  # Apply role filters if provided
  if params[:roles].present?
    role_user_ids = Role.where(role: params[:roles]).pluck(:user_id)
    filtered_user_ids.merge(role_user_ids)
  end

  # Apply game filters if provided
  if params[:games].present?
    params[:games].each do |role, games|
      game_user_ids = Role.joins(:game)
                          .where(role: role, games: { name: games })
                          .pluck(:user_id)
      filtered_user_ids.merge(game_user_ids)
    end
  end

  # Retrieve users matching the collected user IDs
  if filtered_user_ids.any?
    @users = User.where(id: filtered_user_ids)
  elsif params[:roles].present? || params[:games].present?
    # If filters yield no results, display all users and show a warning
    flash[:alert] = "No users match the selected filters. Displaying all users instead."
    @users = User.all
  end
end

#showObject



54
55
56
57
# File 'app/controllers/users_controller.rb', line 54

def show
  @settings = Settings.find_by(user_id: session[:user_id])
  @roles = Role.where(user_id: session[:user_id])
end

#updateObject



62
63
64
65
# File 'app/controllers/users_controller.rb', line 62

def update
  @current_user.update!(user_params)
  redirect_to user_path(@current_user), notice: "Account was successfully updated."
end