From 6144b4daef8b257ca94318f94895e881b5df1934 Mon Sep 17 00:00:00 2001 From: karen chan Date: Tue, 31 Mar 2015 01:58:23 +0200 Subject: [PATCH] Add "make them admin" link next to each user in the admin console Close #170 --- app/controllers/admin/users_controller.rb | 12 ++++++++++-- app/views/admin/users/search.js.erb | 23 ++++++++++++++++++----- config/routes.rb | 1 + 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index e0fefde68..e5c44c4a9 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,7 +1,7 @@ module Admin class UsersController < BaseController - before_filter :get_user, only: [:show, :edit, :update, :destroy, :become] + before_filter :get_user, only: [:show, :edit, :update, :destroy, :become, :make_admin] def index handle_with(UsersSearch, @@ -28,6 +28,14 @@ def become redirect_to request.referrer end + def make_admin + if @user.update_attribute(:is_administrator, true) + redirect_to request.referrer, notice: 'User successfully updated.' + else + redirect_to request.referrer, alert: 'Unable to update user.' + end + end + protected def get_user @@ -35,4 +43,4 @@ def get_user end end -end \ No newline at end of file +end diff --git a/app/views/admin/users/search.js.erb b/app/views/admin/users/search.js.erb index b5746efc3..8ede0c1d4 100644 --- a/app/views/admin/users/search.js.erb +++ b/app/views/admin/users/search.js.erb @@ -4,11 +4,11 @@ <% users = @handler_result.outputs[:items] - contents = osu.action_list( + contents = osu.action_list( records: users, list: { - headings: ['Username', 'First Name', 'Last Name', ''], - widths: ['25%', '25%', '25%', '25%'], + headings: ['Username', 'First Name', 'Last Name', '', 'Is Admin?'], + widths: ['20%', '20%', '20%', '20%', '20%'], data_procs: [ Proc.new { |user| user.username }, @@ -18,7 +18,18 @@ link_to 'Sign in as', become_admin_user_path(user), method: :post - } + }, + Proc.new { |user| + if user.is_administrator + 'Yes' + else + link_to 'Make them admin', + make_admin_admin_user_path(user), + method: :post, + remote: true, + class: 'make-admin' + end + }, ] } ) %> @@ -26,4 +37,6 @@ $("#search-results-list").html("<%= j(contents) %>"); $('#search-results-list').prepend("<%= j(render 'admin/users/search_pagination') %>"); -<% end %> \ No newline at end of file + // reload the users table after clicking on the make admin link + $('.make-admin').bind('ajax:complete', function() { $('.tab-pane.active form').submit(); }); +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 8657dcea7..85229d8c4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -112,6 +112,7 @@ resources :users, only: [:index, :show, :update, :edit] do post 'become', on: :member + post 'make_admin', on: :member end end