Class: UserImpersonate::ImpersonateController

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

Instance Method Summary collapse

Instance Method Details

#createObject

Perform the user impersonate action GET /impersonate/user/123



27
28
29
30
31
# File 'app/controllers/user_impersonate/impersonate_controller.rb', line 27

def create
  @user = find_user(params[:user_id])
  impersonate(@user)
  redirect_on_impersonate(@user)
end

#destroyObject

Revert the user impersonation DELETE /impersonation/revert



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/user_impersonate/impersonate_controller.rb', line 35

def destroy
  unless current_staff_user
    flash[:notice] = "You weren't impersonating anyone"
    redirect_on_revert and return
  end
  user = current_staff
  revert_impersonate
  if user
    flash[:notice] = "No longer impersonating #{user}"
    redirect_on_revert(user)
  else
    flash[:notice] = "No longer impersonating a user"
    redirect_on_revert
  end
end

#indexObject

Display list of all users, except current (staff) user Is this exclusion unnecessary complexity? Normal apps wouldn’t bother with this action; rather they would go straight to GET /impersonate/user/123 (create action)



12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/user_impersonate/impersonate_controller.rb', line 12

def index
  users_table = Arel::Table.new(user_table.to_sym) # e.g. :users
  id_column = users_table[user_id_column.to_sym]   # e.g. users_table[:id]
  @users = user_class.order("updated_at DESC").
                where(
                  id_column.not_in [
                    current_staff.send(user_id_column.to_sym) # e.g. current_user.id
                  ])
  if params[:search]
    @users = @users.where("#{user_name_column} like ?", "%#{params[:search]}%")
  end
end