Class: Admin::UsersController

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

Overview

This controller manage the User model for the admins

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin!

Methods inherited from ApplicationController

#access_denied!, #default_url_options, #record_not_found!, #set_turbo, #switch_locale

Instance Method Details

#editObject

GET /admin/users/:id/edit



19
20
21
# File 'app/controllers/admin/users_controller.rb', line 19

def edit
  @groups = Group.all.order(:title).pluck :title, :id
end

#filter_paramsObject (private)

Filter params for search an User



46
47
48
# File 'app/controllers/admin/users_controller.rb', line 46

def filter_params
  params.fetch(:filter, {}).permit(:text, :category)
end

#indexObject

GET /admin/users



9
10
11
12
13
14
15
16
# File 'app/controllers/admin/users_controller.rb', line 9

def index
  @categories = [ "Admin", "Editor" ]
  @search = {}
  @search[:editor] = true if filter_params[:category] == "Editor"
  @search[:admin] = true if filter_params[:category] == "Admin"
  @text = [ "email ilike :text", { text: "%#{filter_params[:text]}%" } ] if filter_params[:text].present?
  @pagy, @users = pagy(User.where(@search).where(@text))
end

#set_userObject (private)

set @user when needed



36
37
38
# File 'app/controllers/admin/users_controller.rb', line 36

def set_user
  @user = User.find(params[:id])
end

#updateObject

PATCH/PUT /admin/users/:id



24
25
26
27
28
29
30
31
# File 'app/controllers/admin/users_controller.rb', line 24

def update
  if @user.update(user_params)
    redirect_to admin_users_path, notice: "User was successfully updated."
  else
    @groups = Group.all.order(:title).pluck :title, :id
    render :edit
  end
end

#user_paramsObject (private)

Filter params for set an User



41
42
43
# File 'app/controllers/admin/users_controller.rb', line 41

def user_params
  params.require(:user).permit(:editor, :admin, group_ids: [])
end