Commit c5cbbea82e5f1ce07d5928e409fe78a80b1b7094

Authored by Andrey Kumanyaev
1 parent ef85202f

rewrite admin users controller (use 1 variable and find by username)

Showing 1 changed file with 20 additions and 24 deletions   Show diff stats
app/controllers/admin/users_controller.rb
@@ -7,25 +7,21 @@ class Admin::UsersController < Admin::ApplicationController @@ -7,25 +7,21 @@ class Admin::UsersController < Admin::ApplicationController
7 end 7 end
8 8
9 def show 9 def show
10 - @admin_user = User.find(params[:id])  
11 -  
12 - @projects = if @admin_user.authorized_projects.empty? 10 + projects = if admin_user.authorized_projects.empty?
13 Project 11 Project
14 else 12 else
15 - Project.without_user(@admin_user) 13 + Project.without_user(admin_user)
16 end.all 14 end.all
17 end 15 end
18 16
19 def team_update 17 def team_update
20 - @admin_user = User.find(params[:id])  
21 -  
22 UsersProject.add_users_into_projects( 18 UsersProject.add_users_into_projects(
23 params[:project_ids], 19 params[:project_ids],
24 - [@admin_user.id], 20 + [admin_user.id],
25 params[:project_access] 21 params[:project_access]
26 ) 22 )
27 23
28 - redirect_to [:admin, @admin_user], notice: 'Teams were successfully updated.' 24 + redirect_to [:admin, admin_user], notice: 'Teams were successfully updated.'
29 end 25 end
30 26
31 27
@@ -34,13 +30,11 @@ class Admin::UsersController < Admin::ApplicationController @@ -34,13 +30,11 @@ class Admin::UsersController < Admin::ApplicationController
34 end 30 end
35 31
36 def edit 32 def edit
37 - @admin_user = User.find(params[:id]) 33 + admin_user
38 end 34 end
39 35
40 def block 36 def block
41 - @admin_user = User.find(params[:id])  
42 -  
43 - if @admin_user.block 37 + if admin_user.block
44 redirect_to :back, alert: "Successfully blocked" 38 redirect_to :back, alert: "Successfully blocked"
45 else 39 else
46 redirect_to :back, alert: "Error occured. User was not blocked" 40 redirect_to :back, alert: "Error occured. User was not blocked"
@@ -48,9 +42,7 @@ class Admin::UsersController < Admin::ApplicationController @@ -48,9 +42,7 @@ class Admin::UsersController < Admin::ApplicationController
48 end 42 end
49 43
50 def unblock 44 def unblock
51 - @admin_user = User.find(params[:id])  
52 -  
53 - if @admin_user.update_attribute(:blocked, false) 45 + if admin_user.update_attribute(:blocked, false)
54 redirect_to :back, alert: "Successfully unblocked" 46 redirect_to :back, alert: "Successfully unblocked"
55 else 47 else
56 redirect_to :back, alert: "Error occured. User was not unblocked" 48 redirect_to :back, alert: "Error occured. User was not unblocked"
@@ -82,30 +74,34 @@ class Admin::UsersController < Admin::ApplicationController @@ -82,30 +74,34 @@ class Admin::UsersController < Admin::ApplicationController
82 params[:user].delete(:password_confirmation) 74 params[:user].delete(:password_confirmation)
83 end 75 end
84 76
85 - @admin_user = User.find(params[:id])  
86 - @admin_user.admin = (admin && admin.to_i > 0) 77 + admin_user.admin = (admin && admin.to_i > 0)
87 78
88 respond_to do |format| 79 respond_to do |format|
89 - if @admin_user.update_attributes(params[:user], as: :admin)  
90 - format.html { redirect_to [:admin, @admin_user], notice: 'User was successfully updated.' } 80 + if admin_user.update_attributes(params[:user], as: :admin)
  81 + format.html { redirect_to [:admin, admin_user], notice: 'User was successfully updated.' }
91 format.json { head :ok } 82 format.json { head :ok }
92 else 83 else
93 format.html { render action: "edit" } 84 format.html { render action: "edit" }
94 - format.json { render json: @admin_user.errors, status: :unprocessable_entity } 85 + format.json { render json: admin_user.errors, status: :unprocessable_entity }
95 end 86 end
96 end 87 end
97 end 88 end
98 89
99 def destroy 90 def destroy
100 - @admin_user = User.find(params[:id])  
101 - if @admin_user.personal_projects.count > 0 91 + if admin_user.personal_projects.count > 0
102 redirect_to admin_users_path, alert: "User is a project owner and can't be removed." and return 92 redirect_to admin_users_path, alert: "User is a project owner and can't be removed." and return
103 end 93 end
104 - @admin_user.destroy 94 + admin_user.destroy
105 95
106 respond_to do |format| 96 respond_to do |format|
107 - format.html { redirect_to admin_users_url } 97 + format.html { redirect_to admin_users_path }
108 format.json { head :ok } 98 format.json { head :ok }
109 end 99 end
110 end 100 end
  101 +
  102 + protected
  103 +
  104 + def admin_user
  105 + @admin_user ||= User.find_by_username(params[:id])
  106 + end
111 end 107 end