Commit c5cbbea82e5f1ce07d5928e409fe78a80b1b7094
1 parent
ef85202f
Exists in
master
and in
4 other branches
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 | 7 | end |
| 8 | 8 | |
| 9 | 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 | 11 | Project |
| 14 | 12 | else |
| 15 | - Project.without_user(@admin_user) | |
| 13 | + Project.without_user(admin_user) | |
| 16 | 14 | end.all |
| 17 | 15 | end |
| 18 | 16 | |
| 19 | 17 | def team_update |
| 20 | - @admin_user = User.find(params[:id]) | |
| 21 | - | |
| 22 | 18 | UsersProject.add_users_into_projects( |
| 23 | 19 | params[:project_ids], |
| 24 | - [@admin_user.id], | |
| 20 | + [admin_user.id], | |
| 25 | 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 | 25 | end |
| 30 | 26 | |
| 31 | 27 | |
| ... | ... | @@ -34,13 +30,11 @@ class Admin::UsersController < Admin::ApplicationController |
| 34 | 30 | end |
| 35 | 31 | |
| 36 | 32 | def edit |
| 37 | - @admin_user = User.find(params[:id]) | |
| 33 | + admin_user | |
| 38 | 34 | end |
| 39 | 35 | |
| 40 | 36 | def block |
| 41 | - @admin_user = User.find(params[:id]) | |
| 42 | - | |
| 43 | - if @admin_user.block | |
| 37 | + if admin_user.block | |
| 44 | 38 | redirect_to :back, alert: "Successfully blocked" |
| 45 | 39 | else |
| 46 | 40 | redirect_to :back, alert: "Error occured. User was not blocked" |
| ... | ... | @@ -48,9 +42,7 @@ class Admin::UsersController < Admin::ApplicationController |
| 48 | 42 | end |
| 49 | 43 | |
| 50 | 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 | 46 | redirect_to :back, alert: "Successfully unblocked" |
| 55 | 47 | else |
| 56 | 48 | redirect_to :back, alert: "Error occured. User was not unblocked" |
| ... | ... | @@ -82,30 +74,34 @@ class Admin::UsersController < Admin::ApplicationController |
| 82 | 74 | params[:user].delete(:password_confirmation) |
| 83 | 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 | 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 | 82 | format.json { head :ok } |
| 92 | 83 | else |
| 93 | 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 | 86 | end |
| 96 | 87 | end |
| 97 | 88 | end |
| 98 | 89 | |
| 99 | 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 | 92 | redirect_to admin_users_path, alert: "User is a project owner and can't be removed." and return |
| 103 | 93 | end |
| 104 | - @admin_user.destroy | |
| 94 | + admin_user.destroy | |
| 105 | 95 | |
| 106 | 96 | respond_to do |format| |
| 107 | - format.html { redirect_to admin_users_url } | |
| 97 | + format.html { redirect_to admin_users_path } | |
| 108 | 98 | format.json { head :ok } |
| 109 | 99 | end |
| 110 | 100 | end |
| 101 | + | |
| 102 | + protected | |
| 103 | + | |
| 104 | + def admin_user | |
| 105 | + @admin_user ||= User.find_by_username(params[:id]) | |
| 106 | + end | |
| 111 | 107 | end | ... | ... |