Class: Admin::UsersController

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

Overview

rubocop:todo Style/Documentation

Instance Method Summary collapse

Instance Method Details

#editObject


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

def edit
  @all_roles = Role.keys
  @users_roles = @user.study_and_project_roles.order(name: :asc)
  @studies = Study.order(:id)
  @projects = Project.order(:id)

  respond_to do |format|
    format.js
    format.html
  end
end

#filterObject


105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/admin/users_controller.rb', line 105

def filter
  if params[:q]
    @users =
      User
        .order(:login)
        .where(
          'first_name LIKE :query OR last_name LIKE :query OR login LIKE :query',
          query: "%#{params[:q].downcase}%"
        )
  end

  render partial: 'users', locals: { users: @users }
end

#grant_user_roleObject

rubocop:todo Metrics/MethodLength


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/admin/users_controller.rb', line 51

def grant_user_role # rubocop:todo Metrics/AbcSize
  if request.xhr?
    if params[:role]
      authorizable_object =
        if params[:role][:authorizable_type] == 'Project'
          Project.find(params[:role][:authorizable_id])
        else
          Study.find(params[:role][:authorizable_id])
        end
      @user.grant_role(params[:role][:authorizable_name].to_s, authorizable_object)
      @users_roles = @user.study_and_project_roles.order(name: :asc)

      flash[:notice] = 'Role added'
      render partial: 'roles', status: 200
    else
      @users_roles = @user.study_and_project_roles.order(name: :asc)
      flash[:error] = 'A problem occurred while adding the role'
      render partial: 'roles', status: 500
    end
  else
    @users_roles = @user.study_and_project_roles.sort_by(&:name)
    flash[:error] = 'A problem occurred while adding the role'
    render partial: 'roles', status: 401
  end
end

#indexObject


9
10
11
# File 'app/controllers/admin/users_controller.rb', line 9

def index
  @users = User.order(:login)
end

#remove_user_roleObject

rubocop:enable Metrics/MethodLength


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/admin/users_controller.rb', line 79

def remove_user_role # rubocop:todo Metrics/AbcSize
  if request.xhr?
    if params[:role]
      authorizable_object =
        if params[:role][:authorizable_type] == 'project'
          Project.find(params[:role][:authorizable_id])
        else
          Study.find(params[:role][:authorizable_id])
        end
      @user.remove_role(params[:role][:authorizable_name].to_s, authorizable_object)
      @users_roles = @user.study_and_project_roles.order(name: :asc)

      flash[:error] = 'Role was removed'
      render partial: 'roles', status: 200
    else
      @users_roles = @user.study_and_project_roles.order(name: :asc)
      flash[:error] = 'A problem occurred while removing the role'
      render partial: 'roles', status: 500
    end
  else
    @users_roles = @user.study_and_project_roles.order(name: :asc)
    flash[:error] = 'A problem occurred while removing the role'
    render partial: 'roles', status: 401
  end
end

#showObject


25
# File 'app/controllers/admin/users_controller.rb', line 25

def show; end

#switchObject


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

def switch
  session[:user] = params[:id]
  redirect_to studies_url
end

#updateObject

rubocop:todo Metrics/MethodLength


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/admin/users_controller.rb', line 33

def update # rubocop:todo Metrics/AbcSize
  @user = User.find(params[:id])
  Role.general_roles.each do |role|
    params[:role] && params[:role][role.name] ? @user.grant_role(role.name) : @user.remove_role(role.name)
  end

  @user.update(params[:user]) if @user.id == params[:id].to_i
  if @user.save
    flash[:notice] = 'Profile updated'
  else
    flash[:error] = 'Problem updating profile'
  end
  redirect_to profile_path(@user)
end