Class: Admin::UsersController

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

Instance Method Summary collapse

Instance Method Details

#editObject



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

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



99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/admin/users_controller.rb', line 99

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/AbcSize, Metrics/MethodLength



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

def grant_user_role # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  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:todo Metrics/AbcSize, Metrics/MethodLength



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/admin/users_controller.rb', line 73

def remove_user_role # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  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



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

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/AbcSize, Metrics/MethodLength



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

def update # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  @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