Class: Guts::UsersController

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

Overview

Users controller

Instance Method Summary collapse

Instance Method Details

#createObject

Note:

Redirects to #index if successfull or re-renders #new if not

Creates a user through post



37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/guts/users_controller.rb', line 37

def create
  @user = User.new user_params
  authorize @user

  if @user.save
    flash[:notice] = 'User was successfully created. Don\'t forget to add permissions.'
    redirect_to edit_user_path(@user)
  else
    render :new
  end
end

#destroyObject

Note:

Redirects to #index on success

Destroys a single user



64
65
66
67
68
69
70
# File 'app/controllers/guts/users_controller.rb', line 64

def destroy
  authorize @user
  @user.destroy

  flash[:notice] = 'User was successfully destroyed.'
  redirect_to users_url
end

#editObject

Editing of a user



31
32
33
# File 'app/controllers/guts/users_controller.rb', line 31

def edit
  authorize @user
end

#indexObject

Note:

Filterable by group by passing ‘group` param

Displays a list of users



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

def index
  if params[:group]
    @group = policy_scope(Group).find params[:group]
    @users = policy_scope(User).in_group(@group)
  else
    @users = policy_scope(User).all
  end
end

#newObject

Creation of a new user



25
26
27
28
# File 'app/controllers/guts/users_controller.rb', line 25

def new
  @user = User.new
  authorize @user
end

#showObject

Show details about a single user



20
21
22
# File 'app/controllers/guts/users_controller.rb', line 20

def show
  authorize @user
end

#switch_userObject

Allows switching of users by passing ‘user_id` in params

See Also:

  • SessionsConcern#log_in


74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/guts/users_controller.rb', line 74

def switch_user
  authorize User, :switch_user?

  if request.post?
    user = User.find(params[:user_id])
     user
    flash.now[:notice] = "You are now logged in as #{user.name}."
  end

  @users = policy_scope(User).all
end

#updateObject

Note:

Redirects to #index if successfull or re-renders #edit if not

Updates a user through patch



51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/guts/users_controller.rb', line 51

def update
  authorize @user

  if @user.update(user_params)
    flash[:notice] = 'User was successfully updated.'
    redirect_to edit_user_path(@user)
  else
    render :edit
  end
end